C Programming

C Programming

Pointers has 50 Questions

Point out the compile time error in the program given below.
#include<stdio.h>

int main()
{
    int *x;
    *x=100;
    return 0;
}

Error: invalid assignment for x
Error: suspicious pointer conversion
No error
None of above
Which of the following statements correctly declare a function that receives a pointer to pointer to a pointer to a float and returns a pointer to a pointer to a pointer to a pointer to a float?

float **fun(float***);
float *fun(float**);
float fun(float***);
float ****fun(float***);
Are the expression *ptr++ and ++*ptr are same?

True
False
Is there any difference between the following two statements?
char *p=0;
char *t=NULL;

Yes
No
What is (void*)0?

Representation of NULL pointer
Representation of void pointer
Error
None of above
What will be the output of the program ?
#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;
}

ink
ack
ite
let
Point out the error in the program
#include<stdio.h>

int main()
{
    int a[] = {10, 20, 30, 40, 50};
    int j;
    for(j=0; j<5; j++)
    {
        printf("%d", a);
        a++;
    }
    return 0;
}

Error: Declaration syntax
Error: Expression syntax
Error: LValue required
Error: Rvalue required
Which of the statements is correct about the program?
#include<stdio.h>

int main()
{
    int i=10;
    int *j=&i;
    return 0;
}

j and i are pointers to an int
i is a pointer to an int and stores address of j
j is a pointer to an int and stores address of i
j is a pointer to a pointer to an int and stores address of i
Will the program compile?
#include<stdio.h>
int main()
{
    char str[5] = "ASTIWZ";
    return 0;
}

True
False
Is this a correct way for NULL pointer assignment?
int i=0;
char *q=(char*)i;

Yes
No
Can you combine the following two statements into one?
char *p;
p = (char*) malloc(100);

char p = *malloc(100);
char *p = (char) malloc(100);
char *p = (char*)malloc(100);
char *p = (char *)(malloc*)(100);
What will be the output of the program ?
#include<stdio.h>

int main()
{
    int i=3, *j, k;
    j = &i;
    printf("%d", i**j*i+*j);
    return 0;
}

30
27
9
3
Which of the statements is correct about the program?
#include<stdio.h>

int main()
{
    float a=3.14;
    char *j;
    j = (char*)&a;
    printf("%d", *j);
    return 0;
}

It prints ASCII value of the binary number present in the first byte of a float variable a.
It prints character equivalent of the binary number present in the first byte of a float variable a.
It will print 3
It will print a garbage value
The following program reports an error on compilation.
#include<stdio.h>
int main()
{
    float i=10, *j;
    void *k;
    k=&i;
    j=k;
    printf("%f", *j);
    return 0;
}

True
False
Is the NULL pointer same as an uninitialised pointer?

Yes
No
In which header file is the NULL macro defined?

stdio.h
stddef.h
stdio.h and stddef.h
stdlib.h
What will be the output of the program ?
#include<stdio.h>

int main()
{
    int x=30, *y, *z;
    y=&x; /* Assume address of x is 500 and integer is 4 byte size */
    z=y;
    *y++=*z++;
    x++;
    printf("x=%d, y=%d, z=%d", x, y, z);
    return 0;
}

x=31, y=502, z=502
x=31, y=500, z=500
x=31, y=498, z=498
x=31, y=504, z=504
In the following program add a statement in the function fun() such that address of a gets stored in j?
#include<stdio.h>
int main()
{
    int *j;
    void fun(int**);
    fun(&j);
    return 0;
}
void fun(int **k)
{
    int a=10;
    /* Add a statement here */
}

**k=a;
k=&a;
*k=&a
&k=*a
Are the three declarations char **apple, char *apple[], and char apple[][] same?

True
False
Will the program compile in Turbo C?
#include<stdio.h>
int main()
{
    int a=10, *j;
    void *k;
    j=k=&a;
    j++;
    k++;
    printf("%u %u", j, k);
    return 0;
}

Yes
No
How many bytes are occupied by near, far and huge pointers (DOS)?

near=2 far=4 huge=4
near=4 far=8 huge=8
near=2 far=4 huge=8
near=4 far=4 huge=8
What will be the output of the program ?
#include<stdio.h>

int main()
{
    char str[20] = "Hello";
    char *const p=str;
    *p='M';
    printf("%s", str);
    return 0;
}

Mello
Hello
HMello
MHello
Which of the following statements correct about k used in the below statement?
char ****k;

k is a pointer to a pointer to a pointer to a char
k is a pointer to a pointer to a pointer to a pointer to a char
k is a pointer to a char pointer
k is a pointer to a pointer to a char
Will the following program give any warning on compilation in TurboC (under DOS)?
#include<stdio.h>

int main()
{
    int *p1, i=25;
    void *p2;
    p1=&i;
    p2=&i;
    p1=p2;
    p2=p1;
    return 0;
}

Yes
No
If a variable is a pointer to a structure, then which of the following operator is used to access data members of the structure through the pointer variable?

'.'
'&'
'*'
'->'
What will be the output of the program If the integer is 4bytes long?
#include<stdio.h>

int main()
{
    int ***r, **q, *p, i=8;
    p = &i;
    q = &p;
    r = &q;
    printf("%d, %d, %d", *p, **q, ***r);
    return 0;
}

8, 8, 8
4000, 4002, 4004
4000, 4004, 4008
4000, 4008, 4016
Which of the statements is correct about the program?
#include<stdio.h>

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

Output: Garbage value
Output: 1
Output: 3
Error: Invalid indirection
What would be the equivalent pointer expression for referring the array element a[i][j][k][l]

((((a+i)+j)+k)+l)
*(*(*(*(a+i)+j)+k)+l)
(((a+i)+j)+k+l)
((a+i)+j+k+l)
What will be the output of the program ?
#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", **q);
}

Error: cannot convert from void** to int**
Garbage value
0
No output
Which statement will you add to the following program to ensure that the program outputs "ASTIWZ" on execution?
#include<stdio.h>

int main()
{
    char s[] = "ASTIWZ";
    char t[25];
    char *ps, *pt;
    ps = s;
    pt = t;
    while(*ps)
        *pt++ = *ps++;

    /* Add a statement here */
    printf("%s", t);
    return 0;
}

*pt='';
pt='';
pt='';
*pt='';
A pointer is

A keyword used to create variables
A variable that stores address of an instruction
A variable that stores address of other variable
All of the above
What will be the output of the program ?
#include<stdio.h>

int main()
{
    char *str;
    str = "%s";
    printf(str, "K");
    return 0;
}

Error
No output
K
%s
In the following program add a statement in the function fact(). such that the factorial gets stored in j.
#include<stdio.h>
void fact(int*);

int main()
{
    int i=5;
    fact(&i);
    printf("%d", i);
    return 0;
}
void fact(int *j)
{
    static int s=1;
    if(*j!=0)
    {
        s = s**j;
        *j = *j-1;
        fact(j);
        /* Add a statement here */
    }
}

j=s;
*j=s;
*j=&s;
&j=s;
The operator used to get value at address stored in a pointer variable is

*
&
&&
||
What will be the output of the program ?
#include<stdio.h>
int *check(static int, static int);

int main()
{
    int *c;
    c = check(10, 20);
    printf("%d", c);
    return 0;
}
int *check(static int i, static int j)
{
    int *p, *q;
    p = &i;
    q = &j;
    if(i >= 45)
        return (p);
    else
        return (q);
}

10
20
Error: Non portable pointer conversion
Error: cannot use static for function parameters
What will be the output of the program if the size of pointer is 4-bytes?
#include<stdio.h>

int main()
{
    printf("%d, %d", sizeof(NULL), sizeof(""));
    return 0;
}

2, 1
2, 2
4, 1
4, 2
What will be the output of the program ?
#include<stdio.h>

int main()
{
    void *vp;
    char ch=74, *cp="JACK";
    int j=65;
    vp=&ch;
    printf("%c", *(char*)vp);
    vp=&j;
    printf("%c", *(int*)vp);
    vp=cp;
    printf("%s", (char*)vp+2);
    return 0;
}

