Introduction: Pointers are a fundamental concept in the C programming language . They provide a way to manipulate memory addresses and access data indirectly . Understanding pointers is crucial for efficient memory management and working with complex data structures in C
What is a Pointer? A pointer is a variable that stores the memory address of another variable . It allows us to indirectly access and modify the value stored at a specific memory location .
Declaring Pointers: Pointers are declared using the asterisk (*) symbol . Syntax : data_type * pointer_name ; Example: int * ptr ;
Initializing Pointers: Pointers can be initialized with the address of another variable . Example : int * ptr = #
What is a Pointer?
Initializing Pointers: #include < stdio.h > int main() { int num ; // Declare an integer variable int * ptr ; // Declare a pointer variable num = 10; // Initialize the value of num ptr = & num ; // Assign the address of num to ptr int value = * ptr ; // Access the value stored in num indirectly through the pointer printf ("Value of num : %d\n", num ); printf ("Value accessed through the pointer: %d\n", value); return 0; }
Introduction to Structures in C Structures are user-defined data types in C . They allow you to group variables of different types under a single name . Structures provide a convenient way to represent related data.
Syntax: The syntax for defining a structure in C is as follows: struct structure_name { data_type member1; data_type member2; ... };
Declaring Structure Variables: After defining a structure, you can declare variables of that structure type . Syntax: struct structure_name variable_name
Accessing Structure Members: Structure members can be accessed using the dot (.) operator . Syntax : variable_name.member_name
Example struct Person { char name[50]; int age; char address[100]; }; int main() { struct Person person1 = {"John Doe", 25, "123 Main Street"}; // Declare and initialize a structure variable // Access and print structure members printf ("Name: %s\n", person1.name); printf ("Age: %d\n", person1.age); printf ("Address: %s\n", person1.address); return 0; }
Pointers to Structures in C Pointers in C are variables that store memory addresses . Pointers to structures allow you to access and manipulate structure data indirectly . They provide an efficient way to work with complex data structures.
Syntax: The syntax for declaring a pointer to a structure is as follows : struct structure_name * pointer_name ;
Accessing Structure Members using Pointers: Accessing Structure Members using Pointers : Pointers to structures are used to access structure members using the arrow (->) operator . Syntax: pointer_name -> member_name ;
struct Person { char name[50]; int age; char address[100]; }; int main() { struct Person person1; struct Person * ptr ; // Declare a pointer to a structure ptr = &person1; // Assign the address of person1 to the pointer // Access and modify structure members using the pointer ptr ->age = 25; // Access and modify structure members using the pointer ptr ->age = 25; strcpy (person1.name, "John Doe"); person1.age = 25; strcpy (person1.address, "123 Main Street"); // Access and print structure members using the pointer printf ("Name: %s\n", ptr ->name); printf ("Age: %d\n", ptr ->age); printf ("Address: %s\n", ptr ->address); return 0; }