6
Pointer Example

Pointer example

Embed Size (px)

Citation preview

Page 1: Pointer example

Pointer Example

Page 2: Pointer example

1. What will be the output of the following code?#include<stdio.h>int func(int *p, int q){ *p =(*p)*q; q =q*q; return q;}int main(){ int m=5, n=6, o=2; o=func(&m, n); printf("M=%d N=%d O=%d",m,n,o);

return 0;}

M=30 N=6 O=36

Page 3: Pointer example

2. What will be the output of the following code?

#include<stdio.h>

int* func(int *p, int *q){ *p =(*p)*(*q); *q =(*q)*(*q); return q;}int main(){ int m=5, n=6; int *o=NULL; o=func(&m, &n); printf("M=%d N=%d O=%d",m,n,*o); return 0;}

M=30 N=36 O=36

Page 4: Pointer example

3. What will be the output of the following code?

#include<stdio.h>

int fun(int *m,int n);

int main(){ int p=4,q=2,r=0; r=fun(&p,q); printf("P=%d Q=%d R=%d",p,q,r); return 0;}int fun(int *m,int n){ (*m)++; n++;return *m+n;}

P=5 Q=2 R=8

Page 5: Pointer example

4. What will be the output of the following code?

#include<stdio.h>

int fun(int m,int *n);

int main(){ int p=5,q=3,r=1; r=fun(p,&q); printf("P=%d Q=%d R=%d",p,q,r); return 0;}int fun(int m,int *n){ m++; ++(*n);return m+*n;}

P=5 Q=4 R=10

Page 6: Pointer example

5. What will be the output of the following code?

#include<stdio.h>

int main(){char Str[]= "Best of Luck"; char *ptr;ptr = Str; printf("\n1. %s", ptr); printf("\n 2.%s", ++ptr); Str[8] = '\0'; printf("\n 3.%s", Str); printf("\n 4.%s", ++ptr);

return 0;}

1. Best of Luck2. est of Luck3. best of 4. st of