Thread: about char* in struct

  1. #1
    Registered User
    Join Date
    Jun 2010
    Location
    beijing
    Posts
    23

    about char* in struct

    hello everyone,i dont know how to assign the char* in struct,
    Code:
    struct List
    {
        char *data;
        struct List *next;
    };
    
    struct List *InputData(struct List *head)
    {
        struct List *p = head;
        struct List *pNewNode = NULL;
        char data[50];
    
        printf("input data: ");
        while(1)
        {
            scanf("%s ", data);
            if (0 != strcmp("#", data))
            {
                pNewNode = (struct List*)malloc(sizeof(struct List));
                pNewNode->data = (char *)malloc(sizeof(char));
                if (NULL == pNewNode || NULL == pNewNode->data)
                {
                    printf("error\n");
                    return NULL;
                }
    
                strcpy(pNewNode->data, data);
                pNewNode->next = NULL;
                p->next = pNewNode;
                p = pNewNode;
            }   
            else
            {
                return head;
            }
        }
    		
        return NULL;
    }
    
    int  main()
    {	
        struct List *head = NULL;
    
        head = InitList(head);			
    
        head = InputData(head);
    	
        return 0;
    }
    when i run the program,it has an error,but if i debug it ,it well,please help me out?thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,128
    > pNewNode->data = (char *)malloc(sizeof(char));
    Right idea, but you need
    strlen(data)+1
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Location
    beijing
    Posts
    23
    aha.......what an important thing! but i forgot,hehe,thank u very much~~~

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You are forgetting to free what you malloc.
    Also, do not reading strings with scanf! SourceForge.net: Scanf woes - cpwiki
    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.

  5. #5
    Registered User
    Join Date
    Jun 2010
    Location
    beijing
    Posts
    23
    oh,thank you,now i turn it to gets/puts functions, but i want to use unicode version,still error when i run it!

    Code:
    struct List
    {
        TCHAR *data;
        struct List *next;
    };
    
    struct List *InputData(struct List *head)
    {
        struct List *p = head;
        struct List *pNewNode = NULL;
        TCHAR data[50];
    
        printf("input data: ");
        while(1)
        {
            _getws(data);
            if (0 != lstrcmp(TEXT("#"), data))
            {
                pNewNode = (struct List*)malloc(sizeof(struct List));
                if (NULL == pNewNode)
                {
                    printf("assign memory error!\n");
                    return NULL;
                }
                pNewNode->data = (TCHAR *)malloc(lstrlen(data) + sizeof(TCHAR));
                if (NULL == pNewNode->data)
                {
                    printf("assign memory error!\n");
                    return NULL;
                }
                lstrcpy(pNewNode->data, data);
                pNewNode->next = NULL;
                p->next = pNewNode;
                p = pNewNode;
            }
            else
            {
                return head;
            }
        }
    
        return NULL;
    }
    
    void OutputData(struct List *head)
    {
        struct List *p = NULL;
        if (NULL != head)
        {
            p = head->next;
        }
        else
        {
            return;
        }
        while (NULL != p)
        {
            _putws(p->data);
            p = p->next;
        }
        printf("\n");
    }
    
    int __cdecl wmain()
    {	
        struct List *head = NULL;
    
        setlocale(LC_ALL, "chs");
    
        head = InitList(head);
    	
        head = InputData(head);
        OutputData(head);
        FreeList(head);
    
        return 0;
    }

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Who said you should use gets? It's even worse!
    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
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    I would use fgets() since it allows for you to specify the sizeof the string.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ampc View Post
    oh,thank you,now i turn it to gets/puts functions, but i want to use unicode version,still error when i run it!

    [CODE]struct List
    {
    TCHAR *data; <--- need to assign size
    struct List *next;
    };

    [CODE]
    To make life easyier... When using structs like this you should assign a field size for your string.
    You might also want to use void pointers since a pointer to a structure is undefined within the structure's own definition.

    Code:
    typedef struct tList
        { char data[100];
           void*  Next;  }
         List, *pList;
    Then in your linking code you would use...
    Code:
    // list tracking
    pList Head;       // start of list
    pList Current;   // current item
    
    // init  new chain
    Head = malloc(sizeof(List));   
    Current = Head;
    
    // add to chain
    Current->Next = malloc(sizeof(list));
    /* insert data here */
    
    // move to next item
    Current = Current->Next;
    Last edited by CommonTater; 09-12-2010 at 08:22 AM.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Using "struct List" inside the struct is fine.
    But don't create typedefs for pointers! It serves only to confuse!
    It is also unclear why you're creating an array of pointers in the struct.

    Code:
    typedef struct List
    {
        char* data;
        struct List* Next;
    } List;
    
    // first in chain
    List* Head = malloc(sizeof(List));   
    
    // init pointer to current item
    Current = Head;
    
    // add to chain
    List* Current->Next = malloc(sizeof(list));
    /* insert data here */
    
    // move to next item
    Current = Current->Next;
    
    /* Don't forget to free! */
    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
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Elysia View Post
    Using "struct List" inside the struct is fine.
    But don't create typedefs for pointers! It serves only to confuse!
    It is also unclear why you're creating an array of pointers in the struct.
    The char* data[100]; is C's version of a string, Elysia. I'm merely defining a buffer to hold the string which would later be copied in with something like strcpy(Current->data, newdata); That way the string is right in the struct eliminating the need to malloc each string separately.

    Code:
    typedef struct List
    {
        char* data;
        struct List* Next;
    } List;
    On any compiler I've used that typedef would return a redifinition error.

    Code:
    // first in chain
    List* Head = malloc(sizeof(List));   
    
    // init pointer to current item
    Current = Head;
    That would return an unkown type error for Current.
    Last edited by CommonTater; 09-12-2010 at 08:17 AM.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What?
    Using the typedef INSIDE the struct is not allowed.
    Using it outside is fine.
    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
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Elysia View Post
    What?
    Using the typedef INSIDE the struct is not allowed.
    Using it outside is fine.
    Ok, I give up... you win.

  13. #13
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You might also want to use void pointers since a pointer to a structure is undefined within the structure's own definition.
    erm?

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That may be related to the fact that a typedef of the structure is not defined within the structure, so...
    Code:
    typedef struct mystruct
    {
        struct mystruct* p; // OK
        mystruct* pp; // Not OK
    } mystruct;
    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.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Bayint Naung View Post
    erm?
    I ran into this once... Elysia has one solution, void pointers are another.

    Code:
    typedef struct tFRED
       { char data[1000];
          FRED* Next;  }           <--- FRED is not yet defined
        FRED, *pFRED;             <--- Now FRED is defined
    I found that using void pointers solved the problem for me... so I went with it.

    Code:
    typedef struct tFRED
       { char data[1000];
          void* Next;  }           <--- void is already defined
        FRED, *pFRED;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. casting struct to struct
    By klmdb in forum C Programming
    Replies: 6
    Last Post: 08-14-2010, 02:29 PM
  2. Help me please.
    By yann in forum C Programming
    Replies: 15
    Last Post: 09-29-2009, 09:04 PM
  3. help with structs and malloc!
    By coni in forum C Programming
    Replies: 20
    Last Post: 09-14-2009, 05:38 PM
  4. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  5. Replies: 10
    Last Post: 05-18-2006, 11:23 PM