C Books Guide and List
C++ Books Guide and List
Best Java Books

3,749 questions

4,049 answers


10,705 users

What is a pointer in C?

0 votes
asked Jan 8, 2014 by keem Expert (13,240 points)
edited Jan 10, 2014 by keem

1 Answer

0 votes
Best answer
A pointer is a special variable in C language meant just to store address of any other variable or function. Pointer variables unlike ordinary variables cannot be operated with all the arithmetic operations such as '*','%' operators.It follows a special arithmetic called as pointer arithmetic.

A pointer is declared as:

int *ap;

int a = 5;

In the above two statements an integer a was declared and initialized to 5. A pointer to an integer with name ap was declared. Next before ap is used ap=&a; This operation would initialize the declared pointer to int. The pointer ap is now said to point to a.

Operations on a pointer:

Dereferencing operator ' * ': This operator gives the value at the address pointed by the pointer . For example after the above C statements if we give printf("%d",*ap); Actual value of a that is 5 would be printed. That is because ap points to a. Addition operator ' + ': Pointer arithmetic is different from ordinary arithmetic. ap=ap+1;

Above expression would not increment the value of ap by one, but would increment it by the number of bytes of the data type it is pointing to. Here ap is pointing to an integer variable hence ap is incremented by 2 or 4 bytes depending upon the compiler.
answered Jan 8, 2014 by rajesh Guru (39,140 points)
selected Jan 10, 2014 by keem

Related questions

0 votes
1 answer 33 views
0 votes
1 answer 34 views
34 views asked Jan 8, 2014 by sara Guru (24,570 points)
0 votes
1 answer 92 views
0 votes
1 answer 17 views
17 views asked Dec 28, 2013 by smith Geek (15,340 points)
0 votes
1 answer 11 views
11 views asked Dec 27, 2013 by john Guru (27,140 points)