Structure and Pointer

Storing structure object address into a pointer

Similar to the other variable, structure objects also have an address and this address can be stored into another pointer called structure pointer or a pointer to the structure object.

To store the object's address into a pointer the pointer must be of the same type as the struct object.

Read the code below to understand the fact.

struct Person {
    char name[20];
    int age;
};

struct Person obj = {"Alex",28};
struct Person *p;

p = &obj.

Accessing structure member through pointer

As we have learned to access the structure member through the object dot(.) operator is used.

Similarly to access the structure member from the structure pointer arrow (->) operator is used. arrow(->) is another pointer operator which is used to dereference structure member through object's pointer.

As we have stored the address of obj into a pointer p as shown in the above code snippet. Now following codes snippet shows you how to access structure member through a pointer.

printf("Name: %s\n",(*p).name);
printf("Age: %d\n",(*p).age);

Observe the code p = &obj; clarify that pointer p stores the address of obj.

Now if we dereference the pointer p by writing *p, it jumps to the address obj and then accesses the name(a member of obj) from obj we use obj.name

so combined to access the name from pointer p we write (*p).name that first dereference the pointer then access the member of the object using dot(.) operator as shown in the above code snippet.

Rather than to perform two different operations first dereference and then member access we can directly dereference the structure member from a structure pointer by using the arrow(->) operator as shown in the following code snippet.

printf("Name: %s\n",p->name);
printf("Age: %d\n",p->name);

Program to read and print structure data using functions

#include<stdio.h>

struct book {
	char title[20];
	char author[20];
	int price;
};

void inputData(struct book *p) {
	fflush(stdin);
	printf("Enter title of book: ");
	gets(p->title);
	printf("Enter author of book: ");
	gets(p->author);
	printf("Enter price of book: ");
	scanf("%d",&p->price);
}
void display(struct book obj) {
	printf("%-20s %-20s %-8d\n",obj.title,obj.author,obj.price);
}
int main() {
	struct book b1,b2;
	
	printf("Enter first book data...\n----------------------------\n");
	inputData(&b1);
	printf("Enter second book data...\n---------------------------\n");
	inputData(&b2);
	
	printf("List of books...\n-------------------------------\n\n");
	printf("%-20s %-20s %-8s\n","Title","Author","Price");
	printf("---------------------------------------------------\n");
	
	display(b1);
	display(b2);
	
	return 0;
}

Output

Enter first book data...
----------------------------
Enter title of book: Let us C
Enter author of book: YPK
Enter price of book: 159
Enter second book data...
---------------------------
Enter title of book: Explore C
Enter author of book: Mahesh Shittlani
Enter price of book: 854
List of books...
-------------------------------

Title                Author               Price
---------------------------------------------------
Let us C             YPK                  159
Explore C            Mahesh Shittlani     854

Copying structure object

As a simple variable can copy into another variable by using the assignment operator similarly a structure object can also be copied into another variable by using the assignment operator.

Another way to copy a structure object into another by copying member to member. The following code snippet shows both of the methods.

Copy whole object

struct Person {
   char name[20];
   int age;
};

struct Person p1 = {"Alex",22};
struct person p2 = p1; //copy whole object.

Copy member by member

struct Person {
   char name[20];
   int age;
};

struct Person p1 = {"Alex",22};
struct person p2; 
strcpy(p2.name,p1.name);
p2.age = p1.age;

Returning structure object

As a function can return any type of value, similarly a function can return an object of the struct data type because c allows you to copy one object into another object. 

Return value are received by another object and all values are copied into another.

This method can be used as an alternate way to use the pointer to update the structure object.

The following example shows how to update structure object value by returning the structure object.


Program to demonstrate returning an object

#include<stdio.h>

struct book {
	char title[20];
	char author[20];
	int price;
};

struct book inputData() {
	struct book obj;
	fflush(stdin);
	printf("Enter title of book: ");
	gets(obj.title);
	printf("Enter author of book: ");
	gets(obj.author);
	printf("Enter price of book: ");
	scanf("%d",&obj.price);
	
	return obj;
}
void display(struct book obj) {
	printf("%-20s %-20s %-8d\n",obj.title,obj.author,obj.price);
}
int main() {
	struct book b1,b2;
	
	printf("Enter first book data...\n----------------------------\n");
	b1 = inputData();
	printf("Enter second book data...\n---------------------------\n");
	b2 = inputData();
	
	printf("List of books...\n-------------------------------\n\n");
	printf("%-20s %-20s %-8s\n","Title","Author","Price");
	printf("---------------------------------------------------\n");
	
	display(b1);
	display(b2);
	
	return 0;
}

Output

Enter first book data...
----------------------------
Enter title of book: You can win
Enter author of book: Shiv Khera
Enter price of book: 155
Enter second book data...
---------------------------
Enter title of book: Wings of the fire
Enter author of book: Dr. Abdul Kalaam
Enter price of book: 250
List of books...
-------------------------------

Title                Author               Price
---------------------------------------------------
You can win          Shiv Khera           155
Wings of the fire    Dr. Abdul Kalaam     250

Explanation

As shown in the above example, in inputData() function input is received into a local object and then that object is returned by the function. The returned object is copied on the object resides on the left side of the function call.