8

Pointers

CHAPTER OUTLINE
8.1 POINTER DEFINITION

With pointers we can perform lot many operations by using memory addresses. It is possible to access and display the memory location of a variable using '&' operator. A pointer variable holds the memory address of any type of variable. The pointer variable and normal variable should be of the same type. The pointer is denoted by asterisk (*) symbol.

Pointer: A pointer is a variable that holds a memory address. A pointer can have any name that is legal for other variable, and it is declared in the same fashion like other variables but it is always declared by * operator.

8.2 FEATURES OF POINTERS
  1. Pointers save the memory space.
  2. Execution time with pointer is faster because data is manipulated with the address, i.e. direct access to memory location.
  3. The memory is accessed efficiently with the pointers. The pointer assigns the memory space and also it releases. The memory is dynamically allocated.
  4. Pointers are used with data structures. They are useful for representing two-dimensional and multi-dimensional arrays.
  5. We can access elements of any type of array irrespective of its subscript range.
  6. Pointers are used in file handling.
  7. Pointers are used to allocate memory dynamically.
8.3 POINTERS AND ADDRESSES

When a variable of any data type is declared, according to its data type the memory is reserved.

As shown in Figure 8.1, the memory locations are reserved for the variables x, y and c. Integer variables require two bytes each and for character variable one byte is reserved. The locations 65550 and 65551 are used to store ‘x’, 65552 and 65553 are used to store ‘y’ and so on. The starting address for ‘x’ is 65550 and ‘y’ is 65552. The character needs only one byte and its location shown in Figure 8.1 is 65554.

 

pearson

 

FIGURE 8.1 Memory allocated

8.4 POINTER DECLARATION

The pointer variables can be declared as follows.

Example:

int *x;

float *f;

char *y;

  1. In the first statement, ‘x’ is an integer pointer and it tells to the compiler that it holds the address of any integer variable. In the same way, ‘f’ is a float pointer which stores the address of any float variable and ‘y’ is a character pointer that stores the address of any character ­variable.
  2. The indirection operator (*) is also called the dereferencing operator. When a pointer is dereferenced, the value at that address stored by the pointer is retrieved.
  3. Normal variable provides direct access to their own values, whereas a pointer provides ­indirect access to the values of the variable whose address it stores.
  4. The indirection operator (*) is used in two distinct ways with pointers, declaration and ­dereference.
    1. When a pointer is declared, the asterisk (*) indicates that it is a pointer, not a normal ­variable.
    2. When the pointer is dereferenced, the indirection operator indicates that the value at that memory location stored in the pointer is to be accessed rather than the address itself.
    3. Also note that the indirection operator (*) is the same operator that can be used for multiplication. The compiler knows which operation to be performed, based on the context.
    4. The '&' is the address operator and it represents the address of the variable. The '%u' is used with printf()function for printing the address of a variable. The address of any variable is a whole number.
8.5 ARITHMETIC OPERATIONS WITH POINTERS

Arithmetic operations on pointer ­variables are also possible. Increment, decrement, prefix and postfix operations can be performed with the pointers. The effect of these operations is shown in Table 8.1 given below.

From the above table, we can observe that on the increment of pointer variable for integers, the address is incremented by 2, i.e. 4046 is the original address and on increment its value will be 4048, because integers require two bytes.

Similarly, for characters, floating point numbers and long integers require 1, 4 and 4 bytes, ­respectively. After the effect of increment and ­decrement, the memory locations are shown in Table 8.1. Refer program number 14 under 8.E for detail understanding.

 

TABLE 8.1 Pointer and arithmetic operation

pearson
8.6 POINTERS AND ARRAYS

The array name itself is an address or pointer. It points to the address of the first element (0th) ­element of an array). The elements of the array together with their addresses can be displayed by using array name itself. The array elements are always stored in contiguous memory locations.

8.7 POINTERS TO POINTERS

A pointer is known as a variable containing address of another variable. The pointer variables also have an address. The pointer variable containing address of other pointer variables is called as pointer to pointer. This chain can be continued to any extent.

