Reputation: 634
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.
Upvotes: 1
Views: 1912
Reputation: 9
What you are trying to do is copy 1 string completely with an assignment operator '='. You can either do it in a for loop or use strcpy().
Upvotes: -1
Reputation: 1054
You might want to look into using qsort
(if you have a linux/mac type man qsort
for more info)
the qsort call will look like qsort(dictionary,size_of_arr, sizeof(entry), func_name);
You will have to make a comparison function named func_name
(though you can call it whatever you want, as long as you pass it properly in the function call.
int func_name(void *ent1, void *ent2)
{
struct entry a = *(struct entry*)ent1, b = *(struct entry*)ent2;
return strcmp(a.word, b.word);
}
I think that'll work, if not something close to it will...
Upvotes: 1
Reputation: 3541
dictionary2[0].word[i] = dictionary[0].word[i];
is plain wrong. You cannot copy one array into another as assignment operation as you do for say int
type.
You can use strcpy(or strncpy) to copy the bytes from one array into another. Or use memcpy.
strcpy(dictionary2[0].word[i], dictionary[0].word[i]);
memcpy(dictionary2[0].word[i],
dictionary[0].word[i],sizeof(dictionary[0].word));
Upvotes: 0
Reputation: 563
you need to use a function to assign strings ( character arrays as well )
common function to do this is :
char a[15];
strcpy( a , "ameer" ); % copying second string to first one
from library that means a="ameer";
so in your case you can write :
strcpy( dictionary2[0].word , dictionary[0].word );
Upvotes: 0
Reputation: 53006
As your compiler tells you arrays are not assignable in c, to copy a string you need the strcpy()
function
strcpy(dictionary2[0].word[i], dictionary[0].word[i]);
you need to ensure that the destination array has enough space to hold the string, that is the number of characters in the string +1
for the nul
terminator.
Upvotes: 2