我自己一直在通过Kochan的“C编程”第3版,为明年的研究生做好准备,我第一次陷入困境。我是一个远离指针的章节,但是最近关于字符串的章节末尾的练习有一个问题,我自己的研究似乎只能通过使用指针来解决。
问题与数据结构entry
:
struct entry {
char word[15];
char definition[50];
};
我们创建了一系列条目:
struct entry dictionary[10] =
{{"aardvark", "a burrowing African mammal"},
{"abyss", "a bottomless pit"},
{"addle", "to become confused"},
{"aerie", "a high nest"},
{"ajar", "partially opened"},
{"acumen", "mentally sharp; keen"},
{"affix", "to append; attach"},
{"agar", "a jelly made from seaweed"},
{"ahoy", "a nautical call of greeting"},
{"aigrette", "an ornamental cluster of feathers"}};
提示符显示:“编写一个名为dictionary_sort
的函数,按照字母顺序对字典(如上所定义)进行排序。”
我知道结构和数组与函数有关的细微之处,以及函数如何将它们作为参数或作为返回值返回。对我来说似乎唯一有用的方法是返回一个结构,或者特别是一个结构数组,但我不认为我在这里正确应用它:
struct entry dictionary_sort(struct entry dictionary)
总的来说,我目前的程序版本如下:
#include <stdio.h>
#include <stdbool.h>
struct entry {
char word[15];
char definition[50];
};
// Function to compare two character strings
int compare_strings(const char s1[], const char s2[])
{
int i = 0, answer;
while (s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0')
i++;
if (s1[i] < s2[i])
answer = -1; // s1 < s2
else if (s1[i] == s2[i])
answer = 0; // s1 == s2
else
answer = 1; // s1 > s2
return answer;
}
// Function to sort a dictionary structure
struct entry dictionary_sort(struct entry dictionary[])
{
int dictionary_length = sizeof(dictionary) / sizeof(dictionary[0]);
int i, j, minimum;
struct entry temp;
for (i = 0; i < dictionary_length; i++) {
minimum = i;
for (j = i + 1; j < dictionary_length; j++) {
if (compare_strings(dictionary[j].word,
dictionary[minimum].definition) == -1)
minimum = j;
}
temp = dictionary[minimum];
dictionary[minimum] = dictionary[i];
dictionary[i] = dictionary[minimum];
}
return dictionary;
}
int main(void)
{
struct entry dictionary[10] =
{{"aardvark", "a burrowing African mammal"},
{"abyss", "a bottomless pit"},
{"addle", "to become confused"},
{"aerie", "a high nest"},
{"ajar", "partially opened"},
{"acumen", "mentally sharp; keen"},
{"affix", "to append; attach"},
{"agar", "a jelly made from seaweed"},
{"ahoy", "a nautical call of greeting"},
{"aigrette", "an ornamental cluster of feathers"}};
int i, dictionary_length = sizeof(dictionary) / sizeof(dictionary[0]);
dictionary = dictionary_sort(dictionary);
for (i = 0; i < dictionary_length; i++)
printf("%s - %s.\n", dictionary[i].word, dictionary[i].definition);
return 0;
}
字符串比较函数的行为与预期一致,因为它只返回一个整数。我真的不知道如何在不知道指针的情况下获得所需的功能。有足够的带指针的示例,但我很好奇我在这里缺少什么基本原则,因为我觉得书中的其他内容对我来说非常自然。
提前谢谢!
答案 0 :(得分:4)
你根本不需要返回任何内容,甚至不需要显式指针。就地排序,不要重新发明轮子:使用strcmp()
和qsort()
(live demo here):
struct entry dictionary[] = {
{ "def", "second entry" },
{ "abc", "first entry" },
{ "ghi", "third entry" },
{ "mno", "fifth entry" },
{ "jkl", "fourth entry" }
};
int compare_entry(const void *l, const void *r)
{
const struct entry *ll = l;
const struct entry *rr = r;
return strcmp(ll->word, rr->word);
}
#define COUNT(x) (sizeof(x) / sizeof(x[0]))
qsort(dictionary, COUNT(dictionary), sizeof(dictionary[0]), compare_entry);
答案 1 :(得分:1)
虽然不完美并且仍然需要明确定义指针,但这个答案不仅仅是调用库,而是在问题和本书的范围内。
#include <stdio.h>
#include <stdbool.h>
struct entry {
char word[15];
char definition[50];
};
// Function to compare two character strings
int compare_strings(const char s1[], const char s2[])
{
int i = 0, answer;
while (s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0')
i++;
if (s1[i] < s2[i])
answer = -1; // s1 < s2
else if (s1[i] == s2[i])
answer = 0; // s1 == s2
else
answer = 1; // s1 > s2
return answer;
}
// Function to sort a dictionary structure
void dictionary_sort(struct entry *dictionary, int dictionary_length)
{
int i, j, minimum;
struct entry temp;
for (i = 0; i < dictionary_length - 1; i++) {
minimum = i;
for (j = i + 1; j < dictionary_length; j++) {
if (compare_strings(dictionary[j].word,
dictionary[minimum].word) == -1)
minimum = j;
}
temp = dictionary[minimum];
dictionary[minimum] = dictionary[i];
dictionary[i] = temp;
}
}
// Prints the dictionary in its current state
void print_dictionary(struct entry *dictionary, int dictionary_length)
{
int i;
for (i = 0; i < dictionary_length; i++) {
printf("%s - %s.\n", dictionary[i].word, dictionary[i].definition);
}
}
// Demostrates the dictionary_sort function
int main(void)
{
struct entry dictionary[10] =
{{"aardvark", "a burrowing African mammal"},
{"abyss", "a bottomless pit"},
{"addle", "to become confused"},
{"aerie", "a high nest"},
{"ajar", "partially opened"},
{"acumen", "mentally sharp; keen"},
{"affix", "to append; attach"},
{"agar", "a jelly made from seaweed"},
{"ahoy", "a nautical call of greeting"},
{"aigrette", "an ornamental cluster of feathers"}};
int i, dictionary_length = sizeof(dictionary) / sizeof(dictionary[0]);
print_dictionary(&dictionary, dictionary_length);
printf("\nSorting...\n\n");
dictionary_sort(&dictionary, dictionary_length);
print_dictionary(&dictionary, dictionary_length);
printf("\n");
return 0;
}
答案 2 :(得分:0)
使用[]
声明函数参数时,您已经使用了指针。功能定义
int compare_strings(const char s1[], const char s2[])
在功能上与
相同int compare_strings(const char *s1, const char *s2)
与struct entry dictionary_sort(struct entry dictionary[])
相同。
由于您获得指向struct entry
作为参数dictionary
的指针,return dictionary
也会将指针返回给struct entry
。
您对dictionary
所做的所有更改都已在外部可见,因为您修改了数组本身而不是某些本地数组。
答案 3 :(得分:0)
像"Returning an array of structs without pointers in C"
这样的语句在C中是一个悖论,因为在C中传递或返回数组的唯一方法是通过指针。即使看起来像char *foo(char arr_demo[]){}
这样的数组也是如此传递,基本上它是一个指针被传递,因为它减少到char *foo(char *arr_demo){}
。