Ticker

6/recent/ticker-posts

Pointers in C

 Chapter
15: Pointers in C



 




In this chapter, we will explore pointers in C.
Pointers are variables that store memory addresses, allowing you to directly
access and manipulate memory. We will cover understanding memory and addresses,
declaring and using pointers, pointer arithmetic, pointers and arrays, and
pointers and functions. Let's get started!



 



16.1
Understanding Memory and Addresses:



In C, every variable is stored in memory, and each
memory location has a unique address. Pointers store these memory addresses,
allowing you to access and manipulate the data stored in that memory location.



 



Example:





#include <stdio.h>



 



int main() {



   
int num = 42;



   
int* ptr;



 



   
ptr = &num;



 



   
printf("Value of num: %d\n", num);



   
printf("Address of num: %p\n", &num);



   
printf("Value of ptr: %p\n", ptr);



   
printf("Value at the address pointed by ptr: %d\n", *ptr);



 



   
return 0;



}





 



In this example, we declare an integer variable
`num` and a pointer variable `ptr` that can store the memory address of an
integer. We assign the address of `num` to `ptr` using the address-of operator
`&`. We then print the value of `num`, the address of `num`, the value of
`ptr`, and the value stored at the address pointed by `ptr` using the
dereference operator `*`.



 



16.2
Declaring and Using Pointers:



To declare a pointer variable, we use the `*` symbol
before the variable name. We can then assign a memory address to the pointer
using the address-of operator `&`.



 



Example:





#include <stdio.h>



 



int main() {



   
int num = 10;



   
int* ptr;



 



   
ptr = &num;



 



   
printf("Value of num: %d\n", num);



   
printf("Value of ptr: %p\n", ptr);



   
printf("Value at the address pointed by ptr: %d\n", *ptr);



 



   
return 0;



}





 



In this example, we declare an integer variable
`num` and a pointer variable `ptr`. We assign the address of `num` to `ptr`
using the address-of operator `&`. We then print the value of `num`, the
value of `ptr`, and the value stored at the address pointed by `ptr` using the
dereference operator `*`.



 



16.3
Pointer Arithmetic:



Pointer arithmetic allows you to perform arithmetic
operations on pointers. This can be useful when working with arrays or
dynamically allocating memory.



 



Example:





#include <stdio.h>



 



int main() {



   
int arr[] = {10, 20, 30, 40, 50};



   
int* ptr = arr;



 



   
printf("Array elements: ");



   
for (int i = 0; i < 5; i++) {



       
printf("%d ", *(ptr + i));



   
}



 



   
return 0;



}





 



In this example, we declare an integer array `arr`
and a pointer `ptr` that points to the first element of the array. We use
pointer arithmetic to access the elements of the array by adding an offset `i`
to the pointer `ptr` using the `+` operator. We then print the elements of the
array using the dereference operator `*`.



 



16.4
Pointers and Arrays:



Pointers and arrays are closely related in C. An
array name can be treated as a pointer to its first element. This allows you to
access array elements using pointer notation.



 



Example:





#include <stdio.h>



 



int main() {



   
int arr[] = {10, 20,



 



30, 40, 50};



   
int* ptr = arr;



 



   
printf("Array elements: ");



   
for (int i = 0; i < 5; i++) {



       
printf("%d ", *(ptr + i));



   
}



 



   
return 0;



}





 



In this example, we declare an integer array `arr`
and a pointer `ptr` that points to the first element of the array. We use
pointer arithmetic to access the elements of the array by adding an offset `i`
to the pointer `ptr` using the `+` operator. We then print the elements of the
array using the dereference operator `*`.



 



16.5
Pointers and Functions:



Pointers can be passed to functions, allowing the
function to directly access and modify the data at the memory address pointed
by the pointer.



 



Example:





#include <stdio.h>



 



void increment(int* numPtr) {



   
(*numPtr)++;



}



 



int main() {



   
int num = 10;



 



   
printf("Before increment: %d\n", num);



   
increment(&num);



   
printf("After increment: %d\n", num);



 



   
return 0;



}





 



In this example, we define a function `increment`
that takes a pointer to an integer as a parameter. Inside the function, we use
the dereference operator `*` to access the value pointed by `numPtr` and
increment it. In the `main` function, we declare an integer variable `num` and
pass its address to the `increment` function using the address-of operator
`&`. We then print the value of `num` before and after the increment.



 



That concludes our exploration of pointers in C.
Pointers allow you to directly manipulate memory, access array elements
efficiently, and pass data by reference to functions. Understanding pointers is
crucial for advanced C programming.



 



Keep practicing and experimenting with pointers to
solidify your understanding. In the next chapter, we will cover input and
output operations in C.

Post a Comment

0 Comments