Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Pointer to array and structure
1. Pointer to Array and
Structure
Prepared By: Mr. S. A. Patil
Assistant Professor,PVPIT Budhgaon
2. Pointers to Array
Array is collection elements with same datatype.
When we create an array some memory space is allocated to that array.
So we can create pointer of array also.
By using pointer to array we can access elements of array
4. Access array using pointer
int numbers[5]={10,20,30,40,50};
int *p,i;
p=&numbers; // p=numbers;
for( i=0; i<5 ; i++)
{
printf(“n%d”,*(p+i));
}
5. Pointer to structure
Structure is collection of elements with different datatypes.
When we define any structure we create a user defined datatype of structure type.
Memory is allocated to each structure variable.
We can also create pointer to structure variable.
And access structure by using pointer
7. Access structure elements using pointer
We can access structure elements by using -> operator
Write data into structure variable
printf(“nEnter Roll No:”);
scanf(“%d”,&ptr->roll);
printf(“nEnter Marks:”);
scanf(“%f”,&ptr->marks);
Read structure variable
printf(“nRoll number: %d”,ptr->roll);
printf(“nMarks: %f”,ptr->marks);
8. Pointer to array of structure
It is possible to create pointer to array of structure as regular array.
struct student
{
int roll;
float marks;
};
struct student s[5];
struct student *ptr;
ptr=&s;
9. Access array of structure using pointer
We can access structure elements by using -> operator
Write data into structure variable
for(i=0;i<5;i++)
{
printf(“nEnter Roll No:”);
scanf(“%d”,&ptr->roll);
printf(“nEnter Marks:”);
scanf(“%f”,&ptr->marks);
ptr++;
}
10. Access array of structure using pointer
Read structure variable
for(i=0;i<5;i++)
{
printf(“nRoll number: %d”,ptr->roll);
printf(“nMarks: %f”,ptr->marks);
ptr++;
}