0% found this document useful (0 votes)
2 views18 pages

Pointers Practice Sheet

The document is a comprehensive pointers practice sheet containing various questions and programming tasks related to pointers in C. It covers topics such as pointer definitions, array manipulation, string operations, function calls using pointers, and error prediction in code snippets. The sheet includes numerous programming exercises aimed at enhancing understanding and application of pointers.
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)
2 views18 pages

Pointers Practice Sheet

The document is a comprehensive pointers practice sheet containing various questions and programming tasks related to pointers in C. It covers topics such as pointer definitions, array manipulation, string operations, function calls using pointers, and error prediction in code snippets. The sheet includes numerous programming exercises aimed at enhancing understanding and application of pointers.
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/ 18

Pointers Practice Sheet

1.What do you mean by pointer? Why do we need it? Explain how data is link with
pointer with suitable examples.
2. Array name is considered a constant pointer. Justify this statement.
3. Explain how array elements will be accessed using an external pointer?
4. Write a menu driven C Program to perform the following operations using pointer
on a one Dimensional array of size 8.
a) Count how many even numbers are in this array.
b) Print all odd numbers in this array.
c) Search a number entered by user.
d) Delete a number from this array.
5. How we can access, use and print every character of a string using pointer? Explain
with suitable example.
6. Write a menu Driven C Program to perform the following operations on String
using pointer:
a) Find length of a string.
b) Reverse the string.
c) Check for string palindrome.
d) Count a particular character entered by user.
7. Explain every statements of this program: Write Output if statement is correct if not
write the reason behind it.
void main()
{
char s[30]=”HELLO TO GLA UNIVERSITY”
printf(“%c, s+2);
printf(“%c”, *(s+3));
printf(“%s”, s+2);
printf(“%d”, s+4);
printf(“%d”, *(s+4));
printf(“%s”, s+4);
printf(“%s”, *(s+4));
}

8. Differentiate array of pointer and pointer to an array using suitable example.


9. Discuss the operators which can work with pointer with suitable examples.
10. Can subtraction works within two pointers? Justify your answer.
11. I am having 4 different types of variables in my programs used independently, if I
want to access them using pointer, how many minimum pointer I can use? Justify
your answer with suitable example.
12. What do you mean by pointer to pointer? Write how double pointer declared and
initialized?
13. Write a program to swap two numbers using double pointer.
14. What are the benefits of calling a function using reference over calling a function
using values?
15. WAP to find largest among three numbers using pointer.
16.WAP to find factorial of a number using pointer.
17.An employee in a company works 10 hours per day and gets 10,000 salary
in a week. Write a program using pointer to read working hours per day for a
week and calculate total working hours and find total salary paid to the
employee based on following conditions.
Overtime(Hrs) Bonus

<5 5000

6-10 10000

>10 15000

18. WAP to compute simple interest using pointers.


19. A train 240 m in length crosses a telegraph post in 16 seconds. Write a
C program to find the speed of a train in km/hr.
20. WAP to search an element in an array using pointers.
21. WAP to sort elements of an array using pointers.
22. WAP that print the position of largest element in an array using
pointer.
23. WAP to categorize each element of an array as prime or not using
pointer.
24. There are two students Ria and Sia, store their 5 subject marks in two
different array. Write a C program to find who scored more in
individual subject as well as in average using pointer.
25. Write a c program to find frequency of a character in a string entered
by user using pointer.
26. There is a string entered by the user of length at least 10. We have to
delete all the characters from that string except ‘A’ and store it in same
array in sequence. Write a C program to perform this task using
pointers.
27. Write a program to print largest even number present in an array
using pointer to an array.
28. WAP to find sum of elements of an array using array of pointer.
29. A certain piece of text is entered. By mistake, at some places two or
more spaces are placed between two words. Write a c program using
function with pointer that removes these extra spaced between the
words.
30. Write a C program to check weather a given number is palindrome or
not using function (call by reference).
31. Write a C program using pointer and function to read marks of 8
subjects of a student. Implement the FIND and REPLACE functionality
such that FIND will display the occurrence of a given number n and
REPLACE will replace that number n with m.
32. Write a C program to find sum of first n terms of GP (Geometric
Progression) using function and pointer. Do not use any built-in
function. The value of a, n and r must be entered by user using
a∗(r n−1)
formula:
(r −1)
Find out any error in the following code (1-4):

1. void main()
{
int *ptr=10;
printf(“the value of ptr is %d”,*ptr);
}

2. void main()
{
int a=5, *b=&a;
printf(“d”,a*b);
}

3. void main()

char *c;

float x=10;

c=&x;

printf(“%d”,*c);

4. void main()

int a=10;

float *p=&a;

printf(“%d%d”,a,*p);

what will be the output of following code (5-10):

5. void main()

{
int a=5, *b=&a;
printf(“d”,a**b);
}

6. void main()
{
int a;
*&a=50;
printf(“%d”,a);
}

7. void main()

{
int i=50;
int * j=&i;
printf(“%d”,++(*j));
}

8. void main()

{
int x,*p;
p=&x;
*p=2;
printf(value of x is”,x);
}

9. void main()

int a=10;

int *ptr=&a;

printf(“%d %d”,++*ptr,*ptr++);

10. void main()

int num=5,*p=&num,x=*p;

printf(“%d %d %d”,++num,x+2,*p--);

11. What are the advantages and limitations of pointers?


12. What is difference between a.) *p++ and p++ b.) p and *p.
13. What is the purpose of including data type at the time of declaration of pointer?
14. What is the similarity between array name and pointer?
15. Is the following statement valid? Justify your answer. m=(float *)&p;
What will be the output of following programs?

16. #include <stdio.h>


void main()

char s[] = "hello";

printf("%c\n", *(s+1));

printf("%c\n", *s+1);

printf("%s\n", *(s+1));

17. #include <stdio.h>

void main()

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

int *p;

p = ary + 3;

*p = 5;

printf("%d\n", ary[3]);

18. #include <stdio.h>

void main()

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

int *p = ary + 3;

printf("%d\n", p[-2]);

}
6. #include <stdio.h>

void main()

char *str = "hello world";

char strc[] = "good morning india\n";

strcpy(strc, str);

printf("%s\n", strc);

7. #include <stdio.h>
int main()
{
char a[2][6] = {"hello", "hi"};
printf("%d", sizeof(a));
return 0;
}

8. #include <stdio.h>

int main()

char *a[2] = {"hello", "hi"};

printf("%s", *(a + 1));

return 0;

9. #include<stdio.h>
main()
{
int a[2][3]={ 10,20,30,40,50,60};
int i,j;
for(i=0;i<3;++i)
{
printf(“\n”);
for(j=0;j<3;++j)
printf(“%d”,*(*a+i)+j));
}
}

10. #include<stdio.h>

void main()

char str[]=”ABCDEF”;

printf(“%d”,(&str[3]-&str[0]);

Predict output/error of following program (1-8):

1. #include<stdio.h>
int main()
{
char str[] = "peace";
char *s = str;
printf("%s\n", s++ +3);
return 0;
}

2. #include<stdio.h>
int main()
{
char str1[] = "India";
char str2[] = "BIX";
char *s1 = str1, *s2=str2;
while(*s1++ = *s2++)
printf("%s", str1);
printf("\n");
return 0;
}

3. #include<stdio.h>
int main()
{
static char *s[] = {"black", "white", "pink", "violet"};
char **ptr[] = {s+3, s+2, s+1, s}, ***p;
p = ptr;
++p;
printf("%s", **p+1);
return 0;
}

4. #include<stdio.h>

void main()

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

int *p=a;

int *p2;

p2=p*2;

printf(“%p”,p2);

5. #include<stdio.h>

void main()

int a,*b,**c;

a=6;

**c=20;

*b=**c;

printf(“%d %d %d”,a,*b,**c);

6. #include<stdio.h>

void main()

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

int *p,y;

p=x+4;
y=p-x;

printf(“%d”,y);

7. #include<stdio.h>

int main ()

int var[] = {10, 100, 200};

int i, *ptr;

ptr = var;

i = 0;

while ( ptr <= &var[2] )

printf(“%d\t”,*ptr);

ptr++;

i++;

return 0;

8. #include<stdio.h>

void main()

char *x[ ] = {"hello", "goodbye", "so long", "thanks for all the fish"};

char *y;

int i;

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

{
y = x[i]; while(*y!='\0')

printf("%c", *y); y++;

printf("\n");

9 . What is the difference *p[5] and (*p)[5];

10. What is the meaning of following expressions?

a. *(a+3) b. *(*(a+1)+3) c. *(a[3]+2) d. [2]a e. (3[a]+4)

Predict output of following program-

1. #include <stdio.h>

void f(char *k)

k++;

k[2] = 'm';

void main()

char s[] = "hello";

f(s);

printf("%c\n", *s);

2. #include <stdio.h>

int * function();
void main()

auto int *x;

int *(*ptr)();

ptr=&function;

x=(*ptr)();

printf("%d",*x);

int *function()

static int a=10;

return &a;

3. #include <stdio.h>

void fun(void *p);


int i;
int main()
{

void *vptr;

vptr = &i;

fun(vptr);

return 0;

void fun(void *p)

int **q;

q = (int**)&p;

printf("%d\n", **q);

}
4. #include<stdio.h>

int Sum(int a,int b, int *sum,int *diff)

*sum=a+b;

*diff=a-b;

void main()

int a=10,b=20,sum,diff;

Sum(a,b,&sum,&diff);

printf(“sum is %d and difference is %d”,sum,diff);

5. #include <stdio.h>

int mul(int a, int b, int c)

return a * b * c;

void main()

int (*function_pointer)(int, int, int);

function_pointer = mul;

printf("The product of three numbers is:%d",

function_pointer(2, 3, 4));

6. #include <stdio.h>

int add(int a, int b)


{

return a+b;

void main()

int (*ptr)(int, int);

ptr=add;

printf(%d\n”,ptr(2,3));

printf(“%d”,(*ptr)(2,3);

7. #include <stdio.h>

int retMax (int n1, int n2)

return (n1 > n2) ? n1: n2;

int main ()

int (*ptrMaxFunctin)(int, int);

ptrMaxFunctin = retMax;

int qty1 = 20, qty2 = 50;

printf ("Max of %d and %d is : %d \n", qty1, qty2, (*ptrMaxFunctin)(qty1, qty2));

return 0;

}
8. #include<stdio.h>

int add(int a,int b)

return a+b;

int sub(int a,int b)

return a-b;

int mul(int a,int b)

return a*b;

int div(int a,int b)

return a/b;

void main()

int (*ptr[4])(int,int)={add,sub,mul,div};

int I;

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

printf(“The result of calling fuction %d is %d”,i+1, ptr[i](10,5);

What will be the output/error of the following code?

1. #include <stdio.h> void main()


{ strcat(p,”World”);

int *p; puts(p);

p=(int *)malloc(sizeof(int)); }

*p=100; 5. #include <stdio.h>

printf(“The value of p is %d”, *p); int main()

} {

2. #include <stdio.h> int *p;

void main() p = (int *)malloc(20); /* Assume p has


address of 1314 */
{
free(p);
char *p;
printf("%u", p);
p=(char *)malloc(6);
return 0;
strcpy(p,”Hello”);
}
puts(p);
6. #include <stdio.h>
}
void main()
3. #include <stdio.h>
{
void main()
int a=10,*p=&a;
{
void *v=&a;
int *p;
*p++;
p=(int *)calloc(1, sizeof(int));
*v++;
printf(“The value of p is %d”, *p);
printf(“%d %d”, *p,*v);
}
}
4. #include <stdio.h>

void main()
7. #include<stdio.h>
{
void main()
char *p;
{
p=(char *)malloc(6);
int a=10;
strcpy(p,”Hello”);
void *p=&a;
puts(p);
int *ptr=p;
p=(char *)realloc(p,15);
printf(“%u”,*ptr); printf(“Hello\n”);

} if(r)

8. #include <stdio.h> printf(“r”);

void main() else

{ printf(“world\n”);

char *p=NULL; }

char *r=0;

if(p) 9. What is difference between


uninitialized pointer and Null pointer?
printf(“p”);
10. What is dangling pointer?
else

You might also like