C、从链表中打印多个整数

发布于 2024-10-06 17:33:34 字数 1468 浏览 2 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好菇凉咱不稀罕他 2024-10-13 17:33:34

只要您不将长度超过 51 个字符的专辑名称传递给 add(),您所显示的代码就可以。如果你这样做,你会得到非常奇怪的输出,并且可能会崩溃。

为了防止这种情况,您应该使用长度有限的副本 - 例如:(

void add(char *name,int id,int copies)
{
    /* Pointer to next item */
    struct list *newAlbum;

    newAlbum = malloc(sizeof *newAlbum);
    if (newAlbum) {
        snprintf(newAlbum->name, sizeof newAlbum->name, "%s", name);  // Set album name
        newAlbum->id = id;
        newAlbum->copies = copies;
        newAlbum->pNext = pFirst;
        pFirst = newAlbum;
    }
}

请注意 sizeof *newAlbumsizeof(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:

void add(char *name,int id,int copies)
{
    /* Pointer to next item */
    struct list *newAlbum;

    newAlbum = malloc(sizeof *newAlbum);
    if (newAlbum) {
        snprintf(newAlbum->name, sizeof newAlbum->name, "%s", name);  // Set album name
        newAlbum->id = id;
        newAlbum->copies = copies;
        newAlbum->pNext = pFirst;
        pFirst = newAlbum;
    }
}

(note that sizeof *newAlbum is a little better than sizeof(struct list), since the former is "obviously correct" when reading the line - it will still be corret if the type of newAlbum is ever changed).

酒中人 2024-10-13 17:33:34

我在这里看到的唯一错误是您没有检查 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文