Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Is that something sane ? (http://www.go4expert.com/forums/sane-t4797/)

rstevesat 21Jun2007 16:42

Is that something sane ?
Hi , I code in C++ . Now when a peice of C came to me, i didnt know C could do that also. To recreate my point i have a simple peice of code \


call(void * x){
int * magic1 = x;
int (* magic2)[] = x;
printf(" data is %x \n", (*magic2)[1]);
printf(" data is %x \n", *magic1);
int main(){
int z[3] = {1,2,3} ;

Output is :
data is 2
data is 1
data is 2
data is 1

. Now my basic question is :
when i do this ----------- call(&z)
I send the address of the pointer to the array(i.e z)
But when i do this----------call(z)
I send the pointer to the array.
Now void sinks both of them
And with both calls to function call it gives the same result i.e no difference between pointer to an array and the address of the pointer to an array and also

no difference between int * and int * [3]
Why is that so ?????

In C++ no way you can do that.

DaWei 21Jun2007 20:33

Re: Is that something sane ?
First, read the "Before you make a query" thread, specifically regarding the use of code tags around code. Poor Shabbir is not supposed to have to act like your mommy and pick up after you.

The only reason "C++ no way can do dat" is because the code is valid C, but not valid C++. You can quote that the next time that someone tells you that C is just a subset of C++.

C++ is a more heavily typed language than C. It will not presume that a function declaration with no return type declared will return an int. It will not presume that a void pointer can be implicitly cast to any other type of pointer (use of malloc also points this out).

If you merely convert your code to appropriate C++ code (see the example), the program will perform in precisely the same way. The reason that it does so is that both languages are required to provide a conversion from the address of an array to a pointer to the first byte of an array.


#include <cstdio>

void call (void * x)
    int * magic1 = (int *) x;
    int (* magic2)[] = (int (*)[]) x;
    printf(" data is %x \n", (*magic2)[1]);
    printf(" data is %x \n", *magic1);
int main()
    int z [3] = {1,2,3} ;
    call (&z);
    call (z);
    return 0;

rstevesat 21Jun2007 20:56

Re: Is that something sane ?
I am sorry for the poor representation. Your above solution explicitly type casts it . That is fine .
In C when you pass z or &z it takes the same address i.e the address of the first element . But when you allocate the memory through malloc then it shows different addresses . This was the result i saw with gcc. Is this compiler specific , as i have never seen that before.


DaWei 21Jun2007 21:23

Re: Is that something sane ?
Malloc returns a pointer to the first byte of the allocated memory, period. The only reference to subsequent elements is via pointer arithmetic and not via a conversion of a label representing an address to a pointer to that address. Perhaps you'd like to have a look at the pointer tutorial referenced in my signature.

In any event, one should use C++ if one is using C++. Writing C as C++ typically results in trash programming. The use of malloc over new is merely one small example. Explorations into a compiler's specific reaction to unspecified or undefined behavior is almost always time wasted. Such behavior often changes with the next version.

All times are GMT +5.5. The time now is 00:23.