Skip to main content

Structure and 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;
}
Here, the pointer variable ptr of type struct name is created.
Accessing structure's member through pointer
A structure's member can be accessed 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 Age: ");
scanf(“%d”,&personPtr->age); or scanf("%d",&(*personPtr).age);
printf("Enter Weight: ");
scanf("%f",&personPtr->weight);
printf("Displaying: ");
printf("%d%f",personPtr->age,personPtr->weight);
//can also use (*personPtr).age
return 0;
}
2. Accessing structure member through pointer using dynamic memory allocation
To access structure member using pointers, memory can be allocated dynamically using malloc() function defined under "stdlib.h" header file.
Syntax to use malloc()
ptr = (cast-type*) malloc(byte-size)
Example 
 structure's member access 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 ptr 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;
}
Sample Programs
Create a structure for employee with following info: empid,name and salary. write a program to read the details of 'n' employees and display the details of employees whose salary is above 10000.Use pointer to structure. ( university question)

#include <stdio.h>
#include <stdlib.h>
struct Employee{
int eid;
char name[50];
float salary;
};
int main()
{
struct Employee *ptr;
int i, num;
printf("Enter number of persons: ");
scanf("%d", &num);
ptr = (struct Employee*) malloc(num * sizeof(struct Employee));
for(i = 0; i < num; ++i)
{
printf("Enter name, empid and salary:\n");
scanf("%s%d%f", (ptr+i)->name, &(ptr+i)->eid, &(ptr+i)->salary);
}
printf("Displaying Employees with salary above 10000:\n");
for(i = 0; i < num; ++i)
if((ptr+i)->salary >10000)
printf("%s\t%d\t%.2f\n", (ptr+i)->name, (ptr+i)->eid, (ptr+i)->salary);
return 0;
}

Programs to try

1.Create a structure for employee with following info: empid,name and salary. Write a program to read the details of 'n' employees and display the details of employees whose salary is above 10000.Use pointer to structure. ( university question)

Comments

Popular posts from this blog

KTU Mandatory C programs for Laboratory and Solutions

LIST OF LAB EXPERIMENTS 1. Familiarization of Hardware Components of a Computer 2. Familiarization of Linux environment – Programming in C with Linux 3. Familiarization of console I/O and operators in C     i) Display “Hello World”     ii) Read two numbers, add them and display their sum     iii) Read the radius of a circle, calculate its area and display it 4. Evaluate the arithmetic expression ((a -b / c * d + e) * (f +g))   and display its solution. Read the values of the variables from the user through console 5. Read 3 integer values, find the largest among them. 6. Read a Natural Number and check whether the number is prime or not 7. Read a Natural Number and check whether the number is Armstrong or not 8. Read n integers, store them in an array and find their sum and average 9. Read n integers, store them in an array and search for an element in the    array using an algorithm for Linear Search 10.Read n integers, store them in an array and sort the elements in t

PROGRAMMING IN C KTU EST 102 THEORY AND LAB NOTES

PROGRAMMING IN C  KTU  EST 102  THEORY AND LAB   COMMON FOR ALL BRANCHES About Me Syllabus Theory Syllabus Lab Model Question Paper University Question Papers and evaluation scheme Introduction( Lab) Introduction to C programming Linux History and GNU How to create a bootable ubuntu USB stick Installing  Linux Install Linux within  Windows Virtual Box and WSL Linux Basic Features and Architecture Basic Linux Commands Beginning C Programming Compiling C programs using gcc in Linux Debugging C program using gdb Module 1: Basics of computer hardware and software          Module-1 Reading Material Basics of Computer Architecture Hardware and Software System Software and Application Software  Programming Languages ( High level, Low level and Machine Language) and Translators ( Compiler, Interpreter, Assembler) Algorithm, Flowcharts and Pseudo code Program Development Structured Programming Basics of hardware ( video) Know about Motherboard(video) Know Universal Serial Bus ( USB) - video Lea

Input Output-printf() and scanf()

printf() and scanf() function in C   printf() and scanf() functions are inbuilt library functions in C programming language which are available in C library by default.These functions are declared and related macros are defined in “stdio.h” which is a header file in C language. We have to include “ stdio.h ” file as shown in below to make use of these printf() and scanf() library functions in C program. #include <stdio.h>   printf() function in C   In C programming language, printf() function is used to print the “character, string, float, integer, octal and hexadecimal values” onto the output screen. Here’s a quick summary of the available printf format specifiers:   %c           character %d           decimal (integer) number (base 10) %ld         Long integer %e           exponential floating-point number %f           floating-point number %lf          double number %i            integer (base 10) %o         octal number (base 8) %s         a string of characters %u