8.8 DYNAMIC MEMORY ALLOCATION

There is a technique by which program can obtain space in the main memory which is called dynamic allocation. Dynamic allocation is a technique in which a program can acquire storage space in the main memory. In this method, the space for the program is allocated during execution of the program. The free region of the memory is called the heap.

8.9 MEMORY ALLOCATION FUNCTIONS
  1. malloc( ): This function is used to allocate memory space in bytes to the variables of ­different data types. This function reserves bytes of determined size and returns the base address to the pointer variable. The prototypes are declared in alloc.h and stdlib.h. The format of the malloc() function is as under.

    pnt=(data type*)malloc(given size);

     

    Here, from the data type, compiler understands the pointer type, and given size is the size to reserve the memory.

    pnt=(int *)malloc(20);

     

    Here, in this declaration, 20 bytes are allocated to pointer variable pnt of type int and base address is returned to pointer pnt.

  2. calloc( ): This function is useful for allocating multiple blocks of memory. It is declared with two arguments. The prototypes are declared in alloc.h and stdlib.h. The format of the calloc() function is as follows.

    pnt=(int *)calloc(4,2);

     

    The above declaration allocates four blocks of memory; each block contains 2 bytes. The base address is stored in the integer pointer. This function is usually used for allocating memory for array and structure. The calloc() can be used in place of malloc() function. The programs illustrated with malloc()function can be executed using calloc() function.

  3. free( ): The free() function is used to release the memory allocated by memory allocating functions. Thus, using this function wastage of memory is prevented. The declaration of the function is given below.

    free(pnt);

     

    In the above declaration, pnt is a pointer. The free() function releases the memory occupied by the pointer variable pnt.

  4. realloc( ): This function reallocates main memory. The prototypes are declared in alloc.h and stdlib.h. Attempts are made to shrink or enlarge the previously allocated memory by malloc() or calloc() functions. It returns the address of the reallocated block, which can be different than the original address. If the block cannot be reallocated or size = 0, realloc() returns NULL. The syntax of this function is as below.

    pnt=realloc(pnt,newsize);

    This function reallocates new memory of new size to the pointer variable pnt.

EXERCISES

 

8.A Fill in the blanks

  1. A pointer is a memory variable that stores a _______.
    1. memory address
    2. value
    3. none of the above
  2. Pointer can have any name that is legal for other ________.
    1. variable
    2. keyword
    3. header file
  3. Pointer is declared in the same fashion like other variable, but it is always denoted by ________ operator.
    1. *
    2. &
    3. #
  4. Pointers save the ________.
    1. memory space
    2. value
    3. none of the above
  5. The execution time with pointer is faster because ________.
    1. data is manipulated with the address
    2. data is manipulated with the values
    3. none of the above
  6. Pointers are used with data structures. They are useful for representing ________ and multi-dimensional arrays.
    1. two-dimensional
    2. multi-dimensional
    3. none of the above
  7. In the statement int *x is an ________.
    1. integer pointer
    2. float pointer
    3. unsigned pointer
  8. Declaration of int *x; tells the compiler that it holds the ________.
    1. address of any integer variable
    2. address of any float variable
    3. address of any char variable
  9. The indirection operator (*) is used for ________.
    1. declaration and dereference
    2. declaration
    3. dereference
    4. none of the above
  10. When the pointer is dereferenced, the indirection operator indicates that the ________.
    1. value at that address stored can be accessed
    2. address of variable can be accessed
    3. none of the above
  11. Fill in the columns after ++ and – – operations.

     

    pearson

     

  12. The array name by itself is an address or ________.
    1. pointer
    2. value
    3. identifier
  13. The array of pointers is nothing but ________.
    1. a collection of addresses
    2. a collection of values
    3. all the above
  14. The pointer variables also have an ________ which is stored in another pointer.
    1. name
    2. address
    3. value
  15. The pointer variable containing address of another pointer variables is called as ________.
    1. pointer to pointer
    2. pointer
    3. double pointer
  16. The memory address of the variable is ________.
    1. constant
    2. pointer constant
    3. integer

