0% found this document useful (0 votes)
96 views7 pages

Pointers Examples: Page 1 of 7

The document provides examples of using pointers in C programs. It demonstrates how pointers can be used to pass arguments to functions by reference, access array elements using pointer notation, and manipulate the values pointed to by pointers both within and across functions. Key concepts illustrated include pointers to arrays, passing pointers to functions, and dereferencing pointers with unary operators.

Uploaded by

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

Pointers Examples: Page 1 of 7

The document provides examples of using pointers in C programs. It demonstrates how pointers can be used to pass arguments to functions by reference, access array elements using pointer notation, and manipulate the values pointed to by pointers both within and across functions. Key concepts illustrated include pointers to arrays, passing pointers to functions, and dereferencing pointers with unary operators.

Uploaded by

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

Pointers Examples

1. What is the output of following program?


# include <stdio.h>
void fun(int x)
{
x = 30;
}

int main()
{
int y = 20;
fun(y);
printf("%d", y);
return 0;
}
1Ans:

2.Output of following program?


# include <stdio.h>
void fun(int *ptr)
{
*ptr = 30;
}

int main()
{
int y = 20;
fun(&y);
printf("%d", y);

return 0;
}
2Ans:30

3.
#include <stdio.h>

int main()
{
int *ptr;
int x;

ptr = &x;
*ptr = 0;

Page 1 of 7
printf(" x = %dn", x);
printf(" *ptr = %dn", *ptr);

*ptr += 5;
printf(" x = %dn", x);
printf(" *ptr = %dn", *ptr);

(*ptr)++;
printf(" x = %dn", x);
printf(" *ptr = %dn", *ptr);

return 0;
}
3 Ans: x = 0
*ptr = 0
x=5
*ptr = 5
x=6
*ptr = 6

4.Consider a compiler where int takes 4 bytes, char takes 1 byte and pointer takes
4 bytes.
#include <stdio.h>

int main()
{
int arri[] = {1, 2 ,3};
int *ptri = arri;

char arrc[] = {1, 2 ,3};


char *ptrc = arrc;

printf("sizeof arri[] = %d ", sizeof(arri));


printf("sizeof ptri = %d ", sizeof(ptri));

printf("sizeof arrc[] = %d ", sizeof(arrc));


printf("sizeof ptrc = %d ", sizeof(ptrc));

return 0;
}
4 Ans: sizeof arri[] = 12 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 4

5.Assume that float takes 4 bytes, predict the output of following program.
#include <stdio.h>

int main()

Page 2 of 7
{
float arr[5] = {12.5, 10.0, 13.5, 90.5, 0.5};
float *ptr1 = &arr[0];
float *ptr2 = ptr1 + 3;

printf("%f ", *ptr2);


printf("%d", ptr2 - ptr1);

return 0;
}

5 Ans: 90.500000 3

6.
int f(int x, int *py, int **ppz)
{
int y, z;
**ppz += 1;
z = **ppz;
*py += 2;
y = *py;
x += 3;
return x + y + z;
}
void main()
{
int c, *b, **a;
c = 4;
b = &c;
a = &b;
printf("%d ", f(c, b, a));
return 0;
}

6 Ans:19

7.
#include<stdio.h>
void f(int *p, int *q)
{
p = q;
*p = 2;
}
int i = 0, j = 1;
Page 3 of 7
int main()
{
f(&i, &j);
printf("%d %d n", i, j);
getchar();
return 0;
}
7 Ans: 0 2

Pointers and Multidimensional Arrays


Consider pointer notation for the two-dimensional numeric arrays. consider the
following declaration
int nums[2][3] = { {16, 18, 20}, {25, 26, 27} };

In general, nums[i][j] is equivalent to *(*(nums+i)+j)

POINTER NOTATION ARRAY NOTATION VALUE

*(*nums) nums[0][0] 16

*(*nums + 1) nums[0][1] 18

*(*nums + 2) nums[0][2] 20

*(*(nums + 1)) nums[1][0] 25

*(*(nums + 1) + 1) nums[1][1] 26

*(*(nums + 1) + 2) nums[1][2] 27

Page 4 of 7
Example 1: Pointers and Arrays

#include <stdio.h>
int main() {
int i, x[6], sum = 0;
printf("Enter 6 numbers: ");
for(i = 0; i < 6; ++i) {
// Equivalent to scanf("%d", &x[i]);
scanf("%d", x+i);

// Equivalent to sum += x[i]


sum += *(x+i);
}
printf("Sum = %d", sum);
return 0;
}

When you run the program, the output will be:

Enter 6 numbers: 2
3
4
4
12
4
Sum = 29

Example 2: Arrays and Pointers

#include <stdio.h>
int main() {
int x[5] = {1, 2, 3, 4, 5};
int* ptr;

// ptr is assigned the address of the third element


ptr = &x[2];

printf("*ptr = %d \n", *ptr); // 3


printf("*(ptr+1) = %d \n", *(ptr+1)); // 4
printf("*(ptr-1) = %d", *(ptr-1)); // 2

return 0;

Page 5 of 7
}

When you run the program, the output will be:

*ptr = 3
*(ptr+1) = 4
*(ptr-1) = 2

Example 3: Pass Addresses to Functions

#include <stdio.h>
void swap(int *n1, int *n2);

int main()
{
int num1 = 5, num2 = 10;

// address of num1 and num2 is passed


swap( &num1, &num2);

printf("num1 = %d\n", num1);


printf("num2 = %d", num2);
return 0;
}

void swap(int* n1, int* n2)


{
int temp;
temp = *n1;
*n1 = *n2;
*n2 = temp;
}

When you run the program, the output will be:

num1 = 10
num2 = 5

The address of num1 and num2 are passed to the swap() function using swap(&num1,
&num2); .

Page 6 of 7
Pointers n1 and n2 accept these arguments in the function definition.

Example 4: Passing Pointers to Functions

#include <stdio.h>

void addOne(int* ptr) {


(*ptr)++; // adding 1 to *ptr
}

int main()
{
int* p, i = 10;
p = &i;
addOne(p);

printf("%d", *p); // 11
return 0;
}

Here, the value stored at p , *p , is 10 initially.


We then passed the pointer p to the addOne() function. The ptr pointer gets this
address in the addOne() function.
Inside the function, we increased the value stored at ptr by 1 using (*ptr)++; .
Since ptr and p pointers both have the same address, *p inside main() is also 11.

Page 7 of 7

You might also like