Thread: structs of structs

  1. #31
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote Originally Posted by Salem
    > C++ is backwards compatible with C
    No it isn't.
    It's a common misconception that C++ is a superset of C, it isn't.
    http://david.tribble.com/text/cdiffs.htm

    There's stuff in C which simply doesn't work in C++, and stuff which works differently in C++.
    Simply compiling C code as C++ will cause you grief eventually.
    I know it isn't a superset but I do recall Bjarne mentioning backwards compatibility:
    http://www.research.att.com/~bs/bs_faq.html
    http://www.research.att.com/~bs/bs_faq2.html

    It's in one of those somewhere
    *edit*

    I know certain things just don't work at all, mainly owing to C++ type-safety.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #32
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It isn't backwards compatable. Didn't you ever learn that in true/false situations, if any part of it is false, the whole thing is?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #33
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by rotis23
    Borland Windows compiler doesn't like a cast'less malloc:

    Code:
    my_parent = malloc(sizeof(parent));
    fails with:

    Code:
    Error E2034 struct.c 12: Cannot convert 'void *' to 'parent *' in function
    main()

    I'm joing a little late -- but I'll throw in my 2 cents worth anyway.

    If you are using a c++ compiler to compile c-code, the compiler will treat the c-code as if it were c++. c++ standards require malloc() to be typecast to the appropriate type. That is one instance where c++ is NOT backward compatable to C. With VC++ compiler, all files that have a *.cpp extension are compiled as c++ code. Files with *.c extension are exclusively C. So if you tried to use a c++ class in a *.c file the compiler will complain bitterly. I suspect that Borland compiler is doing the same thing.
    Last edited by Ancient Dragon; 09-07-2005 at 07:30 PM.

  4. #34
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Or you might need to use
    Code:
    #include <stdlib.h>
    stdlib.h has the prototype for malloc().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #35
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by dwks
    Or you might need to use
    Code:
    #include <stdlib.h>
    stdlib.h has the prototype for malloc().

    malloc() is prototyped to return type void*. If you want int* or anything else, c++ requires it be typecast, but C does not.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 08:30 AM
  2. Multidimentional structs + memcpy() == FAIL
    By Viper187 in forum C Programming
    Replies: 8
    Last Post: 06-18-2008, 02:46 AM
  3. packed structs
    By moi in forum C Programming
    Replies: 4
    Last Post: 08-20-2002, 01:46 PM
  4. ArrayLists + Inner Structs
    By ginoitalo in forum C# Programming
    Replies: 5
    Last Post: 05-09-2002, 05:09 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM