Thread: simple pointer and struct question

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Pretty much all of the code here has one or more flaws. I'll demonstrate "working" versions.

    There are several ways of doing this.
    One way:
    Code:
    #include<stdio.h>
    
    typedef struct
    {
        char format_name[64];
    }
    StupidRec;
    
    int main(int argc, char** argv)
    {
        StupidRec* list = malloc(sizeof(StupidRec));
        strcpy(list->format_name, "hello");
        printf("%s\n", list->format_name);
        free(list); // DON'T FORGET!
        return 0;
    }
    Another way:
    Code:
    #include<stdio.h>
    
    typedef struct
    {
        const char* format_name;
    }
    StupidRec;
    
    int main(int argc, char** argv)
    {
        StupidRec list;
        list.format_name = "hello"; // NOTE: CAN'T MODIFY STRING
        printf("%s\n", list.format_name);
        return 0;
    }
    A third way:
    Code:
    #include<stdio.h>
    
    typedef struct
    {
        char* format_name;
    }
    StupidRec;
    
    int main(int argc, char** argv)
    {
        char buf[1024];
        StupidRec* list = malloc(sizeof(StupidRec));
        list->format_name = buf; // SET POINTER TO A VALID STORAGE FIRST
        strcpy(list->format_name, "hello"); // DANGEROUS, BUT WE KNOW "hello" DO NOT TAKE UP 1024 CHARACTERS.
        printf("%s\n", list->format_name);
        free(list); // DON'T FORGET!
        return 0;
    }
    And finally, here is how to NOT do it:
    Code:
    #include<stdio.h>
    
    typedef struct
    {
        char* format_name;
    }
    StupidRec;
    
    int main(int argc, char** argv)
    {
        StupidRec list;
        list.format_name = malloc(1024);
        list.format_name = "hello"; // MEMORY LEAK. DON'T DO THIS.
        printf("%s\n", list.format_name);
        return 0;
    }
    Note how I don't use the StupidList type. It's confusing only to typedef pointers that way. Avoid it.
    Last edited by Elysia; 09-20-2010 at 09:34 AM.
    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.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Elysia View Post
    Note how I don't use the StupidList type. It's confusing only to typedef pointers that way. Avoid it.
    LOL... tell that to the windows crew....
    Thousands and thousands of structs, defined exactly as the OP and I did it.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by CommonTater View Post
    LOL... tell that to the windows crew....
    Thousands and thousands of structs, defined exactly as the OP and I did it.
    Doesn't mean it's a good idea.
    Windows also uses hungarian notation, which we know we love to hate.
    You may be used to it, but don't drag others into that foolishness. It hurts readability.
    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.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Elysia View Post
    Doesn't mean it's a good idea.
    Windows also uses hungarian notation, which we know we love to hate.
    You may be used to it, but don't drag others into that foolishness. It hurts readability.
    I think tha'ts arguable at best Elysia. By and large I like your posts, you are one of the most helpful people here and that can only be a good thing.

    I agree it may hurt readability for those not accustomed to it but what do you do when you have to work with it day in day out? You get used to it and that would be the issue here; what you get used to.

    The windows tactic is to define a struct and pointer together, prefixing the pointer with either P or LP according to rules not entirely clear. If you ever do windows programming --and most of us will-- you do need to be comfortable with the notations.

    So... I wouldn't expound on one while panning the other... in all truth, we need to know both.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM