Computer Programming C: UNIT III: Functions and Pointers

Pointers and Structures

C Programming

Pointer pointing to a structure is known as structure pointers.

POINTERS AND STRUCTURES

Pointer pointing to a structure is known as structure pointers

struct

{

   member 1;

   member2;

} variable, *ptr;


Here, the variable represents structure type variable and *ptr represents the name of the pointer.

Example 1: Program to declare pointer to structure and display the contents of the structure.

/* Program to print name, place and account number */

#include <stdio.h>

#include<conio.h>

void main()   * Local definitions */

{.

struct account

{

      char name[20];

      char place[25];

      int acctno;

};

static struct account_m4={"Venkat", "Arcot", 24201};

struct account *ptr;

/* Statements */

clrscr();

ptr=&m4;

printf("%s\t%s\t%d\n", m4.name, m4.place, m4.acctno);

printf("%s\t%s\t%d\n", ptr‒>name,ptr‒>place,ptr‒> acctno);

getch();

} /* main */

OUTPUT

Venkat Arcot 24201

Venkat Arcot 24201

EXPLANATION: In the above program, the function printf() statement prints the structure elements by calling them as usual. The second printf() statement prints the structure element using pointer and arrow operator is used instead of dot (.) operator.

Here, ptr is not a structure variable, dot operator cannot be used. C language provides an arrow operator '→' to refer the structure elements.

operator '→' is always a pointer to a structure.

Example 2: Program to print name, place and account number using structure and pointer.

/* Program using structures and pointers */

/* account is a structure name with character string and integer variable # */

include <stdio.h>

struct account

{

     char name[20];

     char place [25];

     int acctno;

};

main()

{

      /* Local definitions */

      static struct account m4={"madhavan", "Chennai", 241};

      display (&m4);  /* function call */

} /* main */

display(m) /* display() is a function */

struct account a; /* pointer to a structure*/

{

        prinf ("%s\n%s\n%d\n", ptr‒>name, ptr‒>place, ptr‒> acctno); }

} /* display */

OUTPUT

Madhavan

Chennai

241

EXPLANATION: In the above program, struct account is declared with members name, place of character type and acctno of integer type. In the main function, the structure account is declared as static type. The user defined function display() is called, then the control is transferred to user defined function display(). The display function access the structure account and print the name, place and acctno. Here the structure struct account is declared outside the function main() so that the data is available to the function display( ).

Naturally, a pointer also can be a member of a structure.

Syntax:

struct int_pointers

{

        int *p1;

        int *p2;

}

Description:

In this definition, a structure called into pointer is defined to contain two integer pointers p1 and p2.

 

Computer Programming C: UNIT III: Functions and Pointers : Tag: Computer Science : C Programming - Pointers and Structures


Computer Programming C: UNIT III: Functions and Pointers



Under Subject


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation



Related Subjects


English Essentials I

EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


தமிழர் மரபு - Heritage of Tamils

UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Calculus

MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I

PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Chemistry I

CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Makerspace

ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming Python

CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Fundamentals of Electrical and Electronics Engineering

EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Mechanical Engineering

ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Civil Engineering

CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing

CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I Laboratory

PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Applied Chemistry I Laboratory

CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming Python Laboratory

CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Engineering Drawing

ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electronics and Electrical Engineering

EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing - Laboratory

CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation