0% found this document useful (0 votes)
18 views51 pages

Understanding Pointers and Structures in C

The document provides an overview of pointers and structures in C programming, explaining what pointers are, how to declare and use them, and their advantages. It also discusses structures as user-defined data types that group different variable types, along with unions, which allow different data types to share the same memory location. Additionally, it introduces the typedef keyword for creating aliases for existing data types to improve code readability.

Uploaded by

mithiilesh2507
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)
18 views51 pages

Understanding Pointers and Structures in C

The document provides an overview of pointers and structures in C programming, explaining what pointers are, how to declare and use them, and their advantages. It also discusses structures as user-defined data types that group different variable types, along with unions, which allow different data types to share the same memory location. Additionally, it introduces the typedef keyword for creating aliases for existing data types to improve code readability.

Uploaded by

mithiilesh2507
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

Pointers

• What is a pointer?
• A pointer is a variable that stores the address of another variable.
• 👉 Normal variable → stores a value
👉 Pointer variable → stores an address
• int x = 10;
• int *p;
p = &x;
Printing values
• printf("%d\n", x); // 10
• printf("%d\n", *p); // 10
• printf("%p\n", p); // address of x
• Why * is used?
• int *p; → declares a pointer
• *p → dereferencing (getting the value stored at that address)
Declaration of pointers
• data_type *pointer_name;
• int *p;
• float *fp;
• char *cp;
• Declaration + Definition together
• int a = 10;
• int *p = &a;
• What is dereferencing?
• Dereferencing means accessing the value stored at the address held
by the pointer.
• *pointer_name
• int a = 10;
• int *p = &a;

• printf("%d", *p);
• Output 10
Pointer Variables
Accessing Variables through Pointers
• What does “accessing variables through pointers” mean?
• It means using a pointer to read or modify the value of another variable by using its memory address.
• #include <stdio.h>

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

• p = &a; // p stores address of a

• printf("Value of a using variable: %d\n", a);


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

• return 0;
• }
Add two floating point numbers
Null Pointer
Advantages of Pointers in C Programming

• Efficient Memory Access


• Pointers allow direct access to memory, which makes programs
faster.
• Call by reference
• Dynamic Memory Allocation
• Pointers are required to use malloc(), calloc(), free().
What is a Structure in C
• A structure in C is a user-defined data type that allows you to group
different types of variables under one name.
• It is mainly used when we want to represent real-world entities like a
student, employee, book, etc., which have multiple properties.
• Why do we need Structures?
• Arrays can store only same type of data
• Structures can store different types of data together
• Makes programs more organized and readable
• Very useful in real-life applications
• Example (Real-world idea)
• A Student has:
• Roll number → integer
• Name → string
• Marks → float
• These are different data types → Structure is perfect
• Syntax of Structure
• struct structure_name
•{
• data_type member1;
• data_type member2;
• ...
• };
• struct → keyword
• structure_name → name of the structure
• members → variables inside the structure
• Example Structure Declaration
• struct Student {
• int roll;
• char name[20];
• float marks;
• };
• Declaring Structure Variables
• struct Student s1, s2;
• OR
• at the time of structure definition:
• struct Student {
• int roll;
• char name[20];
• float marks;
• } s1, s2;
• Accessing Structure Members
• Use the dot operator (.)
• [Link] = 101;
• strcpy([Link], "Ravi");
• [Link] = 85.5;
Accessing Structure Operation on Structures
Complex Structure
• What are Complex Structures?
• Complex structures are structures that contain:
• Another structure (nested structure)
• Arrays
• Pointers
• Combination of all the above
Pointer to Structure
Unions
• Suitcase with One Compartment
• You have one suitcase:
• Today you keep clothes
• Tomorrow you keep books
• You cannot keep both at the same time.
• If you put books, clothes are gone.
Union
• What is a Union?
• A union is a user-defined data type in C that allows different data
types to share the same memory location.
• Why is Union Used?
• A union is used when multiple variables share the same memory location
and only one value is required at a time.
• The main purpose of a union is memory optimization.
• Reasons for Using Union
• Saves Memory
• All members share the same memory
• Memory allocated = size of largest member only
• Useful in memory-limited systems
• Improves Performance
• Less memory usage
• Less memory copying
• Faster access in some applications
Referencing Union
• What is Referencing a Union?
• Referencing a union means accessing the members of a union
variable
either:
• directly using the dot (.) operator, or
• indirectly using a pointer and arrow (->) operator.
• Referencing Union Members using Dot (.) Operator
• Used when we have a union variable.
• Syntax
• union_variable.member;
• Referencing Union Members using Pointer (->) Operator
• Used when accessing union members through a pointer to union.
• Syntax
• union_pointer->member;
• union Data {
• int i;
• float f;
• };

• union Data d;
• union Data *p = &d;

• p->i = 25; // referencing using pointer


Union Initialization
• Union initialization means assigning an initial value to a union variable at
the time of declaration.
• Syntax
• union UnionName variable = { value };
• union Data {
• int i;
• float f;
• };

• union Data d = {10};


• ✔ Initializes member i (first member) with value 10.
Type def
• What is typedef?
• typedef is a keyword in C used to create a new name (alias) for an
existing data type.
• It does not create a new data type,
• It only gives a new name for convenience and readability.
• Syntax
• typedef existing_datatype new_name;
• Syntax
• typedef existing_datatype new_name;
• Why typedef is Used?
• 1⃣ Improves Readability

You might also like