8.B True or false

  1. A pointer saves the memory space.
  2. A pointer is a memory variable that stores memory address.
  3. Pointers can be used to represent arrays.
  4. The indirection operator asterisk (*) is used to identify the pointer variable.
  5. The & is address operator and represents the address of the variable.
  6. The physical memory locations of the variable is system dependent.
  7. The user can predict the address of the memory variable.
  8. The pointer of any data type requires only four bytes.
  9. A pointer cannot point to another pointer variable.
  10. The address of pointer variable for integer is 4044, after incrementing the pointer variable its address becomes 4066.
  11. The pointer variable holding address of another pointer is pointer to pointer.
  12. p** is a pointer to pointer.
  13. *p is a pointer.
  14. char **q is pointer to pointer.

8.C Match the following correct pairs given in Group A with Group B

 

  1.  

    pearson
  2.  

    pearson
  3.  

    pearson
    pearson
  4.  

    pearson
  5.  

    pearson

     

8.D Select the appropriate option from the multiple choices given below

  1. Following one operation with which addresses are not possible.
    1. Addition of two addresses (pointers).
    2. ++
  2. Following one operation is possible with address holding (pointers).
    1. multiplication of addresses or multiplication of address with a constant
    2. division of address with a constant
    3. ++
    4. none of the above
  3. The declaration int *arrp[3] is nothing but
    1. integer array
    2. integer pointer array
    3. one of the above
  4. Pointer to pointer can be denoted as
    1. **p
    2. p**
    3. *p
  5. The following program will show the error message
    int p=2;
    char *c=&p;
    1. expression syntax in function main
    2. non-portable assignment
    3. suspicious pointer conversion
    4. none of the above
  6. int p=2, *k;
    int *c=&p;
    k=c;

    we can say that
    1. pointer ‘c’ and ‘k’ are pointing to ‘p’
    2. pointer ‘k’ pointing to ‘p’ and ‘c’
    3. only pointer ‘c’ pointing to ‘p’
  7. int s,*j,**kk;
    j=&s;
    kk=&j;

    we can say that
    1. kk is pointer of pointer
    2. j is pointer of pointer
    3. k is only pointer

