Skip to main content

Posts

Showing posts with the label Structure and Pointers

Structure and Pointers

Structures can be created and accessed using pointers. A pointer variable of a structure can be created as below: struct name { member1; member2; ..... }; int main() { struct name *ptr; } Here, the pointer variable ptr of type struct name is created. Accessing structure's member through pointer A structure's member can be accessed through pointer in two ways: 1.Referencing pointer to another address to access memory. 2.Using dynamic memory allocation. 1. Referencing pointer to another address to access the memory Consider an example to access structure's member through pointer. #include <stdio.h> typedef struct person { int age; float weight; }; int main() { struct person *personPtr, person1; personPtr = &person1; // Referencing pointer to memory address of person1 printf("Enter Age: "); scanf(“%d”,&personPtr->age); or scanf("%d",&(*personPtr).age); printf("Enter Weight: "); scanf("%f",&pe