[英]Error when when using a struct as function parameter?
I made a function that has a pointer and an integer as parameters. 我做了一个函数,它有一个指针和一个整数作为参数。 Its supposed to print values from a linked list where the pointer points at the first object. 它应该从指针指向第一个对象的链接列表中打印值。 The function looks like: 该函数如下所示:
void printlist(talstrul *lank, int langd)
{ int j;
talstrul *temppek = lank;
for(j=0; j<langd; j++)
{
printf("%d\n",*temppek);
temppek = temppek->next;
}
}
The error I get is: 我得到的错误是:
syntax error : missing ')' before '*'
syntax error : missing '{' before '*'
The struct is defined as follows: 该结构定义如下:
struct talstrul
{
int num;
struct talstrul *next;
};
typedef struct talstrul talstrul;
You appear not to have defined talstrul
(or included the definition here). 您似乎没有定义talstrul
(或在此处包括定义)。 Perhaps it's a struct
(but not a typedef struct
) and you wanted struct talstrul * lank
and struct talstrul * temppek = lank;
也许它是一个struct
(但不是typedef struct
),并且您想要struct talstrul * lank
和struct talstrul * temppek = lank;
. 。
Also this line: 也是这一行:
printf("%d\n",*temppek);
has got to be wrong if temppek
points to a struct
as implied by 如果temppek
指向的struct
暗示了错误,那temppek
是错误的
temppek = temppek->next;
您的错误几乎可以肯定地不在该函数中-您可能在页面上方有一个未终止的块。
You want to print the values in the linked list, ie the num
entries of the structs. 您要打印链接列表中的值,即结构的num
个条目。
printf("%d\n",temppek->num);
To catch these kinds of errors, compiling with gcc -Wall
is good practice. 要捕获此类错误,使用gcc -Wall
编译是一种好习惯。
Edit: Indeed, this snippet works fine, and the problem is elsewhere. 编辑:确实,此代码片段工作正常,问题出在其他地方。
#include <stdio.h>
#include <stdlib.h>
typedef struct talstrul {
int num;
struct talstrul *next;
} talstrul;
void printlist(talstrul *lank, int langd) {
int j;
talstrul *temppek = lank;
for(j=0; j<langd; j++) {
printf("%d\n", temppek->num);
printf("Illogical printing: %d\n",*temppek);
temppek = temppek->next;
}
}
int main (void) {
talstrul *mylank = (talstrul*) malloc (sizeof (talstrul));
mylank->num = 13;
mylank->next = NULL;
printlist(mylank, 1);
free(mylank);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.