0% found this document useful (0 votes)
9 views4 pages

Lab 4

Uploaded by

mrb4jw42002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Lab 4

Uploaded by

mrb4jw42002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

GIFT School of Engineering

and Applied Sciences

Fall 2024

CS-349: Operating System- Lab

Lab-4 Manual
Pointers and Structures in C

v1.0

12/06/2022
Objective
To understand programming with Pointer, String, and Structure.

Task # 1

Program to concatenate two strings without using library functions

Objectives: Study string –using char arrays - in C language.


Tools/Equipment: Editors like gedit, and vi editor in Linux with GCC on desktop or laptop
computers.

THEORY:
Strings are one-dimensional arrays of characters terminated by a null character '\0'

ALGORITHM:
step1: start
step2: Read two strings in two arrays str1,str2; let 1=j=0;
Step 3:if str1[i] not equal to ‘\0’ ,increment i, repeat this step
Step 4:if str2[j] not equal to ‘\0’ go to next step otherwise go to step 7
Step 5:str1 [i]=str2[j]
Step 6: increment i and j, go to step 4
Step 7:str1 [i]=’\0’
Step 8: Print Concatenated string str1
Step 9: stop

Input Output
Enter the first string: hello
Enter the second string: world
Concatenated String is: helloworld
Task # 2
Program to print the elements of an array using pointers

Objectives: Study pointers in C language.

Tools/Equipment: Editors like gedit, and vi editor in Linux with GCC on desktop or laptop
computers.

THEORY
A pointer is a variable whose value is the address of another variable, i.e., the direct address of
the memory location. Like any variable or constant, you must declare a pointer before using it
to store any variable address. The general form of a pointer variable declaration is −

type *var-name;

Here, type is the pointer's base type; it must be a valid C data type and var-name is the name
of the pointer variable. The asterisk * used to declare a pointer is the same asterisk used for
multiplication. However, in this statement, the asterisk is being used to designate a variable as a
pointer.

Every variable is a memory location and every memory location has its address defined which
can be accessed using the ampersand (&) operator, which denotes an address in memory.

(a) We define a pointer variable, int *p


(b) Assign the address of a variable to a pointer p=&x, x is an integer variable
(c) Finally access the value at the address available in the pointer variable. v=*p

Write a program to find the sum of all the elements of an array using pointers.

Structure Exercise Questions:


Create a structure named Student which has rollNo, name, address, age, and average_marks
as member variables. Read five students' data from the user and display the details from the
function.
Objectives: Study structure concepts in C language.
Tools/Equipment: Editors like gedit, and vi editor in Linux with GCC on desktop or laptop
computers.

Theory
The structure is a user-defined data type available in C that allows combining data items of
different types
To define a structure, you must use the struct statement. The struct statement defines a new
data type, with more than one member. The format of the struct statement is as follows:-

struct [structure tag] {


member definition;
member definition;
...
member definition;
} [one or more structure variables(optional)];

To access any member of a structure, we use the member access operator (.).

The member access operator is coded as a period between the structure variable name and the
structure member that we wish to access.

We would use the keyword struct to define variables of structure type.


struct Books Book1;
Book1.title
To access the members of a structure using a pointer to that structure, you must use the →
operator as follows:-
struct Books *struct_pointer;
struct_pointer->title;

You might also like