C

Pointers

Pointers are variables that store memory addresses. They are one of the most powerful features in C, allowing direct memory manipulation and efficient data handling.


int num = 42;
int *ptr = #  // ptr stores the address of num

printf("Value: %d\n", *ptr);    // Prints: 42
printf("Address: %p\n", ptr);   // Prints memory address
						

Memory Management

Understanding how to manage memory is crucial in C. Here's how to allocate and free memory dynamically.


							// Allocate memory for an integer
							int *ptr = (int*)malloc(sizeof(int));

							// Use the memory
							*ptr = 42;

							// Free the memory when done
							free(ptr);
							ptr = NULL;  // Good practice to avoid dangling pointers

Structures

Structures allow you to group related data together into a single unit.

struct Person {
    char name[50];
    int age;
    float height;
};

struct Person person1 = {"John", 25, 1.75};
printf("%s is %d years old\n", person1.name, person1.age);