Write about Pointers and Structures?
https://www.computersprofessor.com/2016/06/write-about-pointers-and-structures.html?m=0
We know that the name of an array stands for the add of
its zeroth etc. The same thing is true
of the names of a arrays of structure variable.
Suppose
‘product’ is an array variable of struct type. The name ‘product’ represents
the add of its zeroth element.
Ex:–
struct inventory
{
char name[20];
int no;
float price;
}
product [2],*ptr;
This
statement declares product as an array of 2 elements, each of the type struct
inventory & ptr as apointer to data objects of the type struct inventory. The
assignment
ptr=product;
Would
assign the address of the zeroth element of product to ptr.
i.e; the
pointer ptr will now point to product[0]. It’s members can be accessed using
the following notation.
ptr->name
ptr->no
ptr->price
the symbol -> is called the arrow operator (member
selection operator) and is made up of a minus sign and a greater than sign.
When the
pointer ptr is incremented by one, it is made to point to the next record,
i.e., product[1].
The
following for statement will print the values of members of all the elements of
product array.
for(ptr=product;ptr
printf(“%s%d%f\n”,ptr->name, ptr->no, ptr->price);
Ex:–Structure using pointers.
#include<stdio.h>
#include<conio.h>
main()
{
struct book
{
char
name[20];
char
author[20];
int id;
};
struct book b1={“C”, “swamy”, 100};
struct book *p;
p=&b1;
printf(“%s%s%d\n”, b1.name,b1.author, b1.id);
printf(“%s%s%d\n”, p->name, p->author, p->id);
getch();
}