Thread: Basic pointers to structures question...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    13

    Basic pointers to structures question...

    header:
    Code:
    struct termlist
    {
          uint32_t num_terms; 
          char **term;           
    };
    
    int getMetaData( uint32_t specific_tag, termlist **terms);
    main:
    Code:
    int main(int argc, char **argv)
    {
    
    uint32_t specificTag;
    specificTag = atoi(argv[1]);
    
    termlist *sample1;
    
    getMetaData( specificTag, sample1 );
    
    ...
    
    }

    Hi, I'm really confused about pointers to structures. I'm trying to understand this snippet of code above.

    The function prototype requires a double pointer to the structure, termlist **terms. But in main when calling the function only a pointer to the structure is passed, sample1.

    I'm quite confused on how the function prototype requires a double pointer, but in main only a single pointer is passed.


    Thanks!
    Last edited by superbbrr; 05-16-2012 at 11:57 AM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by superbbrr View Post
    I'm quite confused on how the function prototype requires a double pointer, but in main only a single pointer is passed.
    I'd be confused too if that would compile

    Kurt

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    First, either you're using a C++ compiler or you've omitted a typedef for your struct. If you're writing C, don't use a C++ compiler.

    A compiler is required to spit out some sort of diagnostic for that code, because you're converting between incompatible pointer types. Is your compiler giving you a warning? If so, heed it! If not, either your compiler is very bad or you're not compiling the code you showed here.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    You should always check argc before trying to access argv[1] or greater and then qualify that value before using, otherwise your program might blow up your computer.

    Re:
    Code:
        termlist *sample1;
    The declaration shows no datatype.
    "termlist" is not a data type. It is an identifier of type ??????.

    Are you sure you want a pointer?
    If you are new structs and all perhaps you should start with a termlist object itself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers basic question
    By sed_y in forum C Programming
    Replies: 9
    Last Post: 06-23-2011, 01:47 PM
  2. Basic question about declaring variables/structures
    By bnyec in forum C Programming
    Replies: 11
    Last Post: 05-19-2011, 07:49 PM
  3. Pointers and Data Structures (Simple Question, I think)
    By Flamefury in forum C Programming
    Replies: 16
    Last Post: 11-23-2009, 12:17 AM
  4. Structures and pointers question
    By cjohnman in forum C Programming
    Replies: 2
    Last Post: 04-30-2008, 01:48 PM
  5. Pointers to structures - Beginner question
    By RobJ in forum C Programming
    Replies: 6
    Last Post: 04-10-2006, 05:57 PM