Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. int dictionary_add(struct dictionary* d,
  2. const char * const english,
  3. const char * const foreign){
  4. /* ROLE Adds a new wordPair made of strdup copies of the parameter strings
  5. to a dictionary d
  6.  
  7. RETURNS 0 if everything went fine
  8.  
  9. PARAMETERS d the dictionary to work with
  10. english string representing the english part of the new wordPair
  11. foreign string representing the foreign part of the new wordPair
  12. */
  13.  
  14.  
  15.  
  16.  
  17. //Determine where in the array the wordPair is going.
  18. int location;
  19. location=((d->size)-(d->nbwords))-1;
  20. printf("Adding data to array location: %inn",location);
  21.  
  22.  
  23. //Build the wordPair
  24. const struct wordPair newPair={english,foreign};
  25.  
  26. //Add the wordPair
  27. d->data[0]=&newPair;
  28.  
  29. //***************This is where the problem shows up***************
  30. printf("Added english:%sn",d->data[0]->englishWord);
  31. //d->data[0]=&newPair; //When uncommeted, program doesn't crash.
  32. printf("Added english:%sn",d->data[0]->englishWord);
  33. d->nbwords++;
  34. return 0;
  35. }
  36.  
  37. const char* english=malloc(sizeof(char)*6);
  38. const char* foreign=malloc(sizeof(char)*6);
  39. strcpy(english,"hello");
  40. strcpy(foreign,"holla");
  41.  
  42. struct dictionary *dictionary_build(int size){
  43. /* ROLE Allocate and initialize a new dictionary structure able to accomodate a number of
  44. pairs of words specified by the size parameter
  45. RETURNS Address of new dictionary, if allocation was successfull.
  46. NULL otherwize
  47. PARAMETERS The size of the dictionary to make
  48. */
  49. struct dictionary *d=malloc(sizeof(struct dictionary));
  50.  
  51. d->size=size;
  52. d->nbwords=0;
  53.  
  54. struct wordpair* wordPairs[size]; //create array of pointers to wordpairs
  55.  
  56.  
  57. d->data=&wordPairs; //Set pointer to array of pointers to wordpairs
  58.  
  59. return d;
  60. }
  61.  
  62. struct wordPair {
  63. char* englishWord;
  64. char* foreignWord;
  65. };
  66.  
  67. struct dictionary {
  68. struct wordPair ** data;
  69. int nbwords;
  70. int size;
  71. };
  72.  
  73. struct wordpair* wordPairs[size];
  74. d->data=&wordPairs;
  75. return d;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement