Thursday 29 October 2015

Structure Pointers Simplified


I was running away when it came to structure pointers. It might be my mind block, that I never tried to properly learn structure pointers.

Once I did, it was all easy. You need a good approach for learning such seemingly complex but simple things.

Simple Definition

Structure Pointers are the pointers which points structure. Ha Ha Ha...

This might look funny but it is as simple as that. They are like any other char pointer and int pointers, but they points to a structure.


Declaration of Structure Pointer

Here I am assuming you know how to define structure and use it.

Normally you define structure out side of any function and then take an object of that structure. Here we will take a pointer object of the same structure.

1
2
3
4
5
6
7
8
struct <name>{
        member1;
        member2;
        ...
};

// In the function where you want to use it
struct <name> *ptr;

Here from line no 1 to line no 5 I have shown normal structure declaration. In line no 7 I am taking pointer object of type structure which is defined above. It is written where ever you need to take an object of structure, may it be outside of function or inside function.

Example of Structure Pointer

Let's see an example to make it more clear.

In the above declaration part, line number 3 to 6 shows how a normal structure is being defined, which is quite normal in any case.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>

struct productinfo{
 float   prod_price;
 float offer_price;
};

main(){
 struct productinfo *ptr, apple;

 // Direct assignment of value to member of structure
 apple.prod_price = 5.2;
 printf("\nPrice of Apples is : %f\n\n", apple.prod_price);

 // Assignment of value to member of structure using pointer
 ptr = &apple;

 // Method 1 for assignment of value
 (*ptr).offer_price = 2.5;
 printf("Method 1: Offer Price of Apples is : %f\n\n", (*ptr).offer_price);

 // Method 2 for assignment of value
 ptr->offer_price = 1.3;
 printf("Method 2: Offer Price of Apples is : %f\n\n", ptr->offer_price);

 return 0;
}

The program above demonstrates direct assignment of value to member of a structure and two other methods to assign values using structure pointers.


9
struct productinfo *ptr, apple;

Line no 9 shows that we have taken an object apple of structure named productinfo.
We have taken another object *ptr which is pointer(because it starts with *) of type productinfo structure.


12
apple.prod_price = 5.2;

Line no 12 shows how we normally use to assign values to members of structures. This is called direct assignment of values.


16
ptr = &apple;

Line no 16 initializes structure pointer *ptr with base address of object apple.


19
(*ptr).offer_price = 2.5;


Line no 19 shows Method 1 for assigning values to structure member using structure pointer. (*ptr) points to base address of apple object in our case.


23
ptr->offer_price = 1.3;

Line no 23 shows Method 2 for assigning values to structure member using structure pointer by dereferencing operator. When you dereference the pointer with one of the members of structure, it points to address of that particular member.

If you want to use structure pointer with scanf function, you just need to substitute the variable in the scanf parameters with our pointer string.


1
2
3
scanf("%f", &(*ptr).prod_price);

scanf("%f", &ptr->offer_price);

Example shows both methods which can be used to take the values using structure pointer.


Conclusion

We can conclude that 

(*ptr).prod_price   is same as ptr->prod_price and
(*ptr).offer_price is same as ptr->offer_price.

They can be used interchangeably.

If you like the content, like our facebook page. 



No comments:

Post a Comment