C Programming - Pointers

C Programming - Pointers

A variable which holds the memory address of another variable is called
pointer.
Advantages of pointer[HSEB 2071,73]
1. Pointers are more efficient in handling arrays and data tables.
2. Pointer reduces the program execution time.
3. Pointes allow C to support dynamic memory management.
4. The use of pointer arrays to character strings result in saving of data
storage space in memory.
5. Pointers reduce length and complexity of a program.

Declaration of Pointer:

Syntax:
Data_type * pointer_name;
Example:
int *p;

63.Write a C program to add two values by using pointer?
#include<stdio.h>
#include<conio.h>
main( )
{
int x,y,sum;
int *a,*b;
printf("Enter the first number : ");
scanf("%d",&x);
printf("Enter the second number : ");
scanf("%d",&y);
a=&x;
b=&y;
sum= *a + *b;
printf("\n the sum of the two numbers=%d", sum);
getch( );
}


64. Write a program to demonstrate the value of variable and address of
variable using pointer in C?
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a=1;
printf("Adress of a=%u\n",&a);
printf("Value of a=%d",a);
getch();
}

Pointers and arrays

When an array is declared, the compiler allocates a base address and sufficient amount of
storage contain all the elements of the array in contiguous memory locations. The base
address is the location of the first element (index 0) of the array. The compiler also
defines the array names as a constant pointer to the first element. Suppose we declare
an array x as follows:
int x[5] = {1,2,3,4,5};
Suppose the base address of x is 1000 and assuming that each integer requires two
bytes, the five elements will be stores as follows:
Elements → x[0] x[1] x[2] x[3] x[4]
Value → 1 2 3 4 5
Address → 1000 1002 1004 1006 1008
The name x is defined as a constant pointer pointing to the first element, x[0] and
therefore the value of x is 1000, the location where x[0] is stored. That is,
x= &x[0]=1000
If we declare p as an integer pointer, then we can make the pointer p to point the array x
by the following assignment:
p = x;
This is equivalent to
p = &x[0];
Now, we can access every value of x using p++ to move from one element to another.
The relationship between p and x is shown as:
p = &x[0] (=1000)
p+1 = &x[1] (=1002)
p+2 = &x[2] (=1004)
p+3 = &x[3] (=1006)
p+4 = &x[4] (=1008)
You may notice that the address of an element is calculated using its index and the scale
factor of the data type. For instance,
Address of x[3]= base address+ ( 3 * scale factor of int)
= 1000 + (3*2) = 1006
When handling arrays, instead of using array indexing , we can use pointes to access
array elements. Note that *(p+3) gives the value of x[3]. The pointer accessing method is
much faster than array indexing.
QN1.Write down the meaning of following declarations?[2061]
a) int *p; A pointer that points to an integer data type.
b) int *p[10]; p is 10-element array of pointer to integer data.
c) int (*p) [10]; p is a pointer to a 10-element integer data.
d) int *p [void]; p is a function that returns a pointer to a integer data.
e) int *p(char *a) p is a function that accepts an argument which is a pointer to a character and returns a pointer to an integer data.