JCK
J65K
JAK
JACK
What will be the output of the program?
#include<stdio.h>

int main()
{
    int arr[2][2][2] = {10, 2, 3, 4, 5, 6, 7, 8};
    int *p, *q;
    p = &arr[1][1][1];
    q = (int*) arr;
    printf("%d, %d", *p, *q);
    return 0;
}

8, 10
10, 2
8, 1
Garbage values
What will be the output of the program assuming that the array begins at the location 1002 and size of an integer is 4 bytes?
#include<stdio.h>

int main()
{
    int a[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
    printf("%u, %u, %u", a[0]+1, *(a[0]+1), *(*(a+0)+1));
    return 0;
}

448, 4, 4
520, 2, 2
1006, 2, 2
Error
What will be the output of the program?
#include<stdio.h>

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

2, 3
2, 0
2, Garbage value
0, 0
What will be the output of the program ?
#include<stdio.h>

int main()
{
    char *str;
    str = "%d";
    str++;
    str++;
    printf(str-2, 300);
    return 0;
}

No output
30
3
300
What will be the output of the program ?
#include<stdio.h>

int main()
{
    printf("%c", 7["ASTIWZ"]);
    return 0;
}

Error: in printf
Nothing will print
print "X" of ASTIWZ
print "7"
What will be the output of the program ?
#include<stdio.h>

int main()
{
    char str[] = "peace";
    char *s = str;
    printf("%s", s++ +3);
    return 0;
}

peace
eace
ace
ce
What will be the output of the program ?
#include<stdio.h>

int main()
{
    char *p;
    p="hello";
    printf("%s", *&*&p);
    return 0;
}

llo
hello
ello
h
What will be the output of the program assuming that the array begins at location 1002?
#include<stdio.h>

int main()
{
    int a[2][3][4] = { {1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 1, 2}, 
                       {2, 1, 4, 7, 6, 7, 8, 9, 0, 0, 0, 0} };
    printf("%u, %u, %u, %d", a, *a, **a, ***a);
    return 0;
}

1002, 2004, 4008, 2
2004, 4008, 8016, 1
1002, 1002, 1002, 1
Error
What will be the output of the program ?
#include<stdio.h>
power(int**);
int main()
{
    int a=5, *aa; /* Address of 'a' is 1000 */
    aa = &a;
    a = power(&aa);
    printf("%d", a);
    return 0;
}
power(int **ptr)
{
    int b;
    b = **ptr***ptr;
    return (b);
}

5
25
125
Garbage value
What will be the output of the program ?
#include<stdio.h>

int main()
{
    char str1[] = "Astiwz";
    char str2[] = "QUIZ";
    char *s1 = str1, *s2=str2;
    while(*s1++ = *s2++)
        printf("%s", str1);

    printf("");
    return 0;
}

ASTIWZ
BndiaBIdiaQUIZia
Astiwz
(null)
What will be the output of the program ?
#include<stdio.h>
#include<string.h>

int main()
{
    int i, n;
    char *x="Alice";
    n = strlen(x);
    *x = x[n];
    for(i=0; i<=n; i++)
    {
        printf("%s ", x);
        x++;
    }
    printf("", x);
    return 0;
}

Alice
ecilA
Alice lice ice ce e
lice ice ce e
What will be the output of the program ?
#include<stdio.h>

int main()
{
    int i, a[] = {2, 4, 6, 8, 10};
    change(a, 5);
    for(i=0; i<=4; i++)
        printf("%d, ", a[i]);
    return 0;
}
change(int *b, int n)
{
    int i;
    for(i=0; i<n; i++)
        *(b+1) = *(b+i)+5;
}

7, 9, 11, 13, 15
2, 15, 6, 8, 10
2 4 6 8 10
3, 1, -1, -3, -5
If the size of integer is 4bytes, What will be the output of the program?
#include<stdio.h>

int main()
{
    int arr[] = {12, 13, 14, 15, 16};
    printf("%d, %d, %d", sizeof(arr), sizeof(*arr), sizeof(arr[0]));
    return 0;
}

10, 2, 4
20, 4, 4
16, 2, 2
20, 2, 2