samedi 18 avril 2015

Copying string from one structure to another structure

I am trying to write a program that alphabetically sorts a small dictionary. To do this, I need to be able to copy strings from the unsorted dictionary to the sorted dictionary. If I try to copy an entire string as such:



#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>

struct entry
{
char word[15];
char definition[50];
};


void dictionarySort(struct entry dictionary[]) {
int i;

struct entry dictionary2[100] = {{}};

for (i = 0; i <= strlen(&dictionary->word[0]); i++) {
dictionary2[0].word[i] = dictionary[0].word[i];

}
dictionary2[0].word = dictionary[0].word;

printf("%s\n",dictionary2[0].word);

}

int main (void) {
struct entry dictionary[100] =
{{"aerie", "a high nest"},
{"abyss", "a bottomless pit"},
{"ahoy", "a nautical call of greeting"},
{"addle", "to become confused"},
{"aardvark", "a burrowing African mammal"},
{"agar", "a jelly made of seaweed"},
{"acumen", "mentally sharp; keen"},
{"aigrette", "an ornamental cluster of feathers"},
{"affix", "to attach"},
{"ajar", "partially opened"}};
dictionarySort(dictionary);
}


I get the following error message:



error: array type 'char [15]' is not assignable
dictionary2[0].word = dictionary[0].word;
~~~~~~~~~~~~~~~~~~~ ^


On the other hand, if I copy individual characters, I have no way of differentiating the strings, which is necessary when dictionary2 is accessed.


Aucun commentaire:

Enregistrer un commentaire