Thread: Pointers to structures

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    270

    Question Pointers to structures

    i got the following:
    Code:
    typedef struct {
      int n, d;
    } frac;
    int main(void) {
      frac *f1;
      f1 = (frac *) malloc(sizeof(frac));
    I am a bit confused with the malloc. Why do you have to do this, (frac *) , before it??

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You don't. In fact, you shouldn't.

    (If your compiler is telling you you should, that's because you're using a C++ compiler.)

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    (If your compiler is telling you you should, that's because you're using a C++ compiler.)
    Or perhaps you forgot to #include <stdlib.h>.
    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. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by dwks View Post
    Or perhaps you forgot to #include <stdlib.h>.
    Even that is questionable since most modern compilers will implicitly accept malloc() without including stdlib.h. Though I applaud dwks for being so thorough and proper

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by tabstop View Post
    You don't. In fact, you shouldn't.
    Someone once told me that it did not matter if you did, because there are always tools to spot the bugs in your code.

    So to answer the question: In C, you do not have to do it. You can, but you do not have to.
    Is it good practice to do it? Debatable. I am sure many would have their own opinions.
    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.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    Someone once told me that it did not matter if you did, because there are always tools to spot the bugs in your code.

    So to answer the question: In C, you do not have to do it. You can, but you do not have to.
    Is it good practice to do it? Debatable. I am sure many would have their own opinions.
    Explicitly casting the return of malloc() is like saying, "I don't trust the language." The whole point (in C, not C++) of void * is that it can be silently converted to any other pointer type -- and any pointer type can silently convert to void *. Using an explicit cast is like denying this aspect of the language, and IMHO indicates a misunderstanding of the point of void * in the first place.

    Calling functions without prototypes is just wrong. Including stdlib.h isn't optional.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by brewbuck View Post
    Explicitly casting the return of malloc() is like saying, "I don't trust the language." The whole point (in C, not C++) of void * is that it can be silently converted to any other pointer type -- and any pointer type can silently convert to void *. Using an explicit cast is like denying this aspect of the language, and IMHO indicates a misunderstanding of the point of void * in the first place.
    I am just saying that IMHO, since void* can be anything (as you say, any pointer type can be implicitly converted to void*), so that does not necessarily mean that void* is the type you are assigning to. Maybe it is merely my C++ background.
    That casting the return can mask the call without prototypes is just wrong and it is bad compiler, but I concede that that is not the aspect I am worried about.
    Anyhow. I am just pointing out that, no, it is not wrong to cast the return, and no, it is not wrong to not do it either. In C, it is a taste thing.

    Calling functions without prototypes is just wrong. Including stdlib.h isn't optional.
    200% agreed.
    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.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I never once said the word "optional" I said I appreciate his use of proper coding.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding linked lists, structures, and pointers
    By yougene in forum C Programming
    Replies: 5
    Last Post: 07-13-2011, 08:13 PM
  2. Copying pointers in structures instead of the structure data?
    By Sparrowhawk in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2009, 06:04 PM
  3. returning pointers to structures
    By Giant in forum C++ Programming
    Replies: 2
    Last Post: 06-20-2005, 08:40 AM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Help with pointers and members of structures
    By klawton in forum C Programming
    Replies: 2
    Last Post: 04-19-2002, 12:34 PM