C Programming: Pointers and Arrays 🎯

beginner
25 min

C Programming: Pointers and Arrays 🎯

Welcome to the exciting world of C Programming! Today, we'll dive deep into understanding Pointers and Arrays, two essential concepts in C that will help you build robust and efficient programs. Let's get started!

Pointers 📝

Pointers in C are variables that hold the memory address of other variables. They allow us to manipulate memory directly, which is crucial for many advanced programming tasks.

Declaring Pointers ✅

To declare a pointer, we use the * symbol. For example, to declare an integer pointer, we would write:

c
int *ptr;

Assigning Values to Pointers ✅

To assign a value to a pointer, we use the & operator to get the memory address of a variable.

c
int num = 10; int *ptr = #

Accessing Values through Pointers ✅

To access the value stored at the memory address pointed by a pointer, we use the * operator.

c
int num = 10; int *ptr = # printf("%d", *ptr); // Output: 10

Arrays 📝

Arrays in C are a collection of variables of the same data type stored at contiguous memory locations.

Declaring Arrays ✅

To declare an array, we specify the data type followed by square brackets []. For example:

c
int arr[5];

Accessing Array Elements ✅

To access an array element, we use the index enclosed within square brackets. The first element in an array is at index 0.

c
int arr[5] = {1, 2, 3, 4, 5}; printf("%d", arr[2]); // Output: 3

Array as a Function Parameter ✅

Arrays can be passed as function parameters using pointers. This allows the function to manipulate the original array data.

c
void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } int main() { int arr[3] = {1, 2, 3}; swap(&arr[0], &arr[1]); printf("%d %d %d", arr[0], arr[1], arr[2]); // Output: 2 1 3 return 0; }

Quiz Time 💡

Quick Quiz
Question 1 of 1

What is the output of the following code?

Stay tuned for more exciting lessons on C Programming! If you have any questions or need further clarification on Pointers and Arrays, feel free to ask. Happy coding! 🚀