# Pointer to Structure – Basics

## Pointer to Structure

Structure is a User Defined Data Type. Before discussing Pointer to Structure, you must know about Structures.

##### Example of a Structure :
```struct node
{
int a;
float b;
char c[2];
};```
• Point No 1 :
It is a mixture of various data types.
• Point No 2 : Memory of a structure (Assume 16-bit Processor) :
struct node;  ⇒ It will take no memory.
struct node d; ⇒ It will take memory. How to calculate ?
integer will take 2 bytes; float will take 4 bytes and char will take 1 byte.
So, 2 + 4 + 1*2 = 8 Bytes
So, the size of the “d” is 8 Bytes.
• Point No 3 : Data Type of structure variable “d” :
struct node is the data type of “d”. Neither the node nor the struct is the data type of “d”. Combinedly struct node is a data type.
• Point No 4 : Addressing of a structure :
Suppose the address of the node ‘d’ in 1000. Now, we  want to know, what will be the address of integer variable ‘a’, float variable ‘b’, and character variable ‘c’. Since structure is a mixture of various data types, So, the address of elements will be according to the number of bytes they occupied. So,

• node ‘d’ is stored at address 1000 ⇒ Ist element of structure i.e. ‘a’ is stored at 1000,
• since integer variable ‘a’ occupy 2 bytes, therefore, the next element address i.e. ‘b’ will be stored at address (1000 + 2) = 1002
• Since float variable ‘b’ occupy 4 bytes, therefore, the next element address i.e. ‘c’ will be stored at address (1002 + 4) = 1006

### Structure Initialization :

The value of structure elements cannot be initialized in the structure definition.

```struct node
{
int a = 10;   //wrong
float b;
char c[2];
}d;```

The correct form of initialisation of structure elements is :

```struct node d = {10,20.51,{'S','R'}};
OR
d.a = 10;
d.b = 20.51;
d.c[0] = 'S';
d.c[1] = 'R';```

The second method of initialization can be used only when the structure variable is defined. It cannot be used before the structure variables have been defined.

### Accessing Structure Elements :

• printf(“%d”,d.a) will print 10.
• printf(“%f”,d.b) will print 20.5
• printf(“%s”,d.c) will print SR.

### Pointer to Structure or Accessing Structure Elements using Pointers :

```struct node d = {10,20.51,{'S','R'}};
struct node *p = &d;

For Accessing Structure Elements using pointers, there are two operators used :
1. .(dot Operator)  ⇒ (*p).a or (*p).c
2. →(arrow Operator) ⇒ p → a or p → b```

#### Program No 1:

```struct node
{
int a;       // 2Bytes
float b;     // 4 Bytes
char c;      // 1 Byte
}             // Total Size of structure node = 2+4+1=7 Bytes
struct node 1
{
float d;        // 4 Bytes
struct node e;  // 7 Bytes
}                // Total Size of structure node 1 = 7+4=11 Bytes
main()
{
struct node1 f = {1o.25, 5, 20.65, 'S'};
printf("%d", f.d);
printf("%d",f.e.b)
printf("%d",sizeof(struct node1));
printf("%d",sizeof(struct node));
}```
```10.25
20.65
11
7```

#### Program No 2:

```struct a
{
char ch[7];       // 7 Bytes
char *str         // 2 Bytes
}             // Total Size of structure node = 7+2=9 Bytes
struct b
{
char *c;        // 2 Bytes
struct a ssl;  //  9 Bytes
}                // Total Size of structure node 1 = 9+2=11 Bytes
main()
{
struct b S2 = {"RAIPUR","KANPUR","JAIPUR"};
printf("%s \n%s", S2.c, S2.ssl.str);
printf("\n%s \n%s",++S2.c,++S2.ssl.str);
}```
###### Output :
```RAIPUR
JAIPUR
AIPUR
AIPUR```
###### Explanation :

Cick for Some More Programs on the concept of Structures and Pointers