564 views
1 1 vote
#include<stdio.h>
int main()  
{  
    static int GATE[]={100,200,300,400,500};  
    static int *p[] = {GATE+2, GATE, GATE+3, GATE+4, GATE+1};  
    int **ptr = p;
    ptr++;
    printf("%d", *ptr);
    return 0;
}
 

 

How answer is 500 , i'm getting garbage or NA.

2 Answers

2 2 votes

In its current form it will not actually print the address of GATE array, because "%d" conversion specifier has been used in the printf statment. %d is used to print integers, not address.

Also C standard doesn't specify what should happen when conversion specifier and argument mismatch, hence running this program may lead to undefined behaviour.

This is from KN King C programming a second approach, 2nd edition.

Hence I commented that this question is stupid.

edited by
1 1 vote
This is printing address of GATE, ie. address of 100.

For actually printing 100, the line should be - printf("%d", **ptr);

So yes, running this program gets address of GATE which looks like a garbage value
Position:
Show:

Related questions

7 7 votes
3 3 answers
377
377 views
GO Classes asked Jun 30
377 views
What is the output of the following code?#include <stdio.h struct Student { int roll; int marks; }; int main() { struct Student s[3] = { {1, 50}, {2, 60}, {3, 70} }; stru...
5 5 votes
2 2 answers
327
327 views
GO Classes asked Jun 29
327 views
What is the output of the following code?#include <stdio.h struct Student { int roll; int marks; }; int main() { struct Student s[3] = { {1, 70}, {2, 80}, {3, 90} }; s .m...
8 8 votes
3 3 answers
431
431 views
GO Classes asked Jun 25
431 views
What is the output of the following code?#include <stdio.h int main() { int a [3] = { {2, 4, 6}, {8, 10, 12} }; printf("%d %d %d", a , *(*(a + 1) + 1), *(*a + 2)); retur...
9 9 votes
2 2 answers
277
277 views
GO Classes asked Jun 24
277 views
What is the output of the following code?#include <stdio.h void update(int a[]) { a[0] = a[0] + a ; *(a + 1) = *(a + 1) + 5; } int main() { int arr[] = {2, 4, 6}; update(...