Thread: structures and pointers and members

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    3

    structures and pointers and members

    Code:
    struct wordPair {
           char* englishWord;
           char* foreignWord;
    };
    
    struct dictionary {
           struct wordPair ** data;
           int nbwords;
           int size;
    } *d;
    how would I access members englishWord and foreignWord through *d? I've tried:

    d->data->englishWord;
    d->data->foreignWord;

    but i get errors saying:

    error C2223: left of '->englishWord' must point to struct/union
    error C2223: left of '->foreignWord' must point to struct/union

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    struct dictionary {
           struct wordPair ** data;
           int nbwords;
           int size;
    } *d;
    That's a pointer to a pointer. I doubt that's what you want. But on the off chance it is, you should familiarize yourself with parenthesis. Or just pretend it's an array:
    Code:
    d->data[ X ]->englishWord;
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    Make things easy - use typedef to create pointer types to your structures - then you don't get hung up in pointer notation (as much):

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    typedef struct wordPair {
        char *englishWord;
        char *foreignWord;
    } WordPair;
    
    typedef WordPair *WordPairPtr;
    
    typedef struct dictionary {
        WordPairPtr data;
        int nbwords;
        int size;
    } Dictionary;
    
    typedef Dictionary *DictionaryPtr;
    
    int main( void )
    {
        /* allocate one dictionary structure */
        DictionaryPtr newDictionary = ( DictionaryPtr )malloc( sizeof( Dictionary ) );
    
        /* allocate word pair structure */
        newDictionary->data = ( WordPairPtr )malloc( sizeof( WordPair ) );
    
        /* initialize our new dictionary structure */
        newDictionary->data->englishWord = ( char * )calloc( 10, sizeof( char ) );
        strcpy( newDictionary->data->englishWord, "Hello" );
    
        newDictionary->data->foreignWord = ( char * )calloc( 10, sizeof( char ) );
        strcpy( newDictionary->data->foreignWord, "Bonjour" );
    
        newDictionary->nbwords = 1;
        newDictionary->size    = 1;
    
    
        /* use it */
        printf( "\"%s\" translates to \"%s\"\n",
                newDictionary->data->englishWord,
                newDictionary->data->foreignWord );
    
    
        /* free the memory - note we need to do a deep free */
        free( newDictionary->data->englishWord );
        free( newDictionary->data->foreignWord );
        free( newDictionary->data );
        free( newDictionary );
    
        return EXIT_SUCCESS;
    }
    Last edited by kmess; 04-18-2011 at 09:58 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by kmess View Post
    Make things easy - use typedef to create pointer types to your structures
    People who typedef pointers need to be bludgeoned.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing static structures members to function
    By bhagwat_maimt in forum C++ Programming
    Replies: 0
    Last Post: 11-17-2006, 04:13 AM
  2. Changing a Structures Members array size
    By Xei in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2002, 07:45 PM
  3. Help with pointers and members of structures
    By klawton in forum C Programming
    Replies: 2
    Last Post: 04-19-2002, 12:34 PM
  4. Replies: 5
    Last Post: 04-11-2002, 11:29 AM
  5. Accessing members of structures
    By Nutshell in forum C Programming
    Replies: 6
    Last Post: 02-03-2002, 12:10 PM