Sie sind auf Seite 1von 3

POINTERS IN C

Pointers are often considered to be the tough most chapter in C. Many found it hard to understand it.
But I believe if carefully understand things mentioned below you will say it is the easiest chapter in C.

Best of luck....................

Definition

A pointer is a variable which contains the address of a memory location of another variable, rather than
its stored value.

A pointer provides an indirect way of accessing the value of a data item.

Why pointers are used

1. to return more than one value from a function.


2. to pass arrays and strings more conveniently from one function to another.
3. to manipulate arrays more easily by moving pointers to them(or parts of them), instead of
moving the array themselves.
4. to allocate memory and access it(dynamic memory allocation).
5. to create complex data structures, such as linked lists, where one structure must contain
references to the other data structures.

Pointer variable declaration:

type *name;

Ex: int *a;

The pointer operators:

There are two pointer operators: * and &

The ‘&’ is a unary operator and it returns the memory address of its operands

Ex: n=&m;

it places into ‘n’ the address of the variable m.

The second pointer operator ‘*’ is the complement of ‘&’. It is a unary operator that returns the value
located at the address.

Ex: if ‘n’ contains the memory address of ‘m’, then a=*n places the value of ‘m’ into ‘a’
Try this program now

#include <stdio.h>
#include <conio.h>
void main()
{
int a = 10;
int *p;
p = &a;
printf("\nAddress of a: %u", &a);
printf("\n\nAddress of a: %u", p);
printf("\n\nAddress of p: %u", &p);
printf("\n\nValue of p: %d", p);
printf("\n\nValue of a: %d", a);
printf("\n\nValue of a: %d", *(&a));
printf("\n\nValue of a: %d", *p);
getch();
}

I think you have understood whatever I said till now.

Pointer Operations:

Addition & subtraction are the only operations which can be used with pointers. Two pointers can be
compared in a relational expression. However, this is possible only if both these variables are pointing to
variables of same type.

Pointers and Arrays:

There is a close relationship between pointers and arrays. Consider the following:

char str[80], *p1;

p1=str;

Here ‘p1’ has been set to the address of the first array element in ‘str’. To access fifth element in ‘str’
you could write-- str[4] or *(p1 + 4)

Thank you..............

I would be very happy if you send me feedback in pritam2chatterjee@gmail.com

Das könnte Ihnen auch gefallen