c - 将结构值传递给函数

标签 c

#include<stdio.h>

struct classifier
{
    char src_address[15];
    char dst_address[15];
    int src_port;
    int  dst_port;
};

void display(struct classifier *ptr)
{
    printf("\n%s", ptr->src_address );
    printf("\n%s", ptr->dst_address );
    printf("\n%d", ptr->src_port);
    printf("\n%d", ptr->dst_port );
}

main()
{
    int i;
    struct classifier *ptr[4];
    for(i=0;i<2;i++)
    {
        scanf("%s",ptr[i]->src_address);
        scanf("%s",ptr[i]->dst_address);
        scanf("%d",&ptr[i]->src_port);
        scanf("%d",&ptr[i]->dst_port);
        display(ptr[i]);
    }
    return 0;
}

我想在函数中显示输出。当我第一次输入数据时,我得到了正确的显示。当我第二次输入数据时,它显示段错误。代码有什么问题?据我所知,我已经正确地声明了指针。请帮忙。

最佳答案

你需要一个像这样的结构数组

struct classifier ptr[4];

关于c - 将结构值传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4713003/

相关文章:

c++ - 如何将二进制表示字节转储为十进制字符串?

c - 使用 C/Pthreads : do shared variables need to be volatile?

c - 是否有用于引用/转义表名的 C SQLite API?

c - MPI Irecv 无法正确接收缓冲区的第一个元素?

c - C 中的嵌入式 SQL,有没有办法使表名可变?

c - 如何在 opengl 中正确地构造四边形

c++ - 我如何知道 k-server 动态解决方案的最佳路径位于数组 cost[ i ][ j ][ k ][ t ] 中的什么位置?

c - 用 gdb 调试 c 程序以显示十六进制地址

c - Arduino Esplora 乒乓球游戏

c - fread() 中的索引 : Is there a way to set the minimum index to be returned?