Lab 4
Lab 4
Fall 2024
Lab-4 Manual
Pointers and Structures in C
v1.0
12/06/2022
Objective
To understand programming with Pointer, String, and Structure.
Task # 1
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
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.
Write a program to find the sum of all the elements of an array using pointers.
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:-
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.