1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Is that something sane ?

Discussion in 'C' started by rstevesat, Jun 21, 2007.

  1. rstevesat

    rstevesat New Member

    Joined:
    Jun 21, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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 \

    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} ;
    call(&z);
    call(z);
    } 


    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.
     
    Last edited by a moderator: Jun 21, 2007
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    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.

    C++:
    Code:
    #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;
    }
    
     
  3. rstevesat

    rstevesat New Member

    Joined:
    Jun 21, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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.

    Thanks
     
  4. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    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.
     

Share This Page