12 Structure - Union and Pointer
12 Structure - Union and Pointer
Structures
3
Structures
• Definition of a structure:
struct <struct-type>{
<type> <identifier_list>; Each identifier
<type> <identifier_list>; defines a member
... of the structure.
} ;
• Example:
struct Date {
int day; The “Date” structure
int month; has 3 members,
int year;
day, month & year.
} ;
struct examples
• Example:
struct StudentInfo{
int Id;
int age; The “StudentInfo”
char Gender; structure has 4 members
double CGA;
}; of different types.
• Example:
struct StudentGrade{
char Name[15];
char Course[9];
int Lab[5];
The “StudentGrade”
int Homework[3]; structure has 5
int Exam[2];
members of
};
different array types.
struct examples
• Example:
struct BankAccount{
char Name[15]; The “BankAcount”
int AcountNo[10]; structure has simple,
double balance; array and structure
};
types as members.
• Example:
struct StudentRecord{
char Name[15];
The “StudentRecord”
int Id;
char Dept[5]; structure has 4
char Gender; members.
};
struct basics
Example:
StudentRecord Student1, Student2;
Name Name
Student1 Student2
Id Gender Id Gender
Dept Dept
COMPUTER
Declaring Structures (struct)
void main( )
{
struct record s2={54,200.50};
printf("%d \t %f \n",s1.weight,s1.height);
printf("%d \t %f",s2.weight,s2.height);
}
Array of structure
#include <stdio.h>
struct marks {
int sub1;
int sub2;
int sub3;
};
struct marks m[3];
m[0].sub1=45;
void main( ) m[0].sub2=55;
m[0].sub3=65;
{
struct marks m[3]={
{45,55,65},{85,59,62},{59,57,63},
};
int i;
for(i=0;i<=2;i++)
printf("%d %d %d \n",m[i].sub1,m[i].sub2,m[i].sub3);
Structure and function
#include <stdio.h>
struct student { void show(struct student s)
{
char name[20];
printf("Your Name: %s \t", s.name);
int age; printf("Your Age: %d", s.age);
}; }
void show(struct student);
void main()
{
struct student s1;
printf("Enter your name: ");
scanf("%s", s1.name);
printf("Enter your age: ");
scanf("%d", &s1.age);
show(s1); /* passing struct to function */
}
• A union is a user defined type similar to
structs in C except for one key difference.
Unions • Structs allocate enough space to store all its
members wheres unions allocate the space
to store only the largest member.
union items
{
int m;
Unions float x;
char c
}code;
Example
#include <stdio.h>
union Data
{
int i;
float f;
char name[20];
};
void main( )
{
union Data d;
d.i = 100;
d.f = 20.5;
strcpy( d.name, "Krishna");
printf( "%d \n %f \n%s", d.i, d.f, d.name);
1936290379
}
8495533826379695000000000000000.000000
Krishna
Example #include <stdio.h>
union Data
{
int i;
float f;
char name[20];
};
void main( )
{
union Data d;
d.i = 100;
printf( "i : %d\n", d.i);
d.f = 20.5;
printf( "f : %f\n", d.f);
strcpy( d.name, "My name is amit");
printf( "name : %s\n", d.name);
i : 100
} f : 20.500000
name : My name is amit
Pointers
Pointers
• Within the computer memory, every stored data item occupies one
or more contiguous memory cells.
• The number of memory cells required to store a data item depends on its type
(char, int, double, etc.).
• Whenever we declare a variable, the system allocates memory
location(s) to hold the value of the variable.
• Since every byte in memory has a unique address, this location will also have
its own (unique) address.
• Consider the statement
int x = 50;
• This statement instructs the compiler to allocate a location for the integer
variable x, and put the value 50 in that location.
• Suppose that the address location chosen is 1300.
X ➔ variable
50 ➔ value
1380 ➔ address
• During execution of the program, the system always associates the
name x with the address 1300.
• The value 50 can be accessed by using either the name x or the address
1300.
• Since memory addresses are simply numbers, they can be assigned
to some variables which can be stored in memory.
• Such variables that hold memory addresses are called pointers.
• Since a pointer is a variable, its value is also stored in some memory location.
• Suppose we assign the address of x to a variable p.
• p is said to point to the variable x.
Variable Value
Address p = &x;
x 50
1300
p 1300 1 1300 50
2505
p x
Accessing the Address of a Variable
&235
• Pointing at constant.
int arr[20];
:
&arr;
• Pointing at array name.
&(a+b)
• Pointing at expression.
Example
#include <stdio.h>
main()
{
int a;
float b;
double d;
char c;
}
a is stored in location 6487580
b is stored in location 6487576
d is stored in location 6487568
c is stored in location 6487567
Pointer Declarations
• Example:
int *ptr;
float *point;
• Once a pointer variable has been declared, it can be made to
point to a variable using an assignment statement like:
int *p, x;
:
p = &x;
• This is called pointer initialization.
Accessing a Variable Through its Pointer
p = &c;
b = 4 * (*p + 5) ;
printf (“a=%d b=%d \n”, a, b) ;
}
Example 2
#include <stdio.h>
main()
{
int x, y;
int *ptr;
x = 10 ;
ptr = &x ;
y = *ptr ; ptr= 6487568
printf ("ptr= %d\n",ptr ); *ptr= 10
printf ("*ptr= %d\n", *ptr) ; y= 10
printf ("y= %d\n", y) ; Now x = 50
*ptr = 50;
printf ("Now x = %d \n", x);
}
Pointer Expressions
#include <stdio.h>
main() a and b
{
int a, b; do not
a = 5 ; b = 20 ; swap
swap (a, b) ;
printf (“\n a = %d, b = %d”, a, b); Output
}
a = 5, b = 20
void swap (int x, int y)
{
int t ;
t=x;
x=y;
y=t; x and y swap
}
Example: passing arguments by reference
#include <stdio.h>
main()
{ *(&a) and *(&b)
int a, b;
a = 5 ; b = 20 ; swap
swap (&a, &b) ;
printf (“\n a = %d, b = %d”, a, b);
}