Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int dictionary_add(struct dictionary* d,
- const char * const english,
- const char * const foreign){
- /* ROLE Adds a new wordPair made of strdup copies of the parameter strings
- to a dictionary d
- RETURNS 0 if everything went fine
- PARAMETERS d the dictionary to work with
- english string representing the english part of the new wordPair
- foreign string representing the foreign part of the new wordPair
- */
- //Determine where in the array the wordPair is going.
- int location;
- location=((d->size)-(d->nbwords))-1;
- printf("Adding data to array location: %inn",location);
- //Build the wordPair
- const struct wordPair newPair={english,foreign};
- //Add the wordPair
- d->data[0]=&newPair;
- //***************This is where the problem shows up***************
- printf("Added english:%sn",d->data[0]->englishWord);
- //d->data[0]=&newPair; //When uncommeted, program doesn't crash.
- printf("Added english:%sn",d->data[0]->englishWord);
- d->nbwords++;
- return 0;
- }
- const char* english=malloc(sizeof(char)*6);
- const char* foreign=malloc(sizeof(char)*6);
- strcpy(english,"hello");
- strcpy(foreign,"holla");
- struct dictionary *dictionary_build(int size){
- /* ROLE Allocate and initialize a new dictionary structure able to accomodate a number of
- pairs of words specified by the size parameter
- RETURNS Address of new dictionary, if allocation was successfull.
- NULL otherwize
- PARAMETERS The size of the dictionary to make
- */
- struct dictionary *d=malloc(sizeof(struct dictionary));
- d->size=size;
- d->nbwords=0;
- struct wordpair* wordPairs[size]; //create array of pointers to wordpairs
- d->data=&wordPairs; //Set pointer to array of pointers to wordpairs
- return d;
- }
- struct wordPair {
- char* englishWord;
- char* foreignWord;
- };
- struct dictionary {
- struct wordPair ** data;
- int nbwords;
- int size;
- };
- struct wordpair* wordPairs[size];
- d->data=&wordPairs;
- return d;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement