0% found this document useful (0 votes)
34 views

1 Pointer Code Snippets

The document contains examples of C programs demonstrating the use of pointers. Some key examples include: 1) Declaring an integer array, assigning its base address to a pointer, and printing the value of the first element incremented by 1. 2) Declaring an integer, assigning its address to a pointer, dereferencing the pointer to multiply the integer by 3, and printing the new value. 3) Declaring variables including an integer and pointer, assigning the address of the integer to the pointer, and performing pointer arithmetic and dereferencing in a print statement. 4) Declaring variables including an integer and pointers, assigning addresses to the pointers, and printing the values accessed via pointers and

Uploaded by

Tswapna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

1 Pointer Code Snippets

The document contains examples of C programs demonstrating the use of pointers. Some key examples include: 1) Declaring an integer array, assigning its base address to a pointer, and printing the value of the first element incremented by 1. 2) Declaring an integer, assigning its address to a pointer, dereferencing the pointer to multiply the integer by 3, and printing the new value. 3) Declaring variables including an integer and pointer, assigning the address of the integer to the pointer, and performing pointer arithmetic and dereferencing in a print statement. 4) Declaring variables including an integer and pointers, assigning addresses to the pointers, and printing the values accessed via pointers and

Uploaded by

Tswapna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

1)#include <stdio.

h>

int main()

int a[]={1,2,3,4,5};

int *p;

p=a;

printf("%d",*p+1);

return 0;

o/p:2

2) #include <stdio.h>

int main()

int a=5;

int *ptr;

ptr=&a;

*ptr=*ptr*3;

printf("%d",a);

return 0;

o/p:15
3) #include<stdio.h>

void main()

int i=6,*j,k;

j=&i;

printf("%d\n",i * *j*i+*j);

4) #include<stdio.h>

void main()

int x=10;

int *y,**z;

y=&x;

z=&y;

printf("x=%d\ny=%d\nz=%d\n",x,*y,**z);

o/p:x=10 y=10 z=10


5) #include<stdio.h>

void main()

int x=20,*y,*z;

y=&x;

z=y;

y++;

z++;

x++;

printf("x=%dy=%uz=%u",x,y,z);

o/p:x=21 y=504 z=504


6) /*search an element and find the location,printf -1 if element is not found*/

#include<stdio.h>

void main()

int i,n,pos=-1,*a,element;

printf("enter size of an array");

scanf("%d",&n);

a=malloc(n*sizeof(int));

printf("enter the values:");

for(i=0;i<n;i++)

scanf("%d",a+i);

printf("enter a element to find ");

scanf("%d",&element);

for(i=0;i<n;i++)

if(*(a+i)==element)

pos=i;

break;

printf("%d is in %d",element,pos);

}
7)program to accept 5 integer values from keyboard and print the element of array in reverse order
using a pointer

#include<stdio.h>

main()

int a[5];

int *p=a;

for(i=0;i<5;i++)

scanf(“%d”,p+i);

printf(“numbers in reverse are:\n”);

for(i=4;i>=0;i--)

Printf(“%d\n”,*(p+i);

8) Write a c program to print array elements using pointers?

9)C program to sort array using pointers

#include<stdio.h>

void main()

int a[]={4,2,3,1};

int *p,i,j,t;

p=a;
for(i=0;i<4;i++)

for(j=i+1;j<4;j++)

if(*(p+j)<(*p+i))

t=*(p+i);

*(p+i)=*(p+j);

*(p+j)=t;

for(i=0;i<4;i++)

printf("%d",*(p+i));

10)

#include<stdio.h>

void main()

int b[]={1,2,3,4,5};

int i,*k;

k=&b[4]-4;
for(i=0;i<=4;i++)

printf("%d",*k);

k++;

o/p:1 2 3 4 5

11.

#include<stdio.h>

void main()

int n[25];

n[0]=100;

n[24]=200;

printf("\n%d\n%d",*n,*(n+24)+*(n+0));

12.program to find sum of n elements using malloc() function

13. #include <stdio.h>

int main()

int a=10;

float b=1.2;

void *p;

p=&a;
printf("%d\n",*((int *)p));

p=&b;

printf("%f\n",*((float *)p));

return 0;

You might also like