Unit - 5 - Structures and Pointers-1
Unit - 5 - Structures and Pointers-1
change
RV College of
Engineering
the world
STRUCTURES AND UNIONS
• Introduction
• Structure definition
• Declaring structure variables
• Accessing structure members
• Structure initialization
• Copying and comparing structure variables
• Structures within Structures
Go,
change
RV College of
Engineering
These variables can be of different types, and each has a name which is used to
• For example: A student has various data such as- Name, USN, Courses taken, Marks
• The variables or members of the structure are declared within the body.
struct student{
char name[20];
char usn[10];
int courses;
float marks1, marks2,marks3;
} S1,S2,S3;
Go,
change
RV College of
Engineering
element.
Go,
change
RV College of
Engineering
1. Tagged declaration
2. Typedef declaration
Go,
change
RV College of
Engineering
(structure name).
struct tag_name
{ struct product
{
data-type var-name1;
int pid;
data-type var-name2; char name[20];
: int qnt;
float price;
data-type var-nameN;
};
};
Go,
change
RV College of
Engineering
• It requires identifier at the end of structure block (“}”) and before the semicolon (;).
• Structure variable can be declared as local by declaring it inside the main () function
as follow:
Go,
change
RV College of
Engineering
the world
Memory requirements of structures and structure variables
• Memory representation for the structure variable can be given as shown into the below figure.
pid (int ) = 2 bytes + name (char) 20 bytes + qnt (int) 2 bytes + price(float) 4 bytes = 28 bytes
Go,
change
RV College of
Engineering
struct product
{
int pid;
char name[20];
int qnt;
float price;
};
Go,
change
RV College of
Engineering
• To print this value assigned to the member on the screen, the following code is written. printf(“%d”, s2.b);
Go,
change
RV College of
Engineering
the world
Declaration and Initialization of Pointers
• A pointer has to be declared.
• It will have a value, a scope, a lifetime, a name; and it will occupy a certain
number of memory locations.
• The pointer operator available in C is ‘*’, called value at address operator.
• It returns the value stored at a particular address.
• The value at address operator is also called indirection operator.
• A pointer variable is declared by preceding its name with an asterisk.
• The syntax for declaring a pointer variable is:
datatype * pointer_variable;
Go,
change
RV College of
Engineering
the world
Declaration and Initialization of Pointers
For Example:
char * ptr;
This declaration is evaluated as: ptr is a pointer to char type data.
Table meaning of some pointer type variable declarations
Declaration What it means
int p P is an integer
int *p P is a pointer to an integer
char p P is a character
char *p P is a pointer to a character
long p P is a long integer
long *p P is a pointer to a long integer
Go,
change
RV College of
Engineering
the world
Declaration and Initialization of Pointers
WHAT IS THE OUTPUT OF FOLLOWING CODE?
int main(){
int *p;
float *q;
double *r;
printf("\n the size of integer pointer is %d", sizeof(p));
printf("\n the size of float pointer is %d", sizeof(q));
printf("\n the size of double pointer is %d",sizeof(r));
printf("\n the size of character pointer is %d", sizeof(char *));
return 0;}
Go,
change
RV College of
Engineering
the world
Declaration and Initialization of Pointers
WHAT IS THE OUTPUT OF FOLLOWING CODE?
int main(){
int *p;
float *q;
double *r;
printf("\n the size of integer pointer is %d", sizeof(p));
printf("\n the size of float pointer is %d", sizeof(q));
printf("\n the size of double pointer is %d",sizeof(r));
printf("\n the size of character pointer is %d", sizeof(char *));
return 0;}
Go,
change
RV College of
Engineering
the world
Declaration and Initialization of Pointers
why pointers should have data types
• C has data types of different size, i.e., objects of different types will
have different memory requirements.
• It supports uniformity of arithmetic operations across different
(pointer) types.
Go,
change
RV College of
Engineering
the world
Where is a pointer stored?
• A pointer is like any other variable in the sense that it requires storage space
somewhere in the computer’s memory, but it is not like most variables because it
contains no data, only an address.
• Since it is an address, it actually contains a number referring to some memory
location
Go,
change
RV College of
Engineering
the world
Initializing Pointers
• Consider the following example:
#include int main()
{
int *p; /* a pointer to an integer */
printf(“%d\n”,*p);
return 0;
}
Note: A pointer should be initialized with another variable’s memory address, with 0,
or with the keyword NULL prior to its use; otherwise the result may be a compiler
error or a run-time error.
Go,
change
RV College of
Engineering
the world
Initializing Pointers
#include <stdio.h>
int main()
{
int i = 5;
int *ptr = &i;
printf(“\nThe address of i using &num is %p”, &i);
printf(“\nThe address of i using Ptr is %p”, ptr);
return 0;
}
Note: Type of the Pointer and the data type of the variable must be same.
Go,
change
RV College of
Engineering
the world
Printing pointer value
#include <stdio.h>
int main()
{
int i = 5;
int *ptr = &i;
printf(“\nThe address of i using &num is %p”, &i);
printf(“\nThe address of i using Ptr is %p”, ptr);
return 0;
}
Go,
change
RV College of
Engineering
Assigning constantthe
to a world
Examples:
int main(void)
int a=3, *ip;
{ pointer
int a=10, *p; int *pi;
float *p;
p=&a; pi= (int*)1000;
char ch=’A’;
printf(“\n p = %p”, p); *pi = 5;
p=&a;
return 0;
ip=&ch;
}
P Assigning constant to a
int i=3;
pointer
int *p, *q, *r; i q
p = &i;
r
q = &i;
Go,
change
RV College of
Engineering
the world
REMEMBER:
• In C, pointers are not allowed to store any arbitrary memory address,
but they can only store addresses of variables of a given type
Go,
change
RV College of
Engineering
Examples: USING INDIRECTION OPERATOR and DEREFERENCING
main(){
the world
int i=5;
int *p;
p = &i;
printf(“\nValue of i = %d”, i);
printf(“\nValue of * (&i) = %d”, *(&i));
return 0;
}
Note:
Printing the value of *(&i) is same as printing the value of i.
• always implies value at address.
• *(&i) is identical to i.
Go,
change
RV College of
Engineering
Examples: USING INDIRECTION OPERATOR and DEREFERENCING
‘value at address’.