ProDeveloperTutorial.com

Tutorials and Programming Solutions
Menu
  • Shell Scripting
  • System Design
  • Linux System Programming
  • 4g LTE
  • Coding questions
  • C
  • C++
  • DSA
  • GIT
  • 450 DSA Cracker
  • 5G NR
  • O-RAN

Advanced C Pointer Programming chapter 7: Pointers and Structures.

prodevelopertutorial August 3, 2020

In C, after arrays, structures are the most important data structure available for programmers.

We shall understand different ways to initialize memory to structure and accessing elements inside a structure.

Creating simple structure and accessing elements inside them

In this example, we shall create a structure variable inside main and allocate memory in stack.

Below is a “student” structure that has 2 elements. A char array to hold name and int variable to hold roll number.

Then we create a structure variable inside main, so it will create memory inside stack.

Then as we have statically allocated memory, we can access the elements by dot “.” operators.

 

Example:

#include<stdio.h>  
#include<stdlib.h>  
#include<string.h>  

struct student_database
{
    char name[30];
    int roll_no;
};

int main() 
{ 
    struct student_database stud = {"jane", 2};

    printf("Student name = %s \n", stud.name);
    printf("Student roll no = %d \n", stud.roll_no);

    return 0;
}

 

Output:

Student name = jane
Student roll no = 2

 

Dynamically allocating memory for structure and accessing elements inside them

To dynamically allocate memory you need to use malloc.

This will allocate memory in heap segment.

To access the elements, you need to use arrow operator “->”.

Example:

#include<stdio.h>  
#include<stdlib.h>  
#include<string.h>  

struct student_database
{
    char name[30];
    int roll_no;
};

int main() 
{ 
    struct student_database *stud = NULL;
    stud = (struct student_database*) malloc(sizeof(struct student_database));

    stud->roll_no = 30;
    strcpy(stud->name, "Jane Doe");

    printf("Student name = %s \n", stud->name);
    printf("Student roll no = %d \n", stud->roll_no);

    return 0;
}

Output:

Student name = Jane Doe
Student roll no = 30

 

Creating simple structure with a char pointer and accessing elements inside them

In this example, we shall create a structure variable inside main and allocate memory inside stack.

Below is a “student” structure that has 2 elements. A char pointer to hold name and int variable to hold roll number.

Then we create a structure variable inside main, so it will create memory inside stack.

Once you have created static memory for the structure variable, for the char pointer, only the memory for that pointer will be allocated.

Hence you need to explicitly allocate memory for the string to be held. This is done with the help of dynamic memory management.

Then as we have statically allocated memory, we can access the elements by dot “.” operators.

Example:

#include<stdio.h>  
#include<stdlib.h>  
#include<string.h>  

struct student_database
{
    char *name;
    int roll_no;
};

int main() 
{ 
    struct student_database stud;

    stud.name = (char*) malloc(sizeof("www.ProdeveloperTutorial.com")+1);
    strcpy(stud.name, "www.ProdeveloperTutorial.com");

    stud.roll_no = 20;

    printf("Student name = %s \n", stud.name);
    printf("Student roll no = %d \n", stud.roll_no);

    return 0;
}

Output:

Student name = www.ProdeveloperTutorial.com
Student roll no = 20

 

Creating simple structure with a char pointer and accessing elements inside them. Allocate memory dynamically.

Here also when we allocate memory dynamically for the structure, space is allocated for char pointer only.

So you need to allocate memory to accommodate the string dynamically.

#include<stdio.h>  
#include<stdlib.h>  
#include<string.h>  

struct student_database
{
    char *name;
    int roll_no;
};


int main() 
{ 
    struct student_database *stud = NULL;
    stud = (struct student_database*) malloc(sizeof(struct student_database));

    stud->roll_no = 30;
    stud->name= (char *) malloc( sizeof("Jane Doe")+1);
    strcpy(stud->name, "Jane Doe");

    printf("Student name = %s \n", stud->name);
    printf("Student roll no = %d \n", stud->roll_no);

    return 0;
}

Output:

Student name = Jane Doe
Student roll no = 30

 

How to accessing elements of structure inside a structure:

#include<stdio.h>  
#include<stdlib.h>  
#include<string.h>  

struct student_detail
{
    char name[40];
    int roll_no;
};

struct student_database
{
    struct student_detail detail;
    int percentage;
};



int main() 
{ 
    // statically allocated memory
    struct student_database student_database_1;

    student_database_1.percentage = 80;
    student_database_1.detail.roll_no = 30;
    strcpy(student_database_1.detail.name, "Jane Doe");

    printf("Student 1 percentage = %d\n", student_database_1.percentage);
    printf("Student 1 roll_no = %d\n", student_database_1.detail.roll_no);
    printf("Student 1 name = %s\n", student_database_1.detail.name);

    struct student_database *student_database_2;
    student_database_2 = (struct student_database*) malloc(sizeof(struct student_database));

    student_database_2->percentage = 70;
    student_database_2->detail.roll_no = 10;
    strcpy(student_database_2->detail.name , "New John"); 

    printf("Student 2 percentage = %d\n", student_database_2->percentage);
    printf("Student 2 roll_no = %d\n", student_database_2->detail.roll_no);
    printf("Student 2 name = %s\n", student_database_2->detail.name);

    return 0;
} 


 

Output:

Student 1 percentage = 80
Student 1 roll_no = 30
Student 1 name = Jane Doe
Student 2 percentage = 70
Student 2 roll_no = 10
Student 2 name = New John

Pass structure to a function by value

#include<stdio.h>  
#include<stdlib.h>  
#include<string.h>  

struct student_database
{
    char name[30];
    int roll_no;
};

void display_data(struct student_database stud)
{
    printf("Student name = %s \n", stud.name);
    printf("Student roll no = %d \n", stud.roll_no);
}

int main() 
{ 
    struct student_database stud = {"jane", 2};

    display_data(stud);


    return 0;
} 

 

Output:

Student name = jane
Student roll no = 2

Pass structure to a function by reference

#include<stdio.h>  
#include<stdlib.h>  
#include<string.h>  

struct student_database
{
    char name[30];
    int roll_no;
};

void display_data(struct student_database *stud)
{
    printf("Student name = %s \n", stud->name);
    printf("Student roll no = %d \n", stud->roll_no);
}

int main() 
{ 
    struct student_database stud = {"jane", 2};

    display_data(&stud);


    return 0;
} 


Output:

Student name = jane
Student roll no = 2

 

Return structure to a function

#include<stdio.h>  
#include<stdlib.h>  
#include<string.h>  

struct student_database
{
    char name[30];
    int roll_no;
};

struct student_database* get_data()
{
    struct student_database *stud = NULL;
    stud = (struct student_database*) malloc(sizeof(struct student_database));    stud->roll_no = 30;
    strcpy(stud->name, "Jane Doe");

    return stud;
}

int main() 
{ 
    struct student_database *stud = NULL;
    stud = get_data();

    printf("Student name = %s \n", stud->name);
    printf("Student roll no = %d \n", stud->roll_no);

    return 0;
} 

Output:

Student name = Jane Doe
Student roll no = 30

 

 

 

Share
Email
Tweet
Linkedin
Reddit
Stumble
Pinterest
Prev Article
Next Article

About The Author

prodevelopertutorial

Follow this blog to learn more about C, C++, Linux, Competitive Programming concepts, Data Structures.

Leave a Reply Cancel Reply

You must be logged in to post a comment.

ProDeveloperTutorial.com

Tutorials and Programming Solutions
Copyright © 2023 ProDeveloperTutorial.com
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT