Docsity

Practice problems on pointers, Exercises of Computer Science

Toggle sidebar
bg1
06/09/2017
Malnad College of Engineering
Department of Computer Science and Engineering
Tutorial Class - 1
Subject : Data Structures (CS304)
Course Outcome covered : CO1 - Illustrate dynamic memory allocation using C constructs
Program outcome mapped : PO1, PO3,PSO1
Work Sheet
1 . What is the output of the following program segment?
int count = 10, *temp, sum = 0;
temp = &count;
*temp = 20;
temp = ∑
*temp = count;
printf("count = %d, *temp = %d, sum = %d\n",
count, *temp, sum );
#include<stdio.h>
void fun(int arr[])
{
int i;
int arr_size = sizeof(arr)/sizeof(arr[0]);
for (i = 0; i < arr_size; i++)
printf("%d ", arr[i]);
}
# include <stdio.h>
void fun(int *ptr)
{ *ptr = 30; }
int main()
{
int y = 20;
fun(&y);
printf("%d", y);
return 0;
}
int main()
{
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;
}
#include<stdio.h>
int main()
{
int arr[] = {10, 20, 30, 40, 50, 60};
#include<stdio.h>
int main()
{
int a;
pf3
pf4
pf5

Partial preview of the text

Download Practice problems on pointers and more Exercises Computer Science in PDF only on Docsity!

Malnad College of Engineering

Department of Computer Science and Engineering

Tutorial Class - 1

Subject : Data Structures (CS304)

Course Outcome covered : CO1 - Illustrate dynamic memory allocation using C constructs

Program outcome mapped : PO1, PO3,PSO

Work Sheet

  1. What is the output of the following program segment?

int count = 10, *temp, sum = 0;

temp = &count;

*temp = 20;

temp = ∑

*temp = count;

printf("count = %d, *temp = %d, sum = %d\n", count, *temp, sum );

#include

void fun(int arr[])

{

int i;

int arr_size = sizeof(arr)/sizeof(arr[0]);

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

printf("%d ", arr[i]);

}

include

void fun(int *ptr)

{ *ptr = 30; }

int main()

{

int y = 20;

fun(&y);

printf("%d", y);

return 0;

}

int main()

{

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;

}

#include

int main()

{

int arr[] = {10, 20, 30, 40, 50, 60};

#include

int main()

{

int a;

int *ptr1 = arr;

int *ptr2 = arr + 5;

printf("Number of elements between two pointer are: %d.", (ptr2 - ptr1));

printf("Number of bytes between two pointers are: %d", (char)ptr2 - (char) ptr1);

return 0;

char *x;

x = (char *) &a;

a = 512;

x[0] = 1;

x[1] = 2;

printf("%d\n",a);

return 0;

int main()

{

int i;

int arr[4] = {10, 20 ,30, 40};

fun(arr);

return 0;

}

#include void f(int *p, int *q) { p = q; *p = 2; } int i = 0, j = 1; int main() { f(&i, &j); printf("%d %d \n", i, j); getchar(); return 0; }

#include int main() { int a = 12; void *ptr = (int *)&a; printf("%d", *ptr); getchar(); return 0; }

#include int main() { int arr[] = {1, 2, 3, 4, 5}; int p = arr; ++p; p += 2; printf("%d", *p); return 0; }

void swap (char *x, char *y) { char *t = x; x = y; y = t; } int main() { char *x = "geeksquiz"; char *y = "geeksforgeeks"; char *t; swap(x, y); printf("(%s, %s)", x, y); t = x; x = y; y = t; printf("\n(%s, %s)", x, y); return 0; }

int main() { char blocks[3] = {'A','B','C'}; char *ptr = &blocks[0]; char temp; temp = blocks[0]; temp = *(blocks + 2); temp = *(ptr + 1); temp = *ptr; ptr = blocks + 1; temp = *ptr; temp = *(ptr + 1); ptr = blocks; temp = ++ptr; temp = ++ptr; temp = *ptr++; temp = *ptr; return 0; }

#include void f(int *p, int *q) { p = q; *p = 2;

void printxy(int x, int y) { int *ptr; x = 0; ptr = &x;

(*ptr)++;

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

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

return 0;

}

void f(int* p, int m) { m = m + 5; *p = *p + m; return; } void main() { int i=5, j=10; f(&i, j); printf("%d", i+j); }

#include void main() { int a[3] = {1, 2, 3}; int *p = a; printf("%p\t%p", p, a); }

#include void main() { char *s = "hello"; char *p = s; printf("%p\t%p", p, s); }

#include void main() { char *s= "hello"; char *p = s; printf("%c\t%c", p[0], s[1]); }

#include void main() { char *s= "hello"; char *p = s; printf("%c\t%c", *(p + 3), s[1]); }

#include int main() { int ary[4] = {1, 2, 3, 4}; int *p = ary + 3; printf("%d\n", p[-2]); }

#include int main() { int ary[4] = {1, 2, 3, 4}; int p = ary + 3; printf("%d %d\n", p[-2], ary[p]); }

#include int main() { int ary[4] = {1, 2, 3, 4}; printf("%d\n", *ary); }

#include int main() { const int ary[4] = {1, 2, 3, 4}; int *p; p = ary + 3; *p = 5; printf("%d\n", ary[3]); }

#include int main() { int ary[4] = {1, 2, 3, 4}; int p[4]; p = ary; printf("%d\n", p[1]); }