tags:

views:

199

answers:

6
+1  Q: 

Pointers in c/c++

#include <stdio.h>

void main()  
{  
   int p[]={0,1,2,3,4};  
   int *a[]={p,p+1,p+2,p+3,p+4};  
   printf("%u %u %u",a,*a,*(*a));  
}

What *(*a) will print(will it print 0 or it's address)? And if we make array p as static(static int p[]), output gets changed .Why?

+3  A: 

First of all, I think you mean 3 %u's and not 4. Second, you shouldn't concern yourself with the output of the first 2: a and *a because they are the addresses assigned by the OS.

Now to answer your question, in the first case, when you're not using static, the code space for data is allocated on the stack, and the OS uses randomization techniques, for security purposes to change the address of the variables. Which is why, the output of a and *a keeps changing. However, once you declare p as static, it gets allocated in the data region, which is NOT randomized by the OS, and hence you get *a output as constant. BUT, a still is non-static, and hence its output will change everytime you run the program, because it is still allocated on the stack. I hope this answers your question.

If you mean that in the first case a and *a are very close and in the second case they are not, my answer answers that too.

A: 

To see this code output, just run this code:)

Output is changed, because static variable is not allocated on the stack. This is actually global variable, available only the function where it is declared. Therefore, its address is not equal to a local variable address.

Alex Farber
+2  A: 
What should be the output of the above code?

Undefined. I don't think you can expect %u to correspond to a pointer type. What if on some platform/compiler a pointer is larger than an unsigned? If you want to print pointer types you should use %p.

And if we make array p as static(static int p[]), output gets changed .Why?

Without static, this variable gets placed on the stack. With static, it gets placed in static storage (like a global variable). These regions have different memory locations.

At any rate, good code should not rely on treating pointers as numeric values, or doing printf of raw addresses and having that be overly meaningful.

asveikau
+1  A: 

Use Comeau Online Compiler to test your code for correctness -- for example running it through it gave (unsurprisingly) this result:

"ComeauTest.c", line 3: error: return type of function "main" must be "int"
    So use int main() OR int main(int argc, char *argv[])
  void main()  
       ^

"ComeauTest.c", line 7: warning: argument is incompatible with corresponding format
          string conversion
     printf("%u %u %u",a,*a,*(*a));  
                       ^

"ComeauTest.c", line 7: warning: argument is incompatible with corresponding format
          string conversion
     printf("%u %u %u",a,*a,*(*a));  
                         ^

To test the output use online Codepad:

cc1plus: warnings being treated as errors
In function 'int main()':
Line 7: warning: format '%u' expects type 'unsigned int', but argument 2 has type 'int**'
Line 7: warning: format '%u' expects type 'unsigned int', but argument 3 has type 'int*'

As you see the answers are out there, you can reach for them yourself :)

Kornel Kisielewicz
A: 

You have an array of 5 integers (named p[]). Then you have an array of pointers (named a[]), where each one points to an integer element in p[], respectively.

In the print statement, the program will print out 3 numbers. It's equivalent to:

printf("%u %u %u",&a[0],*(&a[0]),*(*(&a[0])));

This is because an array name that is not followed by a subscript is interpreted as a pointer to the first element of the array. Therefore,

The first one is the address of the array a[]. (Value will vary from one run to another even on the same machine, same compiler, but essentially an address is a large number)

The second one, it's the deferenced value of address of the first pointer, which is the address of first integer element in array p. (read this slowly three times) (again a large number that varies from run to run)

The third one, it's the second one but also deferenced one more time, which is effectively the value of the first integer element in array p, which is 0.

Khnle
A: 

The output is undefined for the first two parameters in the printf statement, this is because you are trying to print the raw address array "a" and the "p" second time. The third parameter will return "0" ( value of *p).

When you make the array p static and recompile the code, this time the array is not on stack but will be put on static region and hence you see the different address location ,however answer is same as I explained above.

dicaprio