0% found this document useful (0 votes)
150 views

C Programming Structure and Pointer

Structures can be accessed using pointers. A pointer to a structure can be declared by defining a pointer variable of the structure type. There are two ways to access structure members through pointers: 1) Referencing the pointer to the address of an existing structure variable to access its members. 2) Dynamically allocating memory for a structure using malloc and accessing members through the pointer. Examples are provided demonstrating how to define and access structure members using pointers with both approaches.

Uploaded by

adegeia
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views

C Programming Structure and Pointer

Structures can be accessed using pointers. A pointer to a structure can be declared by defining a pointer variable of the structure type. There are two ways to access structure members through pointers: 1) Referencing the pointer to the address of an existing structure variable to access its members. 2) Dynamically allocating memory for a structure using malloc and accessing members through the pointer. Examples are provided demonstrating how to define and access structure members using pointers with both approaches.

Uploaded by

adegeia
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

C Programming Structure and Pointer

www.programiz.com/c-programming/c-structures-pointers

Structures can be created and accessed using pointers. A pointer variable of a structure can be created as below:

struct name {
member1;
member2;
.
.
};

int main()
{
struct name *ptr;
}

Accessing structure's member through pointer


A structure's member can be accesssed through pointer in two ways:

1. Referencing pointer to another address to access memory


2. Using dynamic memory allocation

1. Referencing pointer to another address to access the memory


Consider an example to access structure's member through pointer.

#include <stdio.h>
typedef struct person
{
int age;
float weight;
};

int main()
{
struct person *personPtr, person1;
personPtr = &person1; // Referencing pointer to memory address of person1

printf("Enter integer: ");


scanf("%d",&(*personPtr).age);

printf("Enter number: ");


scanf("%f",&(*personPtr).weight);

printf("Displaying: ");
printf("%d%f",(*personPtr).age,(*personPtr).weight);

return 0;
}

In this example, the pointer variable of type struct person is referenced to the address of person1. Then, only
the structure member through pointer can can accessed.

Using -> operator to access structure pointer member

Structure pointer member can also be accessed using -> operator.

(*personPtr).age is same as personPtr->age


(*personPtr).weight is same as personPtr->weight

1/2
2. Accessing structure member through pointer using dynamic memory
allocation
To access structure member using pointers, memory can be allocated dynamically usingmalloc() function defined
under "stdlib.h" header file.

Syntax to use malloc()


ptr = (cast-type*) malloc(byte-size)

Example to use structure's member through pointer using malloc() function.

#include <stdio.h>
#include <stdlib.h>
struct person {
int age;
float weight;
char name[30];
};

int main()
{
struct person *ptr;
int i, num;

printf("Enter number of persons: ");


scanf("%d", &num);

ptr = (struct person*) malloc(num * sizeof(struct person));


// Above statement allocates the memory for n structures with pointer personPtr pointing to base address */

for(i = 0; i < num; ++i)


{
printf("Enter name, age and weight of the person respectively:\n");
scanf("%s%d%f", &(ptr+i)->name, &(ptr+i)->age, &(ptr+i)->weight);
}

printf("Displaying Infromation:\n");
for(i = 0; i < num; ++i)
printf("%s\t%d\t%.2f\n", (ptr+i)->name, (ptr+i)->age, (ptr+i)->weight);

return 0;
}

Output

Enter number of persons: 2


Enter name, age and weight of the person respectively:
Adam
2
3.2
Enter name, age and weight of the person respectively:
Eve
6
2.3
Displaying Information:
Adam 2 3.20
Eve 6 2.30

2/2

You might also like