8.E What will be the output/s of the following program/s?

  1. Use pointer and display value and address of an integer element.

    void main()
    {
          int num=10,*p;
          p=#
          clrscr();
          printf(" %d",*p);
          printf(" %d",num);
          printf(" %u",&num);
          printf(" %u",p);
          getche();
    }

    Explanation: Pointer ‘p’ points value stored at address of num. To access the value of num, we use pointer. Value of num = 10. In the first two printf() statements, the variable value 10 is printed. Also, in the third and fourth printf() statements, the addresses of num are displayed.

  2. Use pointer and display a number.

    void main()
    {
         int p=10;
          void *pt=&p;
          clrscr();
          *(int*)pt=12;
          printf("%d",p);
    }

    Explanation: Void pointer holds the address of any data type such as int, float, char and by assigning the address, we can typecast it, i.e. which data type. The *(pt) points the value stored at the address held by pt. The next value assigned to the pointer is 12 and the same is displayed.

  3. Use pointer and display number by different ways.

    void main()
    {
          int t=10,*p;
          clrscr();
          p=&t;
          printf(" t = %d t = %d t = %d",
          t,*(&t),*p);
    }

    Explanation: In the above program, it displays the value of the variable ‘t’. Three different syntaxes are used in the printf() statement.

  4. Use pointers and display long integers and floating point numbers.

    void main()
    {
    long int a,*pa;
    float b,*pb;
    clrscr();
    printf("Enter Integer & float Value :");
    scanf("%ld %f",&a,&b);
    pa=&a;
    pb=&b;
     
    printf(" Address of a=%u",pa);
    printf(" Value of a=%ld",a);
    printf(" Value of a=%ld",*(&a));
    printf(" Value of a=%ld",*pa);
    printf(" Address of b=%u",pb);
    printf(" Value of b=%.2f",b);
    printf(" Value of b=%.2f",*(&b));
    printf(" Value of b=%.2f",*pb);
    }

    Explanation: Integer value 2345 and float value 2.345 are prompted through the keyboard. The same values and their addresses are printed on the screen.

  5. Display the memory requirement for character, integer and float number and their pointers.

    void main()
    {
    clrscr();
    printf("char %d byte & its pointer %d bytes ",
               sizeof(char), sizeof(char*));
    printf("int %d byte & its pointer %d bytes ",sizeof(int),sizeof (int*));
    printf("float %d byte & its pointer %d bytes ",sizeof(float),sizeof (float*));
    }

    Explanation: Pointer of any data type requires two bytes. This is shown in all the three printf() statements.

  6. Program to display a string using pointer.

    void main()
    {
          char c[]="India";
          char *p;
          p=c;
          clrscr();
          printf("%s",p);
    }

    Explanation: Here, the address of 0th element is assigned to character pointer ‘p’. In other words, base address of the string is assigned to ‘p’. After every increment of ‘p’, the pointer goes to the next character of the string. The displayed string on the screen is India.

  7. Display two strings.

    void main()
    {
          char *c="India";
          char *p ="Bharat";
          char *k;
          k=p;
          p=c;
          clrscr();
          printf("%s",p);
          printf(" %s",k);
    }

    Explanation: Here, the address of the 0th element is assigned to character pointers ‘c’ and ‘p’. In other words, base addresses of a string are assigned to ‘k’ and ‘p’. The displayed string on the screen is India and Bharat.

    Arithmetic operations with pointers

  8. Use pointers and display addition of two numbers with and without pointers.

    void main()
    {
          int a=5,b=7,c,d,*ap,*bp;
          clrscr();
          ap=&a;
          bp=&b;
          c=a+b;
          d=*ap+*bp;
          printf(" Sum of A & B :%d",c);
          printf(" Sum of A & B : %d",d);
    }

    Explanation: Here, sum of A and B is 12 .With simple arithmetic addition a+b, which is stored in ‘c’, is displayed sum of A & B = 12, i.e. addition of value pointed by pointer ap & bp is 12.

  9. Use pointers and display addition, subtraction, multiplication and division of two numbers with pointers.

    void main()
    {
    int a=25,b=10,*p,*j;
    p=&a;j=&b;
    clrscr();
    printf(" %d %d %d %d %d",*p+b, *p–b,*p * *j,*p / *j,*p % *j);
    }

    Explanation: Execution of printf() always starts from right to left, so in following sequences operation will be performed:

    *p%*j = 25 % 10 = 5

    *p / *j = 25 /10 = 2

    *p * *j = 25 * 10 = 250

    *p-b = 25 – 10 = 15

    *p+b = 25+10 = 35

    Thus, the output will be as 35, 15, 250, 2, 5

  10. Multiplication and addition of pointers.

    void main()
    {
          int num=10,*p;
          p=#
          clrscr();
          num=*p**p+*p+*p;
          printf(" %d",num);
          getche();
    }

    Explanation: Here, in this program *p = 10 & num = 10, hence num=*p * *p + *p + *p=10*10+ 10+10= 120

  11. Use pointer increment operation to display characters.

    void main()
    {
          char name[15]="He",*ch;
          ch=name;
          clrscr();
          while(*ch!='')
          {
            printf("%c",(*ch)+1);
            ch++;
          }
    }

    Explanation: *ch points character stored in name array and gives its value in ASCII by %c, thus it will print "He". But (*ch)+1 indicates ASCII value added by 1 every time, hence instead of He it will print If [(I=(ASCII of H)+1) & (f=(ASCII of e)+1)]

  12. Use pointer increment operation to display characters.

    void main()
    {
          char name[15]="Good",*ch;
          ch=name;
          clrscr();
          while(*ch!='')
          {
          printf("%c",(*ch)-1);
          ch++;
          }
    }

    Explanation: The answer is also similar to previous one.

  13. Use pointer increment operation and display addresses of an array element.

    void main()
    {
    int x[5]={3,4,6,8,10},t=0;
    clrscr();
    printf(" Element No. Element Address");
    while(t<5)
    {
    printf(" x[%d] = %8d %9u",t,*(x+t),x+t);
    t++;
    }
    }

    Explanation: In this program, initially t=0 & *(x+t)=> x[t] && x+t=>&x increments with positions indicated by ‘t’. Thus, elements and addresses are displayed.

  14. Pointer incrementation and decrementation operations.

    void main()
    {
          int num=10,*p;
          clrscr();
          p=&num;
          printf(" %u",p);
          printf(" %u",p+4);
          printf(" %u",p-2);
          printf(" %d",++(*p));
          getche();
    }

    Explanation: The ‘p’ prints the address of num and it is 65498. p+4 mean base address added by 8 bytes, i.e. (65498+8=65506) and p-2 becomes (65498–4=65494) and at last ++((*p))=++(10)=11.

    Array operations and display

  15. Display elements of an array without pointer.

    void main()
    {
    int x[]={1,2,3,4,5,6,7},k=1;
    clrscr();
    while(k<=5)
    {
          printf("%2d",k[x-1]);
          k++;
    }

    Explanation: Initially k=1 & k[x-1] indicates & [(base address)–one location]=first address, now ‘k’ will increment the position of base address by particular location, but ‘k’ will increment up to 5 only, the output will print up to 5 value from 0th location to 4th location.

  16. Perform operation on array elements and display them.

    void main()
    {
          int a[]={15,20,40},b[3],j=0,k=2;
          clrscr();
          for(j=0;j<3;j++)
       {
           b[j]=a[k];
           k--;
       }
           j=0;
          while(j<3)
          printf(" %d ",b[j++]);
    }

    Explanation: Inner for loop will assign value as

    b[0]=a[2]=>b[0]=40

    b[1]=a[1]=>b[1]=20

    b[2]=a[0]=>b[2]=15

    thus output will of b[0],b[1],b[2] as 40,20,15.

  17. Display numbers with an array with and without pointer.

    void main()
    {
    int arr[]={10,20,30},p=0;
    clrscr();
       for(p=0;p<3;p++)
      {
          printf(" %d %d %d",
             p[arr],*(arr+p),arr[p]);
       }
    }

    Explanation: In this program p[arr]=>arr[p].*(arr+p)=> pointer points the base address and holds the value stored in it and position increments by ‘p’ value.

    arr[p] will print

    0[arr]=*(arr+0)=arr[0]=10

    1[arr]=*(arr+1)= arr[1]=20

    2[arr]=*(arr+2)=arr[2]=30

  18. Use pointer and display addition of array elements.

    void main()
    {
       int arr[]={15,20,40},sum=0,j=0;
       clrscr();
       while(j<3)
       sum=sum+*(arr+j++);
       printf(" sum %d",sum);
    }

    Explanation: Initially j=0 and the loop will execute thrice as

    sum=0+*(arr+0)=0+15=15

    sum=15+*(arr+1)=15+20=35

    sum=35+*(arr+2)=35+40=75

  19. Use pointer and display array elements.

    void main()
    {
       int *arr[3];
       int ar[]={5,10,15},k;
       for(k=0;k<3;k++)
       arr[k]=ar+k;
       clrscr();
       for(k=0;k<3;k++)
       printf("%d ",*(arr[k]));
    }

    Explanation: *arr[   ] is array of pointer. The arr[k]=ar+k holds the address and pointer points the value stored in that address.

  20. Sum of squares and cubes of array elements.

    #include <math.h>
    void main()
    {
    int k[]={1,2,3,4,5},t,sumsq=0, sumc=0;
    clrscr();
    for(t=0;t<5;t++)
    {
    sumsq=sumsq+pow(*(t+k),2);
    sumc=sumc+pow(k[t],3);
    }
    printf(" Sum of Squares of array elements : %d", sumsq);
    printf(" Sum of Cubes of array elements : %d",sumc);
    }

    Explanation:

    • Power function is used to calculates x raise to y i.e. pow(x,y).
    • *(t+k) takes the value stored in k [ ] array and finds square and cubes according to the functions.
    • And recursive addition is performed.
    • For square

      pow(k[0],2)+pow(k[1],2)+pow(k[2],2)+ pow(k[3],2)+pow(k[4],2)=pow(1,2)+ pow(2,2)+pow(3,2)+pow(4,2)+pow (5,2)=1+4+9+16+25=55

    • For cube

      pow(k[0],3)+pow(k[1],3)+pow (k[2],3)+pow(k[3],3)+pow(k[4],3)=pow(1,3)+pow(2,3)+pow(3,3)+pow(4,3)+pow(5,3)= 1+8+27+64+125=225

  21. Three by three matrix and printout its numbers.

    void main()
    {
    int i,j=1,*p;
    int a[3][3]={{1,2,3},{4,5,6}, {7,8,9}};
    clrscr();
    printf(" Elements of An Array with their addresses ");
    p=&a[0][0];
    for(i=0;i<9;i++,j++)
    {
    printf("%5d [%5u]",*(p),p);
    p++;
    if(j==3)
    {
    printf(" ");
    j=0;
    }}
    }

    Explanation:

    %5d indicates identifier for integer value with 4 blank spaces.

    %5u indicates identifier for integer address.

    p holds the base addresses which are always indicated by a[0][0] and pointer points value at that address.

    Thus,*p gives pointed values after incrementing its location as p++ by 2 bytes.

    Elements and their addresses are displayed.

  22. Display original array and duplicate array elements.

    void main()
    {
    int so[]={1,2,3,4,5},*pb,ds[5],i;
    pb=so;
    clrscr();
    for(i=0;i<5;i++)
    {
          ds[i]=*pb;
          pb++;
    }
    printf("Original Array Duplicated Array");
    for(i=0;i<5;i++)
    printf(" %d %d", so[i],ds[i]);
    }

    Explanation: In this program, ‘pb’ holds the address of ‘so’and *pb points value at that address which is assigned to ‘ds’.

  23. Print addresses and element values of an array.

    void main()
    {
    int *arrp[3];
    int arrl[3]={5,10,15},k;
    for(k=0;k<3;k++)
    arrp[k]=arrl+k;
    clrscr();
    printf(" Address Element ");
    for(k=0;k<3;k++)
    {
           printf(" %u",arrp[k]);
          printf(" %7d ",*(arrp[k]));
       }
       }

    Explanation: The arrp is array of pointer, i.e. arrp[0], arrp[1], arrp[2] will give address of each element.

    arrl is integer array which has value 5 10 15.
    "arrp[k]=arrl+k" indicates assigning the address to array pointer which points the value stored at that address.

    Pointer to pointer

  24. Use pointer to pointer and display values of element.

    void main()
    {
       int a=2,*p,**q;
       p=&a;
       q=&p;
       clrscr();
       printf("%d %d %d",a,*p,**q);
    }

    Explanation:

    • p=&a means ‘p’ holds the address of ‘a’.
    • q=&p means ‘q’ holds the address of ‘p’.
    • *p points value at the address of ‘a’.
    • **q points to the pointer which contains value stored at the address of ‘a’ **q=>*(*(q))=>*(*(&p))=>*(*(&(&a))=>*(&a)=>2
  25. Use pointers to pointers and display numbers.

    void main()
    {
    int k;
    int a[]={1,2,3};
    int *b[3];
    int **c[3];
    int ***d[3];
    int ****e[3];
    int *****f[3];
    clrscr();
    for(k=0;k<3;k++)
    {      
          b[k]=a+k;
          c[k]=b+k;
          d[k]=c+k;
          e[k]=d+k;
          f[k]=e+k;
    }
    for(k=0;k<3;k++)
    {
    printf("%3d",*b[k]);
    printf("%3d",**c[k]);
    printf("%3d",***d[k]);
    }
    }
    }
    printf("%3d",****e[k]);
    printf("%3d ",*****f[k]);
    }
    }

    Explanation:

    *b[3] is array pointer of size 3.

    **c[3] is array pointer to pointer of size 3.
    ***d[3] is array pointer to pointer to pointer of size 3.

    ****e[3] is array pointer to pointer to pointer to pointer of size 3.

    *****f[3] is array pointer to pointer to pointer to pointer to pointer of size 3.

    b[k]=a+k; ‘b’ holds the address of ‘a’.

    c[k]=b+k; ‘c’ holds the address of ‘b’.

    d[k]=c+k; ‘d’ holds the address of ‘c’.

    e[k]=d+k; ‘e’ holds the address of ‘d’.

    f[k]=e+k; ‘f’ holds the address of ‘e’.

    Double pointers

  26. Print element values and their addresses using pointers and double pointers.

    void main()
    {
       int a=2,*p,**q;
       p=&a;
       q=&p;
       clrscr();
       printf (" Value of a= %d Address of a=%u",a,&a);
       printf (" Through *p Value of a= %d Address of a=%u",*p,p);
       printf (" Through **q Value of a= %d Address of
                   a=%d",**q,*q);
    }

    Explanation:

    The address of a becomes –40.

    It takes value within the range –128 to +127, thus *q holds the address of a=65496–65536=–40.

    If we take %u, then it prints the address of a=65496.

8.F Find the bug/s in the following program/s

  1.  

    void main()
    {
    char *c;
    float x=10;
    c=&x;
    printf("%d",*c);
    }

  2.  

    void main()
    {
    float x=10.25,*f;f=x;
    clrscr();
    printf("%g",*f);
    }

  3.  

    void main()
    {
    float x=10.25,*t,*f;
    t=&x;
    f=&t;
    clrscr();
    printf("%g",**f);
    }

  4.  

    void main()
    {
    char sa[]="How are you?";
    char t,*f;f=&sa;
    clrscr();
    printf("%s",f);
    }

  5.  

    void main()
    {
    int *t,x;t=&x;x=11;
    *t++;
    clrscr();
    printf("%d",*t);
    }

  6.  

    void main()
    {
    int t[]={1,2,3,4,5};
    int *p,*q,*r;p=t;q=p[1];
    r=p[2];
    clrscr();
    printf("%d %d %d",*p,*q,*r);
    }

  7.  

    void main()
    {
    int num[2][3]={1,2,3,4,5},*k;
    k=&num;
    clrscr();
    printf("%d",*k);
    }

  8.  

    void main()
    {
    int x=5,y=8,z;
    int *px,*py,*pz;px=&x;py=&y;pz=&z; pz=*px+*py;
    clrscr();
    printf("%d",z);
    }

  9.  

    void main()
    {
    int x=5;
    int *px=&x,*py;
    clrscr();
    printf("%d",px);
    }

  10.  

    void main()
    {
       int x,*px;
       *px=5;
       px=&x;
       clrscr();
       printf("%d",x);
    }

  11.  

    void main()
    {
       int *p,*q,a=2;
       p=&a;
       q=p;
       clrscr();
       printf("%d %d", p, q);
    }

  12.  

    void main()
    {
       int *p,*q,**t,a=2;
       p=&a;
       q=p;
       t=p;
       clrscr();
    printf("%d %d %d", *p, *q, **t);
    }

  13.  

    void main()
    {
       int y[]={1,2,3},*p;
       p=&y[1];
       clrscr();
       printf("%d %d",(1+y),*p);
    }

  14.  

    void main()
    {
       unsigned y[]={65555,65550,65543};
       clrscr();
       printf("%u",y[2]);
    }

  15.  

    void main()
    {
    int p[]={1,2,3,4};
    char *c;
    c=p;
    clrscr();
    printf("%d",*c);
    }

8.G Attempt the following programming exercises

  1. Write a program to allocate memory for 10 integers and 5 floating point numbers.
  2. Write a program to display all the elements of a character array using pointer.
  3. Write a program to access unsigned integers using pointer and display them.
  4. Write a program to calculate area and circumference of a circle using pointer.
  5. Write a program to demonstrate the use of realloc() and calloc() functions.

ANSWERS

 

8.A Fill in the blanks

  1. (a) memory address
  2. (a) variable
  3. (a) *
  4. (a) memory space
  5. (a) data is manipulated with the address
  6. (a) two-dimensional
  7. (a) integer pointer
  8. (a) address of any integer variable
  9. (a) declaration and dereference
  10. (a) value at that address stored can be accessed
  11. 4054 4052
    4062 4054
    4064 4056
  12. (a) pointer
  13. (a) a collection of addresses
  14. (b) address
  15. (a) pointer to pointer
  16. (b) pointer constant

8.B True or false

  1. T   2. T   3. T   4. T   5. T
  6. T   7. F   8. F   9. F 10. F
11. T 12. F 13. T 14. T  

8.C Match the following correct pairs given in Group A with Group B

  1. 1. D, 2. C, 3. A, 4. B
  2. 1. C, 2. B, 3. D, 4. A
  3. 1. C, 2. A, 3. D, 4. B
  4. 1. D, 2. C, 3. B, 4. A
  5. 1. C, 2. D, 3. B, 4. A

8.D Select the appropriate option from the multiple choices given below

  1. (a) addition of two addresses (pointers)
  2. (c) + +
  3. (a) integer pointer array
  4. (a) **p
  5. (a) expression syntax in function main
  6. (a) pointer ‘c’ and ‘k’ are pointing to ‘p’
  7. (a) kk is pointer of pointer

8.E What will be the output/s of the following program/s?

  1. 10 10 65498 65498
  2. 12
  3. t=10 t=10 t=10
  4. Enter Integer & float Value :2345 2.345

    Address of a=65492

    Value of a=2345

    Value of a=2345

    Value of a=2345

    Address of b=65496

    Value of b=2.35

    Value of b=2.35

    Value of b=2.35

  5. char 1 byte & its pointer 2 bytes

    int 2 byte & its pointer 2 bytes

    float 4 byte & its pointer 2 bytes

  6. India
  7. India Bharat
  8. Sum of A & B : 12

    Sum of A & B : 12

  9. 35 15 250 2 5
  10. 120
  11. If
  12. Fnnc
  13. Element No. Element Address

    x[0] = 3 65490

    x[1] = 4 65492

    x[2] = 6 65494

    x[3] = 8 65496

    x[4] = 10 65498

  14. 65498 65506 65494 11
  15. 1 2 3 4 5
  16. 40 20 15
  17. 10 10 10

    20 20 20

    30 30 30

  18. sum=75
  19. 5 10 15
  20. Sum of Squares of array elements : 55

    Sum of Cubes of array elements : 225

  21. Elements of An Array with their addresses

    1[65482] 2[65484] 3[65486]

    4[65488] 5[65490] 6[65492]

    7[65494] 8[65496] 9[65498]

  22. Original Array Duplicated Array

    1         1

    2         2

    3         3

    4         4

    5         5

  23. Address Element

    65494         5

    65496         10

    65498         15

  24. 2 2 2
  25. 1 1 1 1 1

    2 2 2 2 2

    3 3 3 3 3

  26. Value of a=2 Address of a=65496

    Through *p Value of a=2 Address of a=65496

    Through **q Value of a=2 Address of a=–40

8.F Find the bug/s in the following program/s

  1. Pointer and variable should be of the same data type.
  2. & is missing in assignment statement.
  3. **f should be declared as pointer to pointer.
  4. & is not needed.
  5. t++ does not work,(++*t) works.
  6. & is required in q =p[1]; and r =p[2];
  7. k =&num; would be k =&num[0][0];
  8. In the assignment statement *pz is required.
  9. %u is required for formatting address.
  10. The statement *px=5; should appear after px=&x;
  11. * is missing (*p,*q)
  12. In the statement t=p,& is missing.
  13. * missing at (1+y).
  14. Array values are beyond range.
  15. Pointer type does not match with the array type.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.229.142