2,527 views
3 3 votes
What is the output of the following C program

void main( )
{
static char a[5][20] = {“Mona”, “Vijay”, “Kumar”, “Ankur”, “Suresh”};
int i;
char *p;
p = a[3];
a[3] = a[4];
a[4] = p;
for(i = 0; i <= 4; i++)
printf(“%s”, a[i]);
}

1 Answer

Best answer
4 4 votes

Array is a constant pointer. So, as a restriction of constant pointer it cannot change it's value like a general pointer.

i.e., we cannot change the value of a constant pointer like

a[3] = a[4];

This will give compile time error.

If we want to write this code without any compile time error it can be done as follows:

#include<stdio.h>

int main(void) {
        static char* a[20] = {"Mona", "Vijay", "Kumar", "Ankur", "Suresh"};
        int i;
        char *temp = a[4];
        a[4] = a[3];
        a[3]=temp;
        for(i = 0; i <= 4; i++)
                printf("%s", a[i]);
        return 0;
}
• selected by
Position:
Show:

Related questions

0 0 votes
1 1 answer
1.7k
1.7k views
Psnjit asked Jan 12, 2019
1,734 views
main(){unsigned int i= 255;char *p= &i;int j= *p;printf("%d\n", j);unsigned int k= *p;printf("%d", k);} Both the outputs are -1. I have even tried with - int i = 255(3rd ...
0 0 votes
1 1 answer
1.9k
1.9k views
Mr khan 3 asked Nov 3, 2018
1,882 views
#include<stdio.h void fun(int *p,int *q) { p=q; *p=q; } int i=0,j=1; int main() { fun(&i,&j); printf("%d%d",i,j); }What will be the output of i and j in 16-bit C Compiler...
0 0 votes
1 1 answer
1.6k
1.6k views
sushmita asked Sep 30, 2018
1,587 views
What is the interpretation of following C declaration:char *(*( foo[][8])())[];(A) foo is array of 8 pointer to pointer to function returning pointer to array of pointer ...