Pointer and Structure in C · Programming Languages · GATE CSE
Marks 1
Consider the following C function definition.
int fX(char *a) {
char *b = a;
while(*b)
b++;
return b - a; }
Which of the following statements is/are TRUE?
What is printed by the following ANSI C program?
#include <stdio.h>
int main(int argc, char *argv[ ]) {
int x = 1, z[2] = {10, 11};
int *p = NULL;
p = &x;
*p = 10;
p = &z[1];
*(&z[0] + 1) += 3;
printf("%d, %d, %d\n", x, z[0], z[1]);
return 0;
}
Consider the following ANSI C program.
#include<stdio.h>
int main(){
int arr[4][5];
int i, j;
for(i =0; i<4; i++){
for (j =0; j<5; j++){
arr [i][j] = 10*i + j;
}
}
print("%d", *(arr[1] + 9));
return 0;
}
What is the output of the above program?
#include < stdio.h >
int main () {
int arr [] = {1,2,3,4,5,6,7,8,9,0,1,2,5}, *ip = arr+4;
printf ("%d\n", ip[1]);
return 0;
}
The number that will be displayed on execution of the program is _______.
#include< stdio.h >
struct Ournode{
char x,y,z;
};
int main(){
struct Ournode p = {'1', '0', 'a'+2};
struct Ournode *q = &p;
printf ("%c, %c", *((char*)q+1), *((char*)q+2));
return 0;
}
The output of this program is:void f(int, short);
void main()
{
int i = 100;
short s = 12;
short *p = &s;
__________ ; // call to f()
}
Which one of the following expressions, when placed in the blank above, will NOT result in
a type checking error?#include < stdio.h >
void mystery(int *ptra, int *ptrb) {
int *temp;
temp = ptrb;
ptrb = ptra;
ptra = temp;
}
int main() {
int a=2016, b=0, c=4, d=42;
mystery(&a, &b);
if (a < c)
mystery(&c, &a);
mystery(&a, &d);
printf("%d\n", a);
}
The output of the program is _____________.
void f1(int a, int b) {
int c;
c=a; a=b; b=c;
}
void f2(int *a, int *b) {
int c;
c=*a; *a=*b; *b=c;
}
int main(){
int a=4, b=5, c=6;
f1(a,b);
f2(&b, &c);
printf(“%d”,c-a-b);
}
char c[ ] = "GATE2011";
char *p = c;
printf("%s", p + p[3] - p[1]);
#include < stdio.h >
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);
return 0;
}
int (*f)(int *);
void swap (int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
}
In order to exchange the values of two variables x and y.int * A[10], B[10][10];
Of the following expressionsI. A[2]
II. A[2] [3]
III. B[1]
IV. B[2] [3]
Which will not give compile-time errors if used as left hand sides of assignment statements in a C program?
X: m=malloc(5); m= NULL;
Y: free(n); n->value = 5;
Z: char *p; *p='a';
1: using dangling
2: using uninitialized pointers
3. lost memory
is:
struct {
short s[5];
union {
float y;
long z;
} u;
}t;
Assume that objects of the type short, float and long occupy 2 bytes, 4 bytes and
8 bytes, respectively. The memory requirement for variable t, ignoring alignment
considerations, isstruct node{
int i:
float j;
};
struct node *s[10];
define s to beMarks 2
What is the output of the following C program?
#include <studio.h>
int main() {
double a[2]={20.0, 25.0}, *p, *q;
p = a;
q = p + 1;
printf("%d,%d", (int)(q - p), (int)(*q - *p));
return 0;}
Consider the following ANSI C program:
#include <stdio.h>
#include <stdlib.h>
struct Node{
int value;
struct Node ⋆next;};
int main(){
struct Node ⋆boxE, ⋆head, ⋆boxN; int index = 0;
boxE = head = (struct Node ⋆) malloc (sizeof(struct Node));
head -> value = index;
for (index = 1; index <=3; index++){
boxN = (struct Node ⋆) malloc(sizeof(struct Node));
boxE -> next = boxN;
boxN -> value = index;
boxE = boxN; }
for (index = 0; index <= 3; index++) {
printf("value at index %d is %d\m", index, head -> value);
head = head -> next;
printf("value at index %d is %d\n", index + 1, head -> value);}}
Which one of the statement below is correct about the program ?
#include < stdio.h >
int main()
{
int a[] = {2, 4, 6, 8, 10} ;
int i, sum = 0, *b = a + 4 ;
for (i = 0; i < 5; i++)
sum = sum + (*b - i) - *(b - i) ;
printf ("%d\n", sum) ;
return 0 ;
}
The output of the above C program is _________.
#include< stdio.h >
void fun1(char *s1, char *s2){
char *tmp;
tmp = s1;
s1 = s2;
s2 = tmp;
}
void fun2(char **s1, char **s2){
char *tmp;
tmp = *s1;
*s1 = *s2;
*s2 = tmp;
}
int main(){
char *str1 = "Hi", *str2 = "Bye";
fun1(str1, str2); printf("%s %s ", str1, str2);
fun2(&str1, &str2); printf("%s %s", str1, str2);
return 0;
}
The output of the program above isint main () {
unsigned int x[4][3] =
{{1,2,3},{4,5,6},{7,8,9},{10,11,12}};
printf(“%u, %u, %u”, x+3, *(x+3), *(x+2)+3);
}
int a, b, c = 0;
void prtFun(void);
main( )
{
static int a = 1; /* Line 1 */
prtFun();
a + = 1;
prtFun();
printf("\n %d %d ", a, b);
}
void prtFun(void)
{
static int a=2; /* Line 2 */
int b=1;
a+ = ++b;
printf("\n %d %d ", a, b);
}
What output will be generated by the given code segment if:
Line 1 is replaced by auto int a = 1;
Line 2 is replaced by register int a = 2;
int a, b, c = 0;
void prtFun(void);
main( )
{
static int a = 1; /* Line 1 */
prtFun();
a + = 1;
prtFun();
printf("\n %d %d ", a, b);
}
void prtFun(void)
{
static int a=2; /* Line 2 */
int b=1;
a+ = ++b;
printf("\n %d %d ", a, b);
}
What output will be generated by the given code segment?#include < stdio.h >
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));
getchar();
}
void swap(int *px, int *py)
{
*px = *px - *py;
*py = *px + *py;
*px = *py - *px;
}
S1: will generate a compilation error S2: may generate a segmentation fault at runtime depending on the arguments passed
S3: correctly implements the swap procedure for all input pointers referring to integers stored in memory locations accessible to the process
S4: implements the swap procedure correctly for some but not all valid input pointers
S5: may add or subtract integers and pointers.
char p[20];
char *s = "string";
int length = strlen(s);
for(i=0; i < length; i++)
p[i] = s[length-i];
printf("%s",p);
The output of the program is
#include < stdio.h >
#define print(x) printf("%d ", x)
int x;
void Q(int z) {
z += x; print(z);
}
void P(int *y) {
int x = *y+2;
Q(x); *y = x-1;
print(x);
}
main(void) {
x = 5;
P(&x);
print(x);
}
The output of this program is[P1] int*g(void)
{
int x=10;
return(&x);
}
[P2] int*g(void)
{
int *px;
*px = 10;
return px;
}
[P3] int*g(void)
{
int *px
px =(int*)malloc (size of (int));
*px = 10;
return px;
}
Which of the above three functions are likely to cause problems with pointers?