嘿,伙计们,我需要你们的帮助。我正在为两天后的c级考试做准备。为此,我为一个简单的列表编写了一小段代码。我的问题是,我得到每次相同的错误:&;函数'copyString'的隐式声明"。你能告诉我我犯了什么错误吗?
在德国我们说:"Liebe gr
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct listen{
int id;
int wert;
char *namen;
char *strasse;
int plz;
struct listen *next;
};
typedef struct listen elem;
elem *createElem(int id, int wert){
elem* newElem = (elem*)malloc(sizeof(elem));
if(newElem == NULL) printf("War nischt!");
newElem->id = id;
newElem->wert = wert;
newElem->namen = NULL;
newElem->strasse = NULL;
newElem->plz = 0;
return newElem;
}
int stringLen(char* len){
int d=0;
while(*len++) d++;
return d;
}
char *createStr(char* st)
{
char* newSt;
newSt = (char*)malloc(stringLen(st)+1);
if(newSt == NULL) {printf("Fehler");};
copyString(st, newSt);
return newSt;
}
void copyString(char *von, char *nach)
{
while(*von!=' ')
{
*nach=*von;
*von++;
*nach++;
}
*nach = ' ';
}
elem* addToList(elem *list, int wert, char* namen, char* strasse, int plz)
{
elem* p = createElem(list->id, wert);
p->namen = createStr(namen);
p->strasse = createStr(strasse);
p->plz = plz;
list->id++;
p->next = list->next;
list->next = p;
return list;
}
elem* createList(){
elem* liste = createElem(0, 0);
return liste;
}
void PrintElem(Elem *E)
{
printf("ID: %d | Wert: %d | Name : %s | Strasse: %s | PLZ: %d", E->id, E->wert, E->namen, E->strasse, E->plz);
}
void printList(elem* list){
printf("Liste:");
while(list->next != NULL)
{
printElem(list->next);
list = list->next;
}
}
int main(){
elem* Liste01 = createList();
Liste01 = addToList(Liste01, 4, "Peter", "Lustigweg", 15834);
printList(Liste01);
return 0;
}
我尝试重新安排声明的函数。
考虑下面的代码来打印一个范围中的奇数和偶数。
#include <stdio.h>
void print_evens(int start, int stop) {
if (start > stop) return;
if (start % 2 == 0) {
printf("Even: %dn", start);
print_odds(start+1, stop);
}
else {
print_odds(start, stop);
}
}
void print_odds(int start, int stop) {
if (start > stop) return;
if (start % 2 == 1) {
printf("Odd: %dn", start);
print_evens(start+1, stop);
}
else {
print_evens(start, stop);
}
}
int main(void) {
print_evens(1, 10);
}
这会给我同样的错误。print_evens
函数不知道print_odds
起作用。如果我们把它们调换一下,我们会得到同样的问题。
main
作为我们实现的第一个函数。
#include <stdio.h>
void print_evens(int start, int stop);
void print_odds(int start, int stop);
int main(void) {
print_evens(1, 10);
}
void print_evens(int start, int stop) {
if (start > stop) return;
if (start % 2 == 0) {
printf("Even: %dn", start);
print_odds(start+1, stop);
}
else {
print_odds(start, stop);
}
}
void print_odds(int start, int stop) {
if (start > stop) return;
if (start % 2 == 1) {
printf("Odd: %dn", start);
print_evens(start+1, stop);
}
else {
print_evens(start, stop);
}
}
我试了一下你的代码,正如上面好的评论所指出的,函数在使用"createStr"函数。一旦我完成了这一点重构,我就能够编译程序并执行程序了。然而,在查看代码时,我对它在列表结构中的遍历有一些疑问。很明显,在这个链表中有两个元素;但是,只打印第二个列表元素。而且,元素ID似乎没有进入其预期的列表元素。因此,我在链表打印函数中添加了一些额外的代码,以便可以查看初始链表元素。以下是代码的重构版本。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct listen
{
int id;
int wert;
char *namen;
char *strasse;
int plz;
struct listen *next;
};
typedef struct listen elem;
void copyString(char *von, char *nach)
{
while(*von!=' ')
{
*nach=*von;
*von++;
*nach++;
}
*nach = ' ';
}
elem *createElem(int id, int wert)
{
elem* newElem = (elem*)malloc(sizeof(elem));
if(newElem == NULL) printf("War nischt!n");
newElem->id = id;
newElem->wert = wert;
newElem->namen = NULL;
newElem->strasse = NULL;
newElem->plz = 0;
return newElem;
}
int stringLen(char* len)
{
int d=0;
while(*len++) d++;
return d;
}
char *createStr(char* st)
{
char* newSt;
newSt = (char*)malloc(stringLen(st)+1);
if(newSt == NULL)
{
printf("Fehlern");
}
copyString(st, newSt);
return newSt;
}
void addToList(elem *list, int wert, char* namen, char* strasse, int plz) /* FYI - made this a void function as the linked list pointer is already known and doesn't change */
{
elem* p = createElem(list->id, wert);
p->namen = createStr(namen);
p->strasse = createStr(strasse);
p->plz = plz;
list->id++;
p->next = list->next;
list->next = p;
return;
}
elem* createList()
{
elem* liste = createElem(0, 0);
return liste;
}
void printElem(elem *E)
{
printf("ID: %d | Wert: %d | Name : %s | Strasse: %s | PLZ: %dn", E->id, E->wert, E->namen, E->strasse, E->plz);
}
void printList(elem* list)
{
printf("Liste:");
printf("Data within initial list elementn"); /* Just to illustrate that the initial list element is not used */
printElem(list);
while(list->next != NULL)
{
printElem(list->next);
list = list->next;
}
}
int main()
{
elem* Liste01 = createList();
addToList(Liste01, 4, "Peter", "Lustigweg", 15834);
printList(Liste01);
return 0;
}
加上额外的打印信息,下面是在终端上的输出:
@Vera:~/C_Programs/Console/DeutschList/bin/Release$ ./DeutschList
Liste:Data within initial list element
ID: 1 | Wert: 0 | Name : (null) | Strasse: (null) | PLZ: 0
ID: 0 | Wert: 4 | Name : Peter | Strasse: Lustigweg | PLZ: 15834
可以看出,添加第二个链表元素时,ID值存储在第一个链表元素中;但是,其他数据元素放到下一个链表元素中。
我将留给您改进代码,使初始链表元素包含初始数据,但看看这是否使您按照项目的精神前进。