0% found this document useful (0 votes)
13 views8 pages

Module 5

Module 5 covers structures and pointers in C programming. It explains how to define, declare, and access structure variables, initialize them, and work with arrays of structures. Additionally, it introduces pointers, their declaration, initialization, and how to access values using pointers.

Uploaded by

prajjugowda9769
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views8 pages

Module 5

Module 5 covers structures and pointers in C programming. It explains how to define, declare, and access structure variables, initialize them, and work with arrays of structures. Additionally, it introduces pointers, their declaration, initialization, and how to access values using pointers.

Uploaded by

prajjugowda9769
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Module 5

Structures and Pointers: Introduction, Defining a Structure, Declaring and Accessing


Structure Variables and Members, Structure Initialization, Copying and Comparing Structure
Variables, Array of Structures, Arrays within Structures.

Pointers: Introduction, Understanding Pointers, Accessing the Address of Variable, Declaring


pointer variables, initialization of pointers, accessing variables through its pointer.

Introduction to Structures

Definition:
A structure is a user-defined data type in C that allows us to group different types of
variables under a single name.

Why we use structures:

 To store related data together


 To represent real-world entities (student, employee, book, etc.)

Example:

A student has:

 Roll number (int)


 Name (char array)
 Marks (float)

Defining a Structure:
Defining a structure in C means creating a user-defined data type that groups variables of
different data types under one name. It specifies the structure name and its members but does
not allocate memory. Memory is allocated only when structure variables are declared.

Syntax:

struct Student
{
int roll;
char name[20];
float marks;
};
Declaring and Accessing Structure Variables and Members.

Declaring Structure Variable:


Definition:
Declaring a structure variable means creating a variable of structure type.

Example:

struct Student s1;

Or declaration at definition:

struct Student
{
int roll;
char name[20];
float marks;
}

Accessing Structure Members:


Definition:
Structure members are accessed using the dot (.) operator.

Example:

#include <stdio.h>

struct Student
{
int roll;
float marks;
};

int main()
{
struct Student s1;
[Link] = 1;
[Link] = 85.5;

printf("Roll = %d\n", [Link]);


printf("Marks = %.2f\n", [Link]);
return 0;
}

Structure Initialization
Definition:
Assigning values to structure members at the time of declaration is called structure
initialization.

Example:

struct Student s1 = {1, "Ravi", 90.5};

Full Program:

#include <stdio.h>

struct Student
{
int roll;
char name[20];
float marks;
};

int main()
{
struct Student s1 = {1, "Ravi", 90.5};

printf("%d %s %.2f", [Link], [Link], [Link]);


return 0;
}

Copying Structure Variables


Definition:
One structure variable can be assigned to another of the same type using the assignment
operator (=).

Example:

struct Student s1 = {1, "Ravi", 90.5};

struct Student s2;


s2 = s1; // Copying structure

✔ All member values of s1 are copied to s2.

Comparing Structure Variables


Definition:
Structures cannot be compared directly using ==.
Each member must be compared individually.

Example:

if ([Link] == [Link] && [Link] == [Link])


{
printf("Structures are equal");
}

Array of Structures
Definition:
An array of structures stores multiple structure variables of the same type.

Example:

#include <stdio.h>

struct Student
{
int roll;
float marks;
};

int main()
{
struct Student s[3];
int i;

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


{
printf("Enter roll and marks: ");
scanf("%d %f", &s[i].roll, &s[i].marks);
}
for(i = 0; i < 3; i++)
{
printf("Roll: %d Marks: %.2f\n", s[i].roll, s[i].marks);
}

return 0;
}

Arrays within Structures


Definition:
A structure can contain an array as its member.

Example:

#include <stdio.h>

struct Student
{
int roll;
char name[20]; // array inside structure
};

int main()
{
struct Student s1 = {1, "Anita"};

printf("Roll: %d\n", [Link]);


printf("Name: %s\n", [Link]);

return 0;
}
POINTERS IN C

Introduction to Pointers
Definition:
A pointer is a variable that stores the address of another variable.

Example:

int a = 10;
int *p = &a;

Accessing Address of Variable


Definition:
The address-of operator (&) is used to get the address of a variable.

Example:

printf("%p", &a);
Declaring Pointer Variables
Syntax:

data_type *pointer_name;

Example:

int *p;

Initialization of Pointers
Definition:
Assigning an address to a pointer variable.

Example:

int a = 10;
int *p = &a;

Accessing Value Using Pointer


Definition:
The dereference operator (*) is used to access the value stored at the address.

Example:

#include <stdio.h>

int main()
{
int a = 10;
int *p = &a;

printf("Value of a = %d\n", a);


printf("Address of a = %p\n", p);
printf("Value using pointer = %d\n", *p);

return 0;
}

You might also like