pointers as member

Pointers as Member of Structure in C

A pointer could be a member of structure, but you should be careful before creating the pointer as a member of structure in C. Generally we take a pointer as a member when we don’t know the length of the data which need to store.

Let’s see an example to the better understanding,

typedef struct
{
    int iLen;
    char *pcName;
} Info;

 

The above structure “Info” contains two members, integer variable (iLen) and a pointer to the character (pcName).

 

How to access pointer member of structure in C

Similar to another members pointer member is also access by the structure variable or pointer with the help of dot ( . ) or arrow ( -> ) operator. The left (first) operand of the operator should be variable of structure or pointer to the structure and right (second) operand shall name of a pointer member that you want to access.

See the below code, in which we are creating a structure variable and initializing the variable with literal string and their length. We are also assigning the address of the structure variable (MyInfo) to the pointer to the structure (ptrMyInfo).

#include<stdio.h>

typedef struct
{
    int iLen;

    char *pcName;

} Info;


int main()
{

    //structure variable
    Info MyInfo = { 11,  "Aticleworld"};

    //pointer to structure
    Info *ptrMyInfo = &MyInfo;

    //Used arraw operator
    printf("ptrMyInfo->pcName = %s\n\n",ptrMyInfo->pcName);


    //Used dot operator
    printf("PMyInfo.pcName = %s\n",MyInfo.pcName);

    return 0;
}

Output:

pointer as member in structure

 

If you like the video course, you can check this course created by my friends Kenny Kerr. The course contains video lectures of 4.13-hour length covering all basic topics of c language.

 

How to assign a value to a pointer member of structure in C

Before assigning a value to a pointer you should assign a valid memory. If you don’t assign a valid memory, you will get the undefined behavior. There is two way to access the value of a pointer member of a structure in C.

1.  Using the structure variable

 

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

typedef struct
{
    int *piData;

    char *pcName;

} Info;


int main()
{

    //structure variable
    Info MyInfo;

    //Allocate memory for integer
    MyInfo.piData =  malloc(sizeof(int));

    //check allocated memory
    if((MyInfo.piData) == NULL)
    {
        printf("FAIL TO ALLOCATE MEMORY\n");

        return 0;
    }

    // Copy 12 in piData
    *MyInfo.piData = 12;

    printf("MyInfo.piData = %d\n",*MyInfo.piData);


    //Allocate memory for pointer to char
    MyInfo.pcName = malloc(sizeof(char) * 12);

    //check allocated memory
    if((MyInfo.pcName) == NULL)
    {
        free(MyInfo.piData);

        printf("FAIL TO ALLOCATE MEMORY\n");

        return 0;
    }

    //Copy data in pcName
    strncpy(MyInfo.pcName,"Aticleworld", (*MyInfo.piData));

    printf("MyInfo.pcName = %s\n",MyInfo.pcName);



    //Free allocated memory
    free(MyInfo.piData);
    free(MyInfo.pcName);

    return 0;
}

Output:

pointer as member

 

How does the above program work?

In the above program, MyInfo is a structure variable. Using the MyInfo we can access the members of the structure piData and pcName. As we know that we have to provide a valid memory to the pointer before assigning a value, so here I am using the malloc (memory management function) to allocate heap memory for the pointers.

After the allocation of the memory, I am copying the data in piData and pcName and displaying the copied data on the console using the printf.

 

2.  Using the pointer to the structure

Similar to the structure variable you can access the pointer member using the pointer to structure. But the difference is that when you are going to access using the pointer to structure you should assign memory to the pointer. See the below example code.

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

typedef struct
{
    int *piData;

    char *pcName;

} Info;


int main()
{

    //pointer to structure
    Info *ptrMyInfo = NULL;

    //Assign memory to the pointer
    ptrMyInfo = malloc(sizeof(Info));

    //check allocated memory
    if((ptrMyInfo) == NULL)
    {
        printf("FAIL TO ALLOCATE MEMORY\n");

        return 0;
    }


    //Allocate memory for integer
    ptrMyInfo->piData =  malloc(sizeof(int));

    //check allocated memory
    if((ptrMyInfo->piData) == NULL)
    {
        free(ptrMyInfo);
        printf("FAIL TO ALLOCATE MEMORY\n");

        return 0;
    }

    // Copy 12 in piData
    *ptrMyInfo->piData = 12;

    printf("ptrMyInfo.piData = %d\n",*ptrMyInfo->piData);


    //Allocate memory for pointer to char
    ptrMyInfo->pcName = malloc(sizeof(char) * 12);

    //check allocated memory
    if((ptrMyInfo->pcName) == NULL)
    {
        free(ptrMyInfo->piData);
        free(ptrMyInfo);

        printf("FAIL TO ALLOCATE MEMORY\n");

        return 0;
    }

    //Copy data in pcName
    strncpy(ptrMyInfo->pcName,"Aticleworld", (*ptrMyInfo->piData));

    printf("ptrMyInfo.pcName = %s\n",ptrMyInfo->pcName);



    //Free allocated memory
    free(ptrMyInfo->piData);
    free(ptrMyInfo->pcName);
    free(ptrMyInfo);

    return 0;
}

Output:

pointer as member

 

Recommended Posts for you