pointers

15
POINTERS DEF :- A pointer is a data object that refers to a memory location which is an address. It resides I the memory location which is represented by the pointer variable. Thus, a pointer variable may contain address of another variable or any valid address in the computer memory. Every pointer variable has allocated bytes of memory. DECLARATION :- Format:- datatype *var1,*var2…..,*var n; Where datatype refers to the datatype of the value stored in the address given by the pointer variable and var1,var2,….,var n are the pointer variables. Ex:- 1) main() { int *a,b; b=5; a=&b; printf(“%d”,b); printf(“%d”,*b); printf(“%d”,&b); } 2) int *ap; float *xp; char *c1p,*c2p; here ap is a pointer variable of integer type which points to address of an interger value. xp is a pointer variable of float type, which points to address of an float value. c1p,c2p is a pointe variable of char type, which points to address of an char value. Pointers operators :- There are 2 unary operators that are exclusively used in connection with pointers. They are

Upload: gceramesh

Post on 18-Dec-2015

5 views

Category:

Documents


0 download

DESCRIPTION

this note is used for c learners

TRANSCRIPT

POINTERS

DEF :-

A pointer is a data object that refers to a memory location which is an address. It resides I the memory location which is represented by the pointer variable. Thus, a pointer variable may contain address of another variable or any valid address in the computer memory.

Every pointer variable has allocated bytes of memory.

DECLARATION:-

Format:- datatype *var1,*var2..,*var n;

Where datatype refers to the datatype of the value stored in the address given by the pointer variable and var1,var2,.,var n are the pointer variables.

Ex:-

1)

main()

{

int *a,b;

b=5;

a=&b;

printf(%d,b);

printf(%d,*b);

printf(%d,&b);

}2)

int *ap;

float *xp;

char *c1p,*c2p;

here ap is a pointer variable of integer type which points to address of an interger value.

xp is a pointer variable of float type, which points to address of an float value.

c1p,c2p is a pointe variable of char type, which points to address of an char value.

Pointers operators:-

There are 2 unary operators that are exclusively used in connection with pointers. They are

1) address of operator (&)

2) indirection or dereferencing operator (*)

The address of operator (&) returns the address of a variab;e. The indirection operator (*) returns the value at that address.

Invalid operations:-

1) &10 //constant does not have any address

2) &(a+b) //an expression does not have any address

3) register int r;

&r;

Indirection:-

The indirection operator * is used to access the pointed by its operand. The operand must be a pointer variable or pointer expression.

Ex:- int n,*f;

n=15;

f=#

valid expressions using asterisk:- n p

1) *f; // 15

2) *(&n); // 15

3) *&*&n; // 15

4) **&p; // 15

Invalid:-

1) *1024; //constant cannot be used as an operand of asterist

2) *n; //only pointer variable can be used as an operand

3) (*&)num; //invalid parenthesis

1) Program to find the sum of two integer values using pointer

main()

{

int *p,*q,a,b;

printf(enter a,b values);

scanf(%d %d,&a,&b);

p=&a;

q=&b;

printf(sum is :%d,*p+*q);

}

main()

{

int a,b,sum;

int *p=&a,*q=&b;

printf(enter a,b values);

scanf(%d %d,p,q);

printf(sum is :%d,*p+*q);

}

Dynamic memory allocation with respect to pointer:-

In this dynamic memory allocation with respect to pointers the memory was allocated and destroyed using 4 library functions.

1) malloc()

2) calloc()

3) realloc()

4) free()

1) malloc ():-

The function is used for memory allocation at the time of execution. This function takes only one argument and that argument represents no. of bytes to be allocated.

The malloc() function returns the base address of the memory which it is allocated and return type of address is of type void. Thats why we have to typecast that void type to any other primitive data type depending on the pointer variable.

The memory which allocated by malloc() function by defaultly takes the large value.

Format:- (void*)malloc(no. of bytes);

Ex:- float *p;

P=(float *)malloc(sizeof(float));

Here p allocates 2 bytes, malloc() returns 4 bytes base address of 4 bytes of void type and it is typecasted to float type.

4 bytes

1024Ex:- int *p;

P=(int*)malloc(5*(sizeof(int));

1024 1026 1028 1030 1032

Calloc():-

Format:- (void*)calloc(no.of objects,bytes of each datatype);

Ex:- int *p;

P=(int*)calloc(10,sizeof(int));

1024 1026

realloc():-

This function is capable of increasing or decreasing the space that has been allocated previously. Format:- (void*)realloc(pointer variable,no of bytes);

The first argument of realloc() is a pointer to a block of memory of which the size is to be alter.

The second argument specifies the new size.

If the allocation is successful the returned value is again the pointer variable to the first byte of the allocated memory retaining the old contents.

Ex:- int *p;

P=(int*)malloc(10*sizeof(int));

P=(int*)realloc(p,10);

free():-

the memory obtained by malloc() , calloc() and realloc() functions can be freed when that space is not required for any storage. The function free() dois this job.

Ex: free(pointer variable);

Program to find sum of two variables using dynamic memory allocationmain()

{

int *p,*q;

p=(int*)malloc(sizeof(int));

q=(int*)malloc(sizeof(int));

printf(enter a,b values);

scanf(%d %d,p,q);

printf(sum is :%d,*p+*q);

free(p);

}

Call by value:-

void swap(int , int );

main()

{

int *p,*q;

p=(int*)malloc(sizeof(int));

q=(int*)malloc(sizeof(int));

printf(enter a,b values);

scanf(%d %d,p,q); //5 10printf(\n Before swapping: a= %d \t b= %d,a,b); // a=5 b=10swap(a,b);

printf(\n After swapping: a= %d \t b= %d,a,b); // a=5 b=10free(p); free(q);

}

void swap(int x, int y)

{

int t=x;

x=y;

y=t;

}

Call by reference:-void swap(int *, int *);

main()

{

int *p,*q;

p=(int*)malloc(sizeof(int));

q=(int*)malloc(sizeof(int));

printf(enter a,b values);

scanf(%d %d,p,q); //5 10

printf(\n Before swapping: a= %d \t b= %d,a,b); // a=5 b=10

swap(&a,&b);

printf(\n After swapping: a= %d \t b= %d,a,b); // a=10 b=5

free(p); free(q);

}

void swap(int *x, int *y)

{

int t=x;

x=y;

y=t;

}

The arguments can generally passed to functions in one of the two ways:

1) sending the values of the arguments.

2) sending the address of the arguments.

In the first method, the valud of each actual argument in the calling function is copied into the corresponding formal arguments of the called function. With this method changes are made to the formal arguments in the called function and have no effect on the values of the actual arguments.

In the second method the addresses of actual arguments in the calling function are copied into the formal arguments of the called function. This means that, using these address we would have an access to the actual arguments from the formal arguments and hence we would able to manipulate the actual arguments in the function definition.

Using call by reference we can make a function return more than one value at a time, which is not possible in call by value.

If a function is called by passing the values then such functions are called call by value. By this, what we mean is on calling a function ,we are passing values of variables to it.

If we pass the location no.(address) of a variable to a function, then it is called call by reference i..e.., we are passing addresses of variables to the called functions. In called function, we should declare the pointer variable of the same datatype in order to receive the address of the variable.

Pointer with 1-D Arrays:-

Address = base address + no. of elements * scale factor

Scale factor is nothing but no. of bytes allocated based on data type depending on that address.

1) program to read 1-D array & print that array

main()

{

int a[5],n,i;

printf(enter n value:);

scanf(%d,&n);

for(i=0;i