simple pointer and struct question

This is a discussion on simple pointer and struct question within the C Programming forums, part of the General Programming Boards category; I'm very new to C and I, quite frankly, don't understand pointers yet. When executing this very stupid bit of ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    245

    simple pointer and struct question

    I'm very new to C and I, quite frankly, don't understand pointers yet.
    When executing this very stupid bit of code, I get a segmentation fault.

    Code:
    #include<stdio.h>
    
    typedef struct stupid {
      char *format_name;
    }StupidRec, *StupidList;
    
    int main(int argc, char** argv)
    {
      StupidList list;
      list->format_name = "hello";
      printf("%s\n", list->format_name);
      return 0;
    }
    I'm really not sure as to what I'm doing wrong.

    THank you.
    Last edited by dayalsoap; 09-19-2010 at 07:59 PM.

  2. #2
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265
    Don't you have to allocate memory for that pointer? Someone correct me if I'm wrong but you should be using malloc on that struct AND the char pointer field.

  3. #3
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265
    For that typedef'd struct pointer you should do:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct stupid {
      char *format_name;
    }StupidRec, *StupidList;
    
    int main(int argc, char** argv){
    
      StupidList list = malloc(sizeof(struct stupid));
      list->format_name = malloc(32);
    
      list->format_name = "hello";
      printf("%s\n", list->format_name);
      return 0;
    
    }
    Does that fix your problem?


    Also, the below shows you the difference between using the non pointer type:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct stupid {
      char *format_name;
    }StupidRec, *StupidList;
    
    int main(int argc, char** argv){
    
        StupidList list = malloc(sizeof(struct stupid));
        StupidRec list2;
    
        list->format_name = malloc(32);
    
        list->format_name = "hello";
        list2.format_name = "world";
    
        printf("%s\n", list->format_name);
        printf("%s",list2.format_name);
      
        return 0;
    
    }
    Last edited by Syscal; 09-19-2010 at 08:20 PM.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    245
    Quote Originally Posted by Syscal View Post
    For that typedef'd struct pointer you should do:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct stupid {
      char *format_name;
    }StupidRec, *StupidList;
    
    int main(int argc, char** argv){
    
      StupidList list = malloc(sizeof(struct stupid));
      list->format_name = malloc(32);
    
      list->format_name = "hello";
      printf("%s\n", list->format_name);
      return 0;
    
    }
    Does that fix your problem?
    Fixes it brilliantly. Thank you!

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    245
    Quote Originally Posted by Syscal View Post
    For that typedef'd struct pointer you should do:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct stupid {
      char *format_name;
    }StupidRec, *StupidList;
    
    int main(int argc, char** argv){
    
      StupidList list = malloc(sizeof(struct stupid));
      list->format_name = malloc(32);
    
      list->format_name = "hello";
      printf("%s\n", list->format_name);
      return 0;
    
    }
    Does that fix your problem?


    Also, the below shows you the difference between using the non pointer type:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct stupid {
      char *format_name;
    }StupidRec, *StupidList;
    
    int main(int argc, char** argv){
    
        StupidList list = malloc(sizeof(struct stupid));
        StupidRec list2;
    
        list->format_name = malloc(32);
    
        list->format_name = "hello";
        list2.format_name = "world";
    
        printf("%s\n", list->format_name);
        printf("%s",list2.format_name);
      
        return 0;
    
    }
    Turns out, you don't need the malloc there since "hello" allocates memory and returns a pointer to it.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The struct has no address, so list->name is goofed. Ok.

    But if the struct HAD a valid address, then couldn't format_name be assigned the address of the "hello" string?

    And I know it's semantics maybe, but pointers don't need *MEMORY*, what they *point* at, needs the memory. The pointer just needs a valid address.

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
        list->format_name = malloc(32);
    
        list->format_name = "hello";
    Well, this is a memory leak. I don't think someone who write code like this are not really comfortable with pointers...

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by dayalsoap View Post
    I'm very new to C and I, quite frankly, don't understand pointers yet. When executing this very stupid bit of code, I get a segmentation fault.
    I'm really not sure as to what I'm doing wrong.
    THank you.
    Try it like this....

    Code:
    #include<stdio.h>
    
    typedef struct stupid {
      char format_name[64];
    }StupidRec, *StupidList;
    
    int main(int argc, char** argv)
    {
      StupidList list = malloc(sizeof(StupidRec));
      strcpy(list->format_name, "hello");
      printf("%s\n", list->format_name);
      return 0;
    }
    Pointers, although always confusing, are simply the addresses of things in memory. You can't move into a house with no address... so all pointers have to be assigned an address in some manner.


    Now, Don't just go handing my version in as your homework assignment ... Take some time and figure out why I would do it that way....

    If you're real lucky, my version will have a bug and you get to figure out how to fix it.
    Last edited by CommonTater; 09-20-2010 at 09:15 AM.

  9. #9
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    CommonTater,

    I like your style.... The other posts even have hints.

    I have an inquiry though, when I was introduced to C/C++ were discouraged from assembling typedef statements like that (with the definition embedded in them). The idea being, that one statement should only do one thing. At least until we had our "footing...." Why all the convolution in the first place?

    It became clear later, after dealing with programming languages and compilers in general, how those syntactical structures made sense semantically, but the purpose of the exercise is to learn how to deal with a pointer (or a pass-by-reference value of some type for that matter) not the compiler's expansion of the statement, isn't it?

    Best Regards,

    New Ink -- Henry
    Kept the text books....
    Went interdisciplinary after college....
    Still looking for a real job since 2005....

    During the interim, I may be reached at ELance, vWorker, FreeLancer, oDesk and WyzAnt.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    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.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by new_ink2001 View Post
    CommonTater,

    I like your style.... The other posts even have hints.

    I have an inquiry though, when I was introduced to C/C++ were discouraged from assembling typedef statements like that (with the definition embedded in them). The idea being, that one statement should only do one thing. At least until we had our "footing...." Why all the convolution in the first place?
    I spend 99.9% of my programming time working at the Windows API level. I also like procedural programming, never could stand OOP... If you look at windows headers you will find literally thousands of examples in the format I used. After a while it starts to make sense... especially when you don't have much choice in the matter.

  12. #12
    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.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    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.

  14. #14
    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.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    Quote Originally Posted by Elysia
    Doesn't mean it's a good idea.
    I think that it really depends on the situation; I don't see how a typedef of an opaque pointer type would be a bad idea. (And in a smarter version of the program, StupidList might be just such a pointer type )
    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

Page 1 of 2 12 LastLast
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, 07: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, 09: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

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