*
and &
)int a=5; //declare that a=5
int *p; // declare a pointer, p is the pointer (store the address)
p=&a // &+variable represent the address of that variable, so p equal to the address of the variable
printf("the value of a %d\n",*p) // *p will first use the address store in the p, to find the value. *p ---> (p store the address of a) ---> find the value in that address ---> 5
p
is pointer, storing the address of a*p
can return the corresponding value of that address —> a=5p=&a
assign the address of a to the pointer p, so p contain address of aint *p
declare the pointer& meaning the memory address of the operator / variables, if a function take a parameter as a pointer and i passing the &a
to the function, it means pass by reference. it will affect the variable even outside the function.
#include <stdio.h>
int add (int *p){
*p=10;
}
int main(){
int a=5;
printf("The value of a %d\n",a);
add(&a);
printf("The value of a %d\n",a);
return 0;
}
void *
void *
is generic pointer that can be used to represent the pointer in any data type –> untyped.
int a=5;
char b='d';
// usage 1, Pointer to any type
void *pr; // declare a pointer with untyped
pr=&a; // pr store the address of a
pr=&b;
// usage 2 Casting
void *pr=&a;
int *c=(int *)pr // convert the untyped pointer to int pointer type and then assign to new integer pointer
// (int *) emphasize that it should convert to int pointer type.
tech — Feb 22, 2024
Made with ❤ and at Earth.