Developer community
0 votes
73 views
asked by Expert (13.2k points) 27 41 59
edited by | 73 views

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 by Guru (39.1k points) 10 22 34
selected by

Related questions

0 votes
1 answer
asked by sara Guru (24.6k points) 29 51 79 | 42 views
0 votes
1 answer
0 votes
1 answer
asked by smith Geek (15.3k points) 17 41 61 | 21 views
0 votes
1 answer
asked by john Guru (27.1k points) 33 50 98 | 15 views
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

Categories


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

3.8k questions

4k answers

117 comments

10.7k users

3,751 questions
4,046 answers
117 comments
10,709 users