Ad Code

Responsive Advertisement

pointer in c language.

Q:-1 Explain Concept of pointers and advantages and disadvantages of pointer:- 




Pointers are widely used in programming; they are used to refer to memory location of another variable without identifier itself.

1) Pointers are mainly used linked list and call by references functions.
2)Every pointer is also variable.
3) The pointer contains address of another variable to which the pointer pointing.
4) To point a variable, the pointer must be of same data type as the variable.
5) To declare pointer we use * operator.
6) & notation means address – of operand in this case &a means ‘address of – a’

Example
main()
{
 int i=10,*ptr;
 ptr=&i;
 printf(“\n\n The value of i %d”, i);
 printf(“\n\n The value of i through pointer %d”, *ptr);
 printf(“\n\n The address of i %d”, &i);
 printf(“\n\n The address of pointer %d”, ptr);
 getch();
}


Advantages of pointer:-

->Pointer can be used to return multiple values from function via arguments or
parameters.

->Pointer is easy use for handling array.
Pointers array using to character string results in saving of data storage space in
memory.

->Pointers reduce length & complexity of program.

->More compact and efficient coding.

-> It supports dynamic memory allocation.

->C uses pointers explicitly with Array, function & structure.


           >>>     Disadvantages  <<<

-> Uninitialized pointers might cause segmentation fault.

-> Dynamically allocated block needs to be freed explicitly. Otherwise, it would lead to
memory leak.

-> Pointers are slower than normal variables.

-> If pointers are updated with incorrect values, it might lead to memory corruption.

-> Pointer bugs are difficult to debug. Its programmers responsibility to use pointers
effectively and correctly.


Q:-2Explain Arithmetic of Pointer with examples:- 

Arithmetical operations are possible with pointers. Pointers can be added and
subtracted. However pointer arithmetic is quite meaningless performed in arrays.
Addition and subtraction are mainly for moving forward and backward array.
*

A pointer in c is an address, which is a numeric value. Therefore, you can perform
arithmetic operations on a pointer just as you can on a numeric value. There are four
arithmetic operators that can be used on pointers: ++, --, +, and –

Operator
( ++ )  - > Goes to the next memory location the pointer is pointing to.

( -- ) - > Goes to the pervious memory location the pointer is pointing to.

( -= ) - > or - Subtract value of pointer.

( += or + )  - > Adding to the pointer

Incrementing Pointer

We prefer using a pointer in our program instead of an array because the variable pointer
can be incremented, unlike the array name which cannot be incremented because it is a
constant pointer.

main()
{
 int a[3]={4,5,6};
 int *ptr;
 ptr=&a;
 printf(“address %u array value %d\n”,ptr,*ptr);
 ptr++;
 printf(“address %d array value %d\n”,ptr,*ptr);
}

Decrementing Pointer

The same considerations apply to decrementing a pointer, which decreases its value by
the number of bytes of its data type as shown below

main()
{
 int a[3]={4,5,6};
 int *ptr;
 ptr=&a;
 printf(“address %d array value %d\n”,ptr,*ptr);
 ptr++;
 printf(“address %d array value %d\n”,ptr,*ptr);
}

Pointer Comparison

Pointers may be compared by using relational operators, such as ==, <, and >. If p1 and
p2 point to variables that are related to each other, such as elements of the same array,
then p1 and p2 can be meaningfully compared.
#include<stdio.h>
int main()
{
int *ptr1,*ptr2;
ptr1 = (int *)1000;
ptr2 = (int *)2000;
if(ptr2 > ptr1)
 printf("Ptr2 is far from ptr1");
return(0);
}

Q:-3 Explain Pointer with Function. OR Explain Call by value and call by reference

The arguments or parameters to the functions are passed in two ways.

      1. Call by value
      2. Call by reference

In call by value the values of the variables are passed. In this value of variable are not
affected by changing the value of the formal parameter.

