Understanding pointers in c


Introduction to pointers in c



Pointer is a variable just like other variables of c but only difference is unlike the other variable it stores the memory address of any other variables of c. This variable may be type of int, char, array, structure, function or any other pointers. For examples:

(1)
Pointer p which is storing memory address of a int type variable:

int i=50;
int *p=&i;

(2)
Pointer p which is storing memory address of an array:

int arr[20];
int (*p)[20]=&arr;

(3)
Pointer p which is storing memory address of a function:

char display(void);
char(*p)(void)=&display;

(4)
Pointer p which is storing memory address of struct type variable:

struct abc{
int a;
float b;
}var;
struct abc *p=&var;

10 comments:

maddy said...

int (*p)[20]=&arr;


can u explain what this line does

jithu said...

when we point the structure means will pointer occupy 2 bytes or more than that a?

manu said...

it depends on memory management in system because in 16 bit system pointer points to 16 bit address and takes 2bytes, even if it is for struct or anithing else

Mean-2 said...

what is the benefit of pointer in c programming.....

ajeet pal said...

good knowledge of c pointer
ajeet

Ajit Gupta said...

Its tough to get Pointers understanding.

Manish Kumar said...

i think the line *p= &i;

is incorrect its invalid conversion from int* to int

as per the compiation in Dev C++ 4.9.9.2

Vannara Houth said...

int(*p)[20]=&arr;

it is basically create a variable p and let p has the address of arr first element at the same time p has space of 4 bytes to keep the address.

Sathish Kumar said...

*p=&i;
invalid conversion..please check

Sweta Banerjee said...

int *p=&i
line is absolutely correct.
Checked in minGW as well as DevC++