Like Tree1Likes
  • 1 Post By AndiPersti

pointers to structures

This is a discussion on pointers to structures within the C Programming forums, part of the General Programming Boards category; Hi all. Can someone please explain why the compiler is giving this error? Why does the compiler think that there ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242

    pointers to structures

    Hi all.

    Can someone please explain why the compiler is giving this error? Why does the compiler think that there is no structure? Thanks in advance.

    Code:
    main.c||In function ‘main’:|
    main.c|15|error: request for member ‘word’ in something not a structure or union|
    ||=== Build finished: 1 errors, 0 warnings ===|
    Code:
    #include <stdio.h>
    
    struct word {
        char word[40];
        int x;
    };
    
    int main(void)
    {
        struct word word;
        struct word *p_word;
    
        p_word = &word;
    
        p_word.word = 40 * mallaoc(sizeof(char));
    
        return 0;
    }
    Same error for this.
    Code:
    #include <stdio.h>
    
    struct word {
        char word[40];
        int x;
    };
    
    int main(void)
    {
        struct word *word;
    
        word.word = mallaoc(sizeof(char));
    
        return 0;
    }
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    p_word.word = 40 * mallaoc(sizeof(char));
    "p_word" is a pointer. Either dereference it before accessing the struct member
    Code:
    (*p_word).word
    or use the "->" operator
    Code:
    p_word->word
    Bye, Andreas
    cfanatic likes this.

  3. #3
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    Ok, thanks.
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers to structures (2)
    By templarz in forum C Programming
    Replies: 3
    Last Post: 08-15-2006, 04:56 PM
  2. Structures, and pointers to structures
    By iloveitaly in forum C Programming
    Replies: 4
    Last Post: 03-30-2005, 05:31 PM
  3. structures with pointers to structures
    By kzar in forum C Programming
    Replies: 3
    Last Post: 11-20-2004, 08:32 AM
  4. Pointers to Classes || pointers to structures
    By C++Child in forum C++ Programming
    Replies: 24
    Last Post: 07-30-2004, 06:14 PM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM

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