about type casting...

This is a discussion on about type casting... within the C Programming forums, part of the General Programming Boards category; hello...I'm designing an interface that each module of my app should follow. It has a funcion for initialization, one for ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    41

    about type casting...

    hello...I'm designing an interface that each module of my app should follow. It has a funcion for initialization, one for finalization ecc. Their prototypes are like that:

    Code:
    int Init(void* ptr);
    void Destroy();
    //ecc.
    The problem is that the Init function of each module should use ptr not as a void*, but as a type different from module to module. Each implementation of the Init function in the modules use ptr as pointer to a proprietary type. So I tried doing so:

    Code:
    int Init(void* ptr)
    {
        (strMyStruct*) ptr;
        ptr->a = 10;
    }
    but VC++ 2005 gives me an error sayng that ptr is a pointer to void instead of a struct.

    So is there a way to cast ptr only one time for all the scope of the function?

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Yes, but you will need to assign it to another variable

    Code:
    int Init (void * ptr) {
       strMyStruct * structptr = (strMyStruct*) ptr;
       structptr->a = 10;
    }
    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    You are just casting ptr to strMyStruct*, and then using ptr as a void*. You probably wanted to write:
    Code:
    int Init(void *ptr)
    {
        strMyStruct *myptr = ptr;
        myptr->a = 10;
    }
    But then do you really need to make ptr a void* in the first place? If you want to be consistent, each "module" would have an Init() function that takes a pointer, though since there are no namespaces in C, you would need to name it say, MyStructInit().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    41
    I know that a pointer is just 4bytes (or 8bytes)...but I don't like the idea to have to copy the variable only to use it as a different type...really there aren't other ways? Also why the line

    (strMyStruct*) ptr;

    compiles fine? What sense should it have?

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    41
    laserlight...thanks for your answer...but I don't understand what you're sayng...could you explain it a bit better?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by ZeroMemory View Post
    I know that a pointer is just 4bytes (or 8bytes)...but I don't like the idea to have to copy the variable only to use it as a different type...really there aren't other ways? Also why the line

    (strMyStruct*) ptr;

    compiles fine? What sense should it have?
    It casts ptr to a pointer-to-strMyStruct, and then ... ignores it. It doesn't change its type on a permanent basis (such a thing isn't possible).

    Either cast it every time you use it, or (as suggested) create an actual pointer-to-strMyStruct to use.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    You can't permnently change the type of a variable in C.
    Either you create a new variable and tell the assign the old one to it (and tell the compilr to treat your pointer as the same type as the new).
    Or you can tell the compiler to temporarily treat the pointer as a new type:

    ((strMyStruct*)ptr)->a = 10;

    Using...

    (strMyStruct*) ptr;

    ...is actually a valid line. Unfortunately, it does nothing by itself, so that's why it compiles but you get no effect. Some compiler will warn you that the line does nothing, as well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    ((strMyStruct*)ptr)->a = 10;
    But people are typically never a fan of that type of coding... they always grumble something about "human readable" or some non-sense (And yes, I do cast like that all the time--sue me).

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    I don't find a problem with it.
    I don't find a problem with most things.
    It's big, complex lines and stuff you try to stuff into one line that makes it unreadable.
    When it becomes long, break it into several parts. At those times, a temp variable with the new type, as shown above, really shines.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    (*(strMyStruct*)ptr).a = 10;
    Heh.. I think when one is doing pointer math it is useful.

    Code:
    ((myStruct *)((int *)p + 56))->fps = 30.0f;
    Sometimes I augment these "cluttered" lines with simple comments like.

    /* Matt: If you don't understand, just ask me. */
    Or the ever popular.

    /* Matt: If you don't understand, you may wish to seek a new career. */

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    Quote Originally Posted by master5001 View Post
    Code:
    (*(strMyStruct*)ptr).a = 10;
    This could probably be simplified... again because we typicall don't do "(*var).", but "var->", so it should be
    Code:
    ((strMyStruct*)ptr)->a = 10;
    Code:
    ((myStruct *)((int *)p + 56))->fps = 30.0f;
    This one is horrible.
    Code:
    int* pTemp = (int*)p + 56;
    ((strMyStruct*)pTemp)->fps = 30.0f;
    So much easier to read.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    My first one was actually a sample off of someone else's currently circulating thread. The int * thing I do frequently when parsing binary files. I will do stuff like that to skip over x blocks of data. But yes, it is not the clearest, and no I won't change my style.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    I'm not saying you should change your style, but it would be best (and most readable) if broken into two lines, as I demonstrated.
    Anyway, it was just a demonstration as to why people can find it unreadable, right?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Indubitably! So point well taken. And don't worry, I don't feel like my style is being threatened or challenged. In fact, if anything I basically said "here is my ugly code, don't let me see any of you doing this."

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    Hehehe. Gotcha!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What dose this type casting mean??
    By zhoufanking in forum C Programming
    Replies: 4
    Last Post: 06-11-2008, 07:09 AM
  2. Replies: 0
    Last Post: 03-20-2008, 08:59 AM
  3. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  4. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 06:17 AM
  5. help with simple type casting problem
    By Jeremy_S in forum C Programming
    Replies: 2
    Last Post: 02-27-2002, 12:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21