brief explain about structure pointers with With example program

process the structure using a structure pointer

Program

struct student       \\ A
{
    char name[30];   \\ B
    float marks;     \\ C
};              \\ D
 
main ( )
{
    struct student *student1;      \\ E
    struct student student2; \\ F
    char s1[30];
    float f;
    student1 = &student2;    \\ G
    scanf ("%s", name);          \\ H
    scanf (" %f", & f);      \\ I
    *student1.name = s1;         \\ J student1-> name = f;
    *student2.marks = f;         \\ K student1-> marks = s1;
 
    printf (" Name is %s \n", *student1.name);    \\ L
    printf (" Marks are %f \n", *student2.marks); \\ M
}                       

Explanation

1.      Statement E indicates that student1 is the pointer to the structure.
2.      Statement F defines the structure variable student2 so that memory is allocated to the structure.
3.      Statement G assigns the address of the structure student2 to the pointer variable structure student1.
4.      In the absence of statement G, you cannot refer to the structure using a pointer. This is because when you define the pointer to the structure, the memory allocation is done only for pointers; the memory is not allocated for structure. That is the reason you have to declare a variable of the structure type so that memory is allocated to the structure and the address of the variable is given to the point.
5.      Statement J modifies a member of the structure using the * notation. The alternative notation is
6.      student1-> name = f;
student1-> marks = s1;  

0 Comment "brief explain about structure pointers with With example program"

Post a Comment