(Created page with "Pointer as the word implies just points. It is a datatype in C which is used to hold memory address. Pointers may point to any datatype in C and based on that pointer arithmet...")
(No difference)

Revision as of 13:52, 15 June 2014

Pointer as the word implies just points. It is a datatype in C which is used to hold memory address. Pointers may point to any datatype in C and based on that pointer arithmetic is done.

A pointer is declared using * as follows:

int* p; //p is a pointer to int. int *p is also syntactically correct but * here is always taken with int as int* and p is the name for the variable. Many take this as *p which is not correct
char* y; //y is a pointer to char

Actually pointer operation is very simple as there are just two operators for using pointer.

  • p -> gives the content of the location in p.

What is the content depends on the type of p. If p is an integer pointer, *p will return 4 bytes from the location contained in p. If p is a char pointer *p will return 1 byte from the location in p.

int *p, a;
scanf("%d",&a); //memory address of a is passed to scanf and it stores the keyboard entered value in that location
p = &a; //Memory address of a is stored in p
printf("*p = %d", *p); //Content of p is printed by printf. Since %d is used, 4 bytes (assuming sizeof int is 4) from the memory location given by p is converted to integer and printed.
int *p, a;
p = &a;
scanf("%d",p);
printf("a = %d *p = %d", a, *p);

Pointer as the word implies just points. It is a datatype in C which is used to hold memory address. Pointers may point to any datatype in C and based on that pointer arithmetic is done.

A pointer is declared using * as follows:

int* p; //p is a pointer to int. int *p is also syntactically correct but * here is always taken with int as int* and p is the name for the variable. Many take this as *p which is not correct
char* y; //y is a pointer to char

Actually pointer operation is very simple as there are just two operators for using pointer.

  • p -> gives the content of the location in p.

What is the content depends on the type of p. If p is an integer pointer, *p will return 4 bytes from the location contained in p. If p is a char pointer *p will return 1 byte from the location in p.

int *p, a;
scanf("%d",&a); //memory address of a is passed to scanf and it stores the keyboard entered value in that location
p = &a; //Memory address of a is stored in p
printf("*p = %d", *p); //Content of p is printed by printf. Since %d is used, 4 bytes (assuming sizeof int is 4) from the memory location given by p is converted to integer and printed.
int *p, a;
p = &a;
scanf("%d",p);
printf("a = %d *p = %d", a, *p);