C、从链表中打印多个整数
我对 C 很陌生,所以我不完全确定出了什么问题。我不知道如何在函数中打印多个整数值。
添加函数:
void add(char *name,int id,int copies)
{
/* Pointer to next item */
struct list *newAlbum;
newAlbum = malloc(sizeof(struct list));
strcpy((*newAlbum).name, name); // Set album name
newAlbum->id = id;
newAlbum->copies = copies;
newAlbum->pNext = pFirst;
pFirst = newAlbum;
}
show function:
void show()
{
system("clear");
struct list *current_node;
current_node = pFirst;
while(current_node != NULL)
{
printf("Album #%d \n",current_node->id);
printf("Album Name: %s \n",current_node->name);
printf("Album Copies:%d \n",current_node->copies);
printf("\n");
current_node=current_node->pNext;
}
}
我的程序打印出current_node->id,就好像它是current_node->copies,而current_node->copies打印出为134516043,这显然是错误的。
我想我一定是向函数传递了一些错误的东西或者其他东西,但我无法弄清楚。有什么建议吗?
我这样调用函数 add:
add(name,id,copies);
列表如下:
/* THE LIST */
struct list{
char name[52];
int id;
int copies;
int sold;
struct list* pNext;
};
struct list *pFirst = NULL;
我使用这段代码通过用户输入调用该函数:
printf("Enter the name of the new album. \n");
scanf("%s",&name);
printf("Enter the album id. \n");
scanf("%d",&id);
printf("Enter number of copies. \n");
scanf("%d," &copies);
// Pass data to add()
add(name,id,copies);
I'm very new to C, so I'm not totally sure what's the matter. I can't figure out how to print more than a single integer value in a function.
add function:
void add(char *name,int id,int copies)
{
/* Pointer to next item */
struct list *newAlbum;
newAlbum = malloc(sizeof(struct list));
strcpy((*newAlbum).name, name); // Set album name
newAlbum->id = id;
newAlbum->copies = copies;
newAlbum->pNext = pFirst;
pFirst = newAlbum;
}
show function:
void show()
{
system("clear");
struct list *current_node;
current_node = pFirst;
while(current_node != NULL)
{
printf("Album #%d \n",current_node->id);
printf("Album Name: %s \n",current_node->name);
printf("Album Copies:%d \n",current_node->copies);
printf("\n");
current_node=current_node->pNext;
}
}
My program prints out the current_node->id as if it were current_node->copies, and current_node->copies is printed out as 134516043, which is obviously, wrong.
I think I must be passing something wrong to the function or something, but I can't figure it out. Any tips?
I call the function add like this:
add(name,id,copies);
The list is as so:
/* THE LIST */
struct list{
char name[52];
int id;
int copies;
int sold;
struct list* pNext;
};
struct list *pFirst = NULL;
I call the function with user input with this piece of code:
printf("Enter the name of the new album. \n");
scanf("%s",&name);
printf("Enter the album id. \n");
scanf("%d",&id);
printf("Enter number of copies. \n");
scanf("%d," &copies);
// Pass data to add()
add(name,id,copies);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只要您不将长度超过 51 个字符的专辑名称传递给
add()
,您所显示的代码就可以。如果你这样做,你会得到非常奇怪的输出,并且可能会崩溃。为了防止这种情况,您应该使用长度有限的副本 - 例如:(
请注意
sizeof *newAlbum
比sizeof(struct list)
好一点,因为前者在阅读该行时“显然是正确的” - 如果newAlbum
的类型发生更改,它仍然是正确的)。Your code that you've shown is OK, as long as you don't pass an album name to
add()
which is longer than 51 characters. If you do, you'll get very weird output, and possibly a crash.To guard against this, you should use a length-limited copy - for example:
(note that
sizeof *newAlbum
is a little better thansizeof(struct list)
, since the former is "obviously correct" when reading the line - it will still be corret if the type ofnewAlbum
is ever changed).我在这里看到的唯一错误是您没有检查
name
的长度。您应该使用:strncpy(newAlbum->name, 52, name);
这将防止
name
缓冲区溢出。The only thing I can see wrong here is that you don't check the length of
name
. You should use:strncpy(newAlbum->name, 52, name);
This will prevent overrunning the
name
buffer.