2. 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
3. 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.
7. 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 = # // Assign the address of num to ptr
int value = *ptr; // Access the value stored in num indirectly through the pointer
printf("Value of num: %dn", num);
printf("Value accessed through the pointer: %dn", value);
return 0;
}
8. 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.
9. Syntax:
• The syntax for defining a structure in C is as follows:
• struct structure_name
{ data_type member1;
data_type member2; ... };
10. Declaring Structure Variables:
• After defining a structure, you can declare variables of that structure
type.
• Syntax: struct structure_name variable_name
11. Accessing Structure Members:
• Structure members can be accessed using the dot (.) operator.
• Syntax: variable_name.member_name
12. 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: %sn", person1.name);
printf("Age: %dn", person1.age);
printf("Address: %sn", person1.address);
return 0;
}
13. 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.
14. Syntax:
• The syntax for declaring a pointer to a structure is as follows:
• struct structure_name *pointer_name;
15. 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;
16. 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: %sn", ptr->name);
printf("Age: %dn", ptr->age);
printf("Address: %sn", ptr->address);
return 0;
}