0

let

int*p ,b = 5;
p = &b;

denotes a ONE DIMENSIONAL array, then what is the output given by following statement

printf("%d",p);

is it an address? if it is an address then tell me which element it belongs,please explain clearly

MJB
  • 7,639
  • 2
  • 31
  • 41
nobalG
  • 4,544
  • 3
  • 34
  • 72
  • These aren't the arrays you are looking for? – MJB Apr 12 '11 at 16:54
  • @JAMES @ MJB as in case of arrays,the name of the array will going to give me the address of the first element,then in case of pointers what it is going to retrun me,i.e the statement printf("%d",p); – nobalG Apr 12 '11 at 16:59

4 Answers4

1
p = &b

This doesn't denote an array! As I explained here, they're not the same thing. b is just an integer value. If you declare b as int b[] = {1, 2, 3}; then p will point to b's first element.

printf("%d",p);

This will print p's value, and since p is a pointer and points to b, this will print b's address. printf("%d", &b); will give the same result.
By the way, if b was an array, b[5] would be translated into *(p + 5), so you can read (and write) values by adding the number of elements to the beginning of the array. And b[5] == p[5] == *(b + 5) == *(p + 5)!!! But not because arrays and pointers are the same thing, just because an array's name translates to its first element's address.
As a side note, compilers always use pointers notation (*(base + offset)) when compiling to assembly.

Community
  • 1
  • 1
BlackBear
  • 22,411
  • 10
  • 48
  • 86
  • can you tell me that we can use only %u for printing out the the addresses of the elements,or.%d,%f,,,will work properly in various compilers – nobalG Apr 12 '11 at 17:13
  • @Alfred Nobel: you should use %p, which prints pointer values – BlackBear Apr 12 '11 at 17:16
0

The p pointer does not denote a one-dimensional array. It is simply a pointer to an integer. It may point to the first element of an array, like when you do int* p = new int[6], but that's something entirely different; in that case you allocate space for a new array of six integers and you store the address of the first one (or, the beginning of the array) in p.

If you print p it will print the memory address it stores. If p "denotes an array" (emphasis on quotes) then you will print the address of the first element of the array.

Paul Manta
  • 30,618
  • 31
  • 128
  • 208
0
int*p ,b = 5;
p = &b;

is exactly equivalent to:

int b = 5;
int *p = &b;

p ends up being a pointer to int. Now its true that this code will have much the same effect on what ends up in p (although b has a completely different type and value) as this:

int b[1] = {5};
int *p = b; // or int *p = &b[0];

certainly in either case p points to an int which you may treat as a simple int, or as the first (and only) element in a one-dimensional array of size one. So, what follows is legal and gives meaningful results in both cases:

printf("%d is stored at %p\n", *p, p);
printf("%d\n",p[0]);

but that's pretty much where the similarity ends.

swestrup
  • 4,079
  • 3
  • 22
  • 33
-1

address of the first element of the array. (if b was an array) use p++ to scroll through the array

anon
  • 1,071
  • 2
  • 8
  • 19
  • `p++` will not help you scroll through the array, it will only increase the value of the pointer by one (much in the same way you increment an integer). You should use the `[]` operator to access an array's elements. – Paul Manta Apr 12 '11 at 16:58
  • i meant p++ would give you the address of the next array element. – anon Apr 12 '11 at 18:00