PDA

View Full Version : ambiguity with pointers



c2tarun
January 23rd, 2011, 10:50 AM
#include<stdio.h>
void main()
{
int a[5]={7,3,1,4,5};
printf("%p\n",a);
printf("%p\n",&a);
printf("%p",&a+1);
}

why the o/p of line 5 and 7 are same? Conceptually &a should return the address of the memory location that stores the base address of array.

on increment of 1 the resulting memory address is 14 greater than the base address, why so?

Praveen30
January 23rd, 2011, 11:04 AM
#include<stdio.h>
void main()
{
int a[5]={7,3,1,4,5};
printf("%p\n",a);
printf("%p\n",&a);
printf("%p",&a+1);
}

why the o/p of line 5 and 7 are same? Conceptually &a should return the address of the memory location that stores the base address of array.

on increment of 1 the resulting memory address is 14 greater than the base address, why so?

tell me your output!!!

c2tarun
January 23rd, 2011, 11:06 AM
tell me your output!!!

0x7fff5dbff520
0x7fff5dbff520
0x7fff5dbff534

Praveen30
January 23rd, 2011, 11:30 AM
0x7fff5dbff520
0x7fff5dbff520
0x7fff5dbff534

topic--using array name as a pointer
concept-
An array name can be used as a pointer to the first element in the array...

for example--int a[10];
using a as a pointer to the first element you can modify a[0];
so *a=7;

similarly, you can modify a[1] by *(a+1)..

note-- in general a+i is same as &a[i] and *(a+i) as a[i].

first ans:- a and &a both will point at the first element of the array so the address will be same...

c2tarun
January 23rd, 2011, 12:15 PM
topic--using array name as a pointer
concept-
An array name can be used as a pointer to the first element in the array...

for example--int a[10];
using a as a pointer to the first element you can modify a[0];
so *a=7;

similarly, you can modify a[1] by *(a+1)..

note-- in general a+i is same as &a[i] and *(a+i) as a[i].

first ans:- a and &a both will point at the first element of the array so the address will be same...

If a is pointer to first element than &a should be a pointer to a, as double pointer.

Arndt
January 23rd, 2011, 01:07 PM
If a is pointer to first element than &a should be a pointer to a, as double pointer.

If a is a variable, yes. But pointers are not created by the & operation; they are just values. In this case, a is not a variable whose address can be taken, it's an address itself. What would you want &a to be?

worksofcraft
January 23rd, 2011, 03:37 PM
If a is a variable, yes. But pointers are not created by the & operation; they are just values. In this case, a is not a variable whose address can be taken, it's an address itself. What would you want &a to be?

Yes I agree with this.
However you are correct c2tarun, "a" here is indeed ambiguous.

In C and C++ that "a" can mean the address of the array, but it can also mean the whole array. Try compiling with all warnings switched on and see look this what we get:


g++ -Wall -pedantic arryptr.cpp
arryptr.cpp: In function ‘int main()’:
arryptr.cpp:9: warning: format ‘%p’ expects type ‘void*’, but argument 2 has type ‘int*’
arryptr.cpp:11: warning: format ‘%p’ expects type ‘void*’, but argument 2 has type ‘int (*)[5]’
arryptr.cpp:13: warning: format ‘%p’ expects type ‘void*’, but argument 2 has type ‘int (*)[5]’


Well I'm not interested in the "void*" bit (nor main returning and int), but when you take the address of a the compiler decides that you CAN'T have the address of an address (because an address is not an lvalue (http://en.wikipedia.org/wiki/Value_(computer_science)))

So instead it decides that you must have meant the whole array and the address of that is infact the same as "a" when it is used as an address:


$ ./a.out
0x7fff96eb6790 <--- a
0x7fff96eb6790 <--- see &a = a
0x7fff96eb67a4


now if a is a whole array of 5 ints and an int is 4 bytes then a+1 must be the next array of 5 ints... so 5 * 4 = 20 decimal = 16 + 4 = 0x14 hex...

;)

c2tarun
January 23rd, 2011, 03:43 PM
Yes I agree with this.
However you are correct c2tarun, "a" here is indeed ambiguous.

In C and C++ that "a" can mean the address of the array, but it can also mean the whole array. Try compiling with all warnings switched on and see look this what we get:


g++ -Wall -pedantic arryptr.cpp
arryptr.cpp: In function ‘int main()’:
arryptr.cpp:9: warning: format ‘%p’ expects type ‘void*’, but argument 2 has type ‘int*’
arryptr.cpp:11: warning: format ‘%p’ expects type ‘void*’, but argument 2 has type ‘int (*)[5]’
arryptr.cpp:13: warning: format ‘%p’ expects type ‘void*’, but argument 2 has type ‘int (*)[5]’
Well I'm not interested in the "void*" bit (nor main returning and int), but when you take the address of a the compiler decides that you CAN'T have the address of an address (because an address is not an lvalue (http://en.wikipedia.org/wiki/Value_%28computer_science%29))

So instead it decides that you must have meant the whole array and the address of that is infact the same as "a" when it is used as an address:


$ ./a.out
0x7fff96eb6790 <--- a
0x7fff96eb6790 <--- see &a = a
0x7fff96eb67a4
now if a is a whole array of 5 ints and an int is 4 bytes then a+1 must be the next array of 5 ints... so 5 * 4 = 20 decimal = 16 + 4 = 0x14 hex...

;)

awesome explanation. Thanks a lot.
A bit personal question, are you denss richie or someone, you know almost everything about C and C++. How come :)

worksofcraft
January 23rd, 2011, 03:54 PM
awesome explanation. Thanks a lot.
A bit personal question, are you denss richie or someone, you know almost everything about C and C++. How come :)

lol - no, once upon a time I used to program test routines to test OEM equipment. Then I had an arts and craft shop for a while (hence the name) and I only recently decided to pick up programming again (as a hobby)... still unemployed single parent atm :/

c2tarun
January 23rd, 2011, 04:03 PM
lol - no, once upon a time I used to program test routines to test OEM equipment. Then I had an arts and craft shop for a while (hence the name) and I only recently decided to pick up programming again (as a hobby)... still unemployed single parent atm :/
god if its just a hobby than u know so much :). I can't even imagine what will happen when you'll be a professional. Believe me here there are programmers who know lot less than you and still they are software engineers ;)

worksofcraft
January 23rd, 2011, 04:15 PM
god if its just a hobby than u know so much :). I can't even imagine what will happen when you'll be a professional. Believe me here there are programmers who know lot less than you and still they are software engineers ;)

TYVM :) I did actually apply for some tutor jobs because I think I am good at explaining things. However they tend to only want people with qualifications... so you make sure you get some while you can! :shock: