Write about Pointers & Structures?
https://www.computersprofessor.com/2016/06/write-about-pointers-structures.html
We know that the name of an array stands for the add of
its zeroth element. The same thing is true
of the names of a arrays of structure variables.
Suppose
‘product’ is an array variable of struct type. The name ‘product’ represents
the address of its zeroth element.
Ex:–
srtuct inventory
{
char name[20];
int no;
float price;
}
product [2],*ptr;
This
statement declares product as an array of two elements, each of the type struct
inventory and ptr as a pointer 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 & 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<product+2;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();
}