void main()
{
        int a = 5;
        int b = 8;
        clrscr();
          printf("Before Calling the function a and b are %d and %d \n", a,b);
   value(a,b);
     printf("After Calling the function a and b       are %d and %d \n", a,b);
    getch();
}
value(int p, int q)
{
       p++;
       q++;
     printf("In the function p and q are %d and %d \n", p,q);
}

In call by reference the address of the variable are passed. In this value of variables are
affected by changing the value of the formal parameter.

void main()
{
       int a = 5;
       int b = 8;
      clrscr();
       printf("Before Calling the function a         and b are %d and %d \n", a,b);
     value(&a,&b);
      printf("After Calling the function a and     b   are %d and %d \n", a,b);
    getch();
}
value(int *p, int *q)
{
 (*p)++;
printf("In the function p and q are %d and %d \n", *p,*q);
}

/* Result of execution
Before Calling the function a and b are 5 and 8
In the function p and q are 6 and 9
After Calling the function a and b are 6 and 9
*/

-> In above example the address of a and b are passed to p and q which is a pointer
variable. (*p)++ means value at address is incremented. Similarly (*q)++ means value at address 2000, which is 8 incremented by 1

-> Now, the value of *p = 6 and *q = 9.
Here the address is not changed but value at this address is changed. Therefore after
calling the function the value of variables and b are changed.

Q:-4 Explain Pointer to array with an examples 

-> Firstly Array is the collection of elements which having same data type.

-> There are two types of pointer to array are available:

       (1) pointer to one dimensional array.
       (2) pointer to two dimensional array.
      (1) pointer to one dimensional array.

main()
 {
 int *p, sum, i;
 int x[5] = {5,9,6,3,7};
 i = 0;
 p = x; /* initializing with base address of x */
 printf("Element Value Address\n\n");
 while(i < 5)
 {
 printf(" x[%d] %d %u\n", i, *p, p);
 sum = sum + *p; /* accessing array element */
 i++, p++; /* incrementing pointer */
 }
 printf("\n Sum = %d\n", sum);
 printf("\n &x[0] = %u\n", &x[0]);
 printf("\n p = %u\n", p);
 }

(2) Pointer to two dimensional array

#include <stdio.h>
#include <conio.h>
main()
{
      int *m1=0,i,j;
      clrscr();
      printf("\nENTER VALUE FOR THE FIRST ARRAY:->");
    m1=(int*) malloc(sizeof(int)*3*3);
    for(i=0;i<3;i++)
    {
          for(j=0;j<3;j++)
          {
                scanf("%d",m1+i*3+j);
          }
    }
  printf("THE RESULT IS :->");
  for(i=0;i<3;i++)
  {
        for(j=0;j<3;j++)
        {
             printf("%d",*m1+i*3+j);
        }
        printf("\n");
   }
}

Array of pointers
We can declare an array as a pointer. Every element of this array can hold address of any
variable. We can say that every element of this array is a pointer variable. It is same as
array but it is a collection of addresses.

void main()
{
 int *a[3];
 int x = 5,y = 10, z = 15,i;
 clrscr();
 a[0] = &x;
 a[1] = &y;
 a[2] = &z;
   for(i=0; i<3; i++)
    {
       printf("address = %u\t", a[i]);
       printf("value = %d\n", *(a[i]));
    }
 getch();
}

In above example a is declared as a array of pointer. Every element of this array holds the
address of the variable. a[i] gives the address of the ith element of a and *a[i] gives the
value at this address.

Ex: - To Display the array using element using pointer 

main()
{
 int i,n,*pa;
 printf("Input the value of N=>");
 scanf("%d",&n);
 pa=malloc(n);
     for(i=0;i<n;i++)
    {
         scanf("%d",(pa+i));
    }
   printf("\n print using dynamic array\n");
    for(i=0;i<n;i++)
    {
         printf("%d\n",*(pa+i));
    }
 getch();
}

Post a Comment

0 Comments