Pointers in C
Pointers are a fundamental concept in C programming that enable you to directly access and manipulate memory. Understanding pointers is crucial for effective and efficient C programming.
What is a Pointer?
A pointer is a value that represents a memory address. It points to a specific memory location, allowing you to access and modify the value stored at that location.
Basic Example
int some_var = 4;
int *pointer_to_some_var = &some_var;
here &some_var is address of some_var;
Symbol | Function | Example |
---|---|---|
some_variable | Holds the value in certain memory location | int x = 3; |
* | Points to memory address of a certain variable | int *pX; |
& | Holds the value of address of following variable | &x; |
references and De-referencing
Symbols | what it holds/means | Example code |
---|---|---|
Declared variable | Value of the variable | `int x = 10;` |
A pointer | pointer points to some address | int *p = &x; |
Address of the variable(&) | Memory location of variable(eg:0x7ffe2f14f97c ) | printf("Address of variable x is %p",(void*)&x); |
Pointers name itself with a preceding &(pointer variable) | Memory address of pointer (eg:0x7ffe2f14f97c) | printf("Address of pointer p is %p",(void*)&p); |
pointer preceded by * (not to be confused with initialization of a pointer type) | Value pointed to by pointer(also called dereferencing) | printf("Value pointed by pointer p is %d",*p); |
Why use Pointers?
Pointers help manage scope issues, especially when using functions with structures. By using pointers, you can access out-of-scope variables in functions through their memory addresses.
We use pointers to access out of scope variables in functions by providing pointer pointing to memory address of such variable (or structure).
Example
#include <stdio.h>
#include <stdbool.h>
struct employee_type
{
int id;
int income;
bool staff;
};
void initialize_employee(struct employee_type *e){
e->id = 0;
e->income=0;
e->staff=true;
return;
}
int main(){
struct employee_type Ralph;
initialize_employee(&Ralph);
printf("%d", Ralph.income);
}
Common Pitfalls
Uninitialized Pointers: Always initialize pointers. An uninitialized pointer points to a random memory location, leading to undefined behavior.
Dangling Pointers: Do not use pointers to memory that has been freed or gone out of scope.
Pointer Arithmetic: Be careful with pointer arithmetic to avoid accessing memory out of bounds.
Top comments (0)