当我使用printf语句获取它时,我的一个变量dict2Length正在返回一个惊人的1701996320。尽管我已经评论了任何可能增加其价值的事实,但事实仍然如此,这使我的困惑更加复杂。它最初被定义为1。
这是我的代码。我道歉了。
#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, j, k, word1, word2, dict2Length = 1;
bool bnf = false;
struct entry dictionary2[100] = {{}};
for (i = 0; i <= strlen(&dictionary->word[0]); i++) {
strcpy(&dictionary2[0].word[i], &dictionary[0].word[i]);
}
i = 0;
word1 = 1;
word2 = 0;
while (isalpha(dictionary[word1].word[0])) {
while (i <= strlen(&dictionary->word[word1])) {
//printf("%c", dictionary[word1].word[i]);
if (dictionary[word1].word[i] == dictionary2[word2].word[i]) {
i++;
bnf = false;
}
else if (dictionary[word1].word[i] < dictionary[word2].word[i]) {
//insert section to prevent back-and-forth cycling
if (bnf == false) {
word2--;
bnf = true;
}
else { //(dictionary[word1].word[i] < dictionary[word2].word[i])
//open up new index by moving everything above up one, insert at word
for (j = dict2Length; j > word2; j--) {
//word
for (k = 0; k <= strlen(&dictionary2->word[0]); k++) {
strcpy(&dictionary2[j+1].word[k], &dictionary2[j].word[k]);
}
//definition
for (k = 0; k <= strlen(&dictionary2->word[0]); k++) {
strcpy(&dictionary2[j+1].definition[k], &dictionary2[j].definition[k]);
}
}
//for (k = 0; k < strlen(&dictionary1->word[word1]))
for (k = 0; k < strlen(dictionary[word1].word); k++) {
strcpy(&dictionary2[word2].word[k], &dictionary[word1].word[k]);
//printf("one\n");
}
for (k = 0; k < strlen(dictionary[word1].definition); k++) {
strcpy(&dictionary2[word2].definition[k], &dictionary[word1].definition[k]);
//printf("two\n");
}
//dict2Length++;
break;
}
}
else {
//insert section to prevent back-and-forth cycling
if (bnf == false) {
word2++;
bnf = true;
}
else { //(dictionary[word1].word[i] < dictionary[word2].word[i])
//open up new index by moving everything above up one, insert at word
for (j = dict2Length; j > word2; j--) {
//word
for (k = 0; k <= strlen(&dictionary2->word[0]); k++) {
strcpy(&dictionary2[j+1].word[k], &dictionary2[j].word[k]);
//printf("%d", word1);
}
//definition
for (k = 0; k <= strlen(&dictionary2->word[0]); k++) {
strcpy(&dictionary2[j+1].definition[k], &dictionary2[j].definition[k]);
//printf("two\n");
//printf("%d", word1);
}
}
for (k = 0; k < strlen(dictionary[word1].word); k++) {
strcpy(&dictionary2[word2].word[k], &dictionary[word1].word[k]);
//printf("three\n");
//printf("%d", word1);
}
for (k = 0; k < strlen(dictionary[word1].definition); k++) {
strcpy(&dictionary2[word2].definition[k], &dictionary[word1].definition[k]);
}
//dict2Length++;
break;
}
}
}
word1++;
}
word2 = 0;
printf("dict2Length = %d", dict2Length);
//for (i = 0; i < dict2Length; i++) {
//printf ("%s\n", dictionary2[i].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);
}
答案 0 :(得分:0)
可能是因为您错误地复制了字符串并破坏了堆栈。你的循环看起来像是在尝试一次复制一个角色,你遍历每个角色,然后你去每个角色做一个strcpy。 strcopy将所有字符复制为空,因此您不需要自己进行循环。
> for (i = 0; i <= strlen(&dictionary->word[0]); i++) {
> strcpy(&dictionary2[0].word[i], &dictionary[0].word[i]);
> }
我认为你可能意味着做一些更像下面的事情而不是每个字符的循环来复制字符串。
>
> strcpy(dictionary2[0].word, dictionary[0].word);
>
答案 1 :(得分:0)
如果您要将dictonary
中的字词内容复制到dictionary2
,请填写以下代码
for (i = 0; i <= strlen(&dictionary->word[0]); i++) {
strcpy(&dictionary2[0].word[i], &dictionary[0].word[i]);
}
应替换为以下
for (i = 0; strlen(dictionary[i].word); i++) {
strcpy(dictionary2[i].word,dictionary[i].word);
}
答案 2 :(得分:0)
花了一个白色来弄清楚你想要做什么?使用结构对urnary操作符的使用毫无意义。我仍然可能没有100%正确,但似乎所有代码都是尝试对结构进行排序。我不知道你是否有限制强迫你尝试手动排序,但通常的做法是我们使用像qsort
之类的算法为你排序结构。现在,您可以对整个结构进行排序,但这将首先对空元素进行排序。要避免此问题,请仅向qsort
发送包含单词的元素数。确定填充元素数量的快速入侵只是检查words
中的第一个字符是否存在。结合上述所有内容,您的dictionary
结构可以简化为:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct entry
{
char word[15];
char definition[50];
};
int dictcompare (const void *a, const void *b)
{
struct entry *sa = (struct entry *)a;
struct entry *sb = (struct entry *)b;
return strcmp (sa-> word, sb-> word);
}
int main (void) {
size_t num_entries = 0;
size_t i = 0;
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"}};
/* count the number of filled entries in dictionary */
while (*dictionary[num_entries++].word);
num_entries--;
/* print the unsorted entries */
printf ("\ndictionary:\n\n");
for (i = 0; i < num_entries; i++)
printf (" %-15s : %s\n", dictionary[i].word, dictionary[i].definition);
/* sort the dictionary entries using qsort */
qsort (dictionary, num_entries, sizeof (struct entry), dictcompare);
/* print the unsorted entries */
printf ("\ndictionary (sorted):\n\n");
for (i = 0; i < num_entries; i++)
printf (" %-15s : %s\n", dictionary[i].word, dictionary[i].definition);
return 0;
}
<强>输出强>
$ ./bin/struct_dictionary
dictionary:
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
dictionary (sorted):
aardvark : a burrowing African mammal
abyss : a bottomless pit
acumen : mentally sharp; keen
addle : to become confused
aerie : a high nest
affix : to attach
agar : a jelly made of seaweed
ahoy : a nautical call of greeting
aigrette : an ornamental cluster of feathers
ajar : partially opened