is used to get the value from pointer (deferencing)
int x=3;int*pX =&x;// *integer pointer named pX is set to the address of x*int y =*pX;// * is used alone ( no type beside it )// * is now a dereference , so it goes to the pointer address and gets the value// *integer named y is set to the thing pointed to by pX*
Pointer is just a address
How I visualize pointers
Declaration: To declare a pointer variable, use the asterisk (*) symbol before the variable name. For example:
int*ptr;// declare a pointer to an integer variable
Assignment: To assign a memory address to a pointer variable, use the address-of operator (&) with the variable whose address you want to store. For example:
Dereferencing: To access the value of the variable pointed to by a pointer, use the asterisk (*) symbol before the pointer variable name. For example:
Pointer Arithmetic: You can perform arithmetic operations on pointers such as addition, subtraction, etc. For example:
Null Pointer: A null pointer is a pointer that does not point to any memory location. To initialize a pointer as a null pointer, use the constant value "NULL" or "0". For example:
Pointer to Pointer: A pointer to a pointer is a pointer variable that holds the address of another pointer variable. For example:
Passing Pointers to Functions: Pointers can be passed to functions as arguments. When a pointer is passed to a function, the function can modify the value of the pointed-to variable. For example:
Advantages of using pointers in C:
Dynamic memory allocation
Accessing array elements
Passing parameters to functions
Returning multiple values from functions
Implementing complex data structures
Interacting with hardware
Call by value
Variable passed into arguement is copied
Function does not affect the original variable
alt text
Call by reference
Address to variable is passed into arguement
Function affect original variable
alt text
alt text
Pointer to array
Pointer to function
Callbacks
Callback is a function passed as an arguement into another function
which can be executed later
callbacks can make code more modular, flexible, and maintainable.
int num = 5;
int *ptr = # // assign the address of num to ptr
int num = 5;
int *ptr = #
printf("%d", *ptr); // dereference ptr to get the value of num
int arr[] = {1, 2, 3, 4};
int *ptr = &arr[0];
ptr++; // points to arr[1]
int *ptr = NULL; // initialize ptr as a null pointer
int num = 5;
int *ptr = #
int **pptr = &ptr; // pointer to pointer
void change_num(int *ptr) {
*ptr = 10;
}
int main() {
int num = 5;
int *ptr = #
change_num(ptr); // changes the value of num to 10
return 0;
}
char c1[] = "Hello";
char *c2;
c2 = c1;
//c2 is now a pointer that points to the first char of the c1 array
// = points to address of char 'H'
*return_type (*pointer_name)(arguments); //syntax*
int add(int a, int b) {
return a + b;
}
int (*ptr)(int, int) = &add; //create function pointer and point to addFunction
int result = (*ptr)(2, 3); // calling the function using the pointer by dereferencing it
*void* malloc(size_t size); //syntax*
int* ptr = (int*) malloc(10 * sizeof(int));// i don't think need to type cast