Thursday 6 April 2023

Pointers and structures in C

In C programming, pointers and structures are two important concepts that are often used together.

A pointer is a variable that stores the memory address of another variable. In other words, it points to the location in memory where the variable is stored. Pointers are often used to pass variables by reference, to dynamically allocate memory, and to implement complex data structures such as linked lists.

A structure is a composite data type that groups together variables of different data types under a single name. It is used to create user-defined data types that can represent complex objects with multiple properties. A structure can contain variables of any data type, including other structures.

We can use pointers with structures to manipulate data in memory and to access the variables within a structure. The arrow operator -> is used to access a member of a structure through a pointer to that structure. For example, suppose we have a structure named person that contains two members: name and age. We can define a pointer to this structure as follows:

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

struct person *ptr;
 

To access the members of the structure through the pointer, we can use the arrow operator as follows:

ptr = malloc(sizeof(struct person)); // allocate memory for the structure
strcpy(ptr->name, "John"); // assign a value to the name member
ptr->age = 30; // assign a value to the age member
 

In the example above, we allocate memory for the structure using the malloc() function, which returns a pointer to the allocated memory. We then use the arrow operator to access the members of the structure through the pointer and assign values to them.

Pointers and structures are also used extensively in creating and manipulating dynamic data structures like linked lists, trees, and graphs.

Example:

here's an example program that demonstrates how pointers and structures can be used together:

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

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

int main() {
    struct person *ptr;
    ptr = malloc(sizeof(struct person)); // allocate memory for the structure

    if (ptr == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }

    strcpy(ptr->name, "John"); // assign a value to the name member
    ptr->age = 30; // assign a value to the age member

    printf("Name: %s\n", ptr->name); // access the name member using the arrow operator
    printf("Age: %d\n", ptr->age); // access the age member using the arrow operator

    free(ptr); // free the memory allocated for the structure

    return 0;
}

 

In this program, we define a structure named person with two members: name and age. We then define a pointer to this structure named ptr and allocate memory for it using the malloc() function. We check if the memory allocation was successful, and if not, we print an error message and exit the program.

Next, we use the strcpy() function to assign a value to the name member of the structure, and the assignment operator to assign a value to the age member. We then use the arrow operator -> to access the members of the structure through the pointer and print their values to the console.

Finally, we free the memory allocated for the structure using the free() function.

When we run this program, we should see the following output:

Name: John
Age: 30
 

 

0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top