-->

What is pointer in c language

3 minute read

 

What is pointer in c language

What is pointer?
·         Pointer: pointer is nothing but simply a variable which can store address of other variable.
·          A pointer is a variable its value is stored in another location of the memory.

Variable
Value
Address
Quantity
180
6000
P
6000
5048

·      For example we assign the address of quantity to a variable p.
·      The address of variable p is 5048.
·      Since this value of variable p is address of variable quantity.
·      We may access the value of quantity by using the variable p.
·      P point to the variable quantity.
·      Pointer contain garbage unit it is initialize therefore we must not use pointer variable before it is assign.
·      Pointer contains memory address as their values.
·      Since this memory address are the location in the computer memory where program instruction and data are stored.
·      Pointer can be use to access manipulate data stored in the memory.
·      The declaration of pointer variable like.
·      Syntax: data_type *pt_name.
·      Here * tell that the variable pt_name is a pointer variable.
int *p;
float *p;
·         once a pointer varable has been declare we can use the assignment operator to initialize that variable.
int quantity;
int *p;
p=&quantity;
·      The symbol -> is called arrow operator and also called member selection operator and are made a minus sign and greater than sign.
·       “.” Has a higher precedence then the operator “*”.

Advantages of pointer.
·      Pointer is more efficient in handling array and data tables.
·      Pointer can be use to return multiple values from a function via function argument.
·      Pointer permits references to function.
·      Pointer allowed C to support dynamic memory management.
·      Pointer providers an efficient tool for manipulating dynamic data stricter such as stricter link list queue stock and tree.
·      Pointer reduces length and complexity of program.
·      They increase the execution speed and thus reduce the programmed execution time.

Difference between .operator and->operator.
·      For a pointer, we could just use * pointer variable. But the . operator has grater priority than the * operator, so . is evaluated first. So we need to use the parenthesis (*pointervariable) for grater priority. But typing the ()'s all the time is hard, so here developed -> as a shortcut to say the same thing.
·      .operator cannot be applied to pointer. Arrow operator is generally mean to be applied to pointer.

   Can Single variable pointed by multiple pointers? Justify your answer.
Yes single variable pointed by multiple pointers. It is safe to have more than one pointer to the same address, but make sure that we know that if the memory is deleted using delete, further access to it will be undefined.
Int a=42;
Int *p=&a;
Int *q=p
*p=3145;

Access character array using pointer. Or
How can you use string using pointer? Explain with example.
#include<stdio.h>
void main()
{
char *ch[10];
int i=0,l=0;
clrscr();
printf("\n Enter String :");  scanf("%s",*ch);
for(;*ch[i]!='\0';ch[i]++)
{
l++;
}
printf("\n Length is %d",--l);
getch();
}

 How can we pass a pointer as an argument to a function? Explain with example. Or explain arithmetic operations on pointer variable.
#include<stdio.h>
void main()
{
int *n1,*n2;
clrscr();
printf("\n Enter n1 :");       scanf("%d",&n1);
printf("\n Enter n2 :");       scanf("%d",&n2);
pointer(&n1,&n2);
getch();
}

pointer(int *n1,int *n2)
{
printf("\n sum is %d",*n1 + *n2);
printf("\n sub is %d",*n1 - *n2);
printf("\n mul is %d",*n1 * *n2);
printf("\n div is %d",*n1 / *n2);
printf("\n mod is %d",*n1 % *n2);
}
 What is the use of & operator and * operator explain with example.
& – This operator gives ‘the address of’ a variable
* – This operator gives ‘the value of address’ of a variable
#include<stdio.h>
#include<conio.h>
void main()
{
         int x,y;
         int *ptr;
         x=10;
         ptr=&x;
         clrscr();
         printf("%d is stored at addr %u\n",x,&x);
         printf("%d is stored at addr %u\n",*ptr,ptr);
         getch();
}