Home  >  Article  >  Backend Development  >  In C language, explain the concept of pointer structure with appropriate examples

In C language, explain the concept of pointer structure with appropriate examples

WBOY
WBOYforward
2023-09-03 18:29:061011browse

In C language, explain the concept of pointer structure with appropriate examples

The pointer to the structure saves the address of the entire structure.

Mainly used to create complex data structures, such as linked lists, trees, graphs, etc.

You can use a special operator (arrow operator -> ) to access the members of the structure.

Declaration

The following is the declaration of a pointer to a structure:

struct tagname *ptr;

For example, struct Student *s;

Access

You can access a pointer to a structure using the following code -

Ptr-> membername;

For example, s->sno, s->sname, s->marks;

Example

The following is C program for pointer structure -

#include<stdio.h>
struct student{
   int sno;
   char sname[30];
   float marks;
};
main ( ){
   struct student s;
   struct student *st;
   printf("enter sno, sname, marks:");
   scanf ("%d%s%f", & s.sno, s.sname, &s. marks);
   st = &s;
   printf ("details of the student are");
   printf ("Number = %d</p><p>", st ->sno);
   printf ("name = %s</p><p>", st->sname);
   printf ("marks =%f</p><p>", st ->marks);
   getch ( );
}

Output

When the above program is executed, the following results are produced-

enter sno, sname, marks:1 priya 34
details of the student areNumber = 1
name = priya
marks =34.000000

The above is the detailed content of In C language, explain the concept of pointer structure with appropriate examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete