Pointers

Pointer:
  • The pointer is a variable and it is used to hold the address of another variable.
  • It is mainly used to allocate and De-allocate memory.
  • Each variable has two attributes: Address and value.
  • There are three elements in the pointer.

          * Pointer initialization
          * Pointer declaration
          * Pointer accessing data
Note:
  • & is an address operator.
  • * is the value operator for accessing pointer value.
  • %u is format specifiers for storing addresses.
  • A pointer data type is similar to the address of another variable data type.
Syntax for pointers:

Syntax for initializing pointer:
Data_type * variable_name;
Syntax for declaring pointer:
Pointer_varable=&address_of_another_vaiable;
Syntax for accessing the data by using pointer:
*pointer_variable =value(depend on requirements);

Example program for if:

#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,*p;//initialization
p=&a;//declaration
clrscr();
printf("\n Value of a is %d",a);
printf("\n address of a is %u",&a);
printf("\n  p value is %d",*p);
printf("\n  address of p is %u",&p);
*p=2+a;//accessing data
printf("\n Recent p value is %d",*p);
printf("\n Recent a value is %d",a);
getch();
}

Output:

Explanation:
Here using variable a is an int data type, the integer will have 2 bytes and hence the address of the next address element is incremented by 2.
Pointers and functions:
  • The pointer can be used as arguments in a function. The arguments or parameters to the function are passed in two ways.
Call by value
Call by reference
  • If you want to more detail about this topic, click the below link to learn this topic.
https://www.whereisstuff.com/2020/08/parameter-passing-methods-and-recursion.html

Array of pointers:
  • Array elements are always stored in consecutive memory locations according to the size of the array.
  • The size of the data type with the pointer variable refers to depends on the data type pointed by the pointer.
  • A pointer when incremented always points to a location after skipping the number of bytes required for the data type pointed toby it.
Example program array of pointers:

/* Program to sort a list of strings in alphabetical order using array of pointers*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void reorder(int a, char *x[]);//pointer with function
void main()
{
int i,a=0;
char *x[20];//array of pointer
clrscr();
printf("\n Enter each string on a separate line below.\n");
printf("\n Type \ 'END\' when finished.\n");
//read in the list of strings
do
{
x[a]=(char*) malloc(12 * sizeof (char));//allocate memory
printf("\n String %d: ",a+1);
scanf("%s",x[a]);
}
while(strcmp(x[a++],"END"));
//function calling  to reorder in the list of strings
reorder(--a,x);
printf("\n Reordered List of Strings: \n");
// display the reordered list of strings
for (i=0;i<a;++i)
printf("\n String %d: %s",i+1,x[i]);
getch();
}
// function definition
void reorder(int a,char *x[])
{
char *temp;//pointer variable to initialize
int i,y;
// Find lowest of all remaining strings
for(y=0;y<a-1;++y)
for (i=y+1;i<a;++i)
if (strcmp(x[y],x[i])>0)// compare the string for order
{
//interchanging the two strings
temp=x[y];
x[y]=x[i];
x[i]=temp;
}
return;
}

Output:
Structure of pointers:
  • A pointer pointing to a structure is known as pointers.
Example program:

#include<stdio.h>
#include<conio.h>
struct book
{
int book_no,bcost;
char *bname[20];
};
void main()
{
struct book p[3],*ptr;
clrscr();
printf("\n Enter three book of details....\n");
printf("\n Enter Book number, Name and Cost...\n");
for (ptr=p;ptr<p+3;ptr++)
scanf("%d%s%d",&ptr->book_no,ptr->bname,&ptr->bcost);
printf("\n STRUCTURE OUTPUT\n");
ptr=p;
while(ptr<p+3)
{
printf("%5d\t%-20s\t%d\n",ptr->book_no,ptr->bname,ptr->bcost);
ptr++;
}
getch();
}

Output:
Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Blog Archive

Service Support

Need our help to Learn or Post New Concepts Contact me

Blog Archive

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me