c - 访问函数中的双指针结构

标签 c pointers memory-management malloc dynamic-memory-allocation

我试图使用双指针结构作为结构数组。 当我在 main() 中编写整个代码时它工作正常。 下面是工作代码:

xkumapu@lxapp-2 cat struct2.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>

typedef struct conn_s{
    int no;
    char name[32];
}conn_t;
int main(){
    int i=0, n;
    conn_t **connections;
    printf("How many students?\n");
    scanf("%d", &n);
    for(i=0;i<n;i++){

    connections[i] = (conn_t*)malloc(sizeof(conn_t));
            printf("Enter no,name of student\n");

            scanf("%d%s", &connections[i]->no, &connections[i]->name);
    }

    printf("The student details you entered are");
    for(i=0;i<n;i++){
            printf("%d      %s", connections[i]->no, connections[i]->name);
            free(connections[i]);
    }

    return 1;
}

xkumapu@lxapp-2
xkumapu@lxapp-2 ./struct2
How many students?
3
Enter no,name of student
1 pavan
Enter no,name of student
2 suresh
Enter no,name of student
3 ramesh
The student details you entered are1      pavan2      suresh3      ramesh

但是,当我在一个函数中使用相同的代码时,它不起作用。

xkumapu@lxapp-2 cp struct2.c  struct1.c
xkumapu@lxapp-2 vi struct1.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>

typedef struct conn_s{
    int no;
    char name[32];
}conn_t;
int data(){
    int i=0, n;
    conn_t **connections;
    printf("How many students?\n");
    scanf("%d", &n);
    for(i=0;i<n;i++){

    connections[i] = (conn_t*)malloc(sizeof(conn_t));
            printf("Enter no,name of student\n");

            scanf("%d%s", &connections[i]->no, &connections[i]->name);
    }

    printf("The student details you entered are");
    for(i=0;i<n;i++){
            printf("%d      %s", connections[i]->no, connections[i]->name);
            free(connections[i]);
    }

    return 1;
}

int main(){
    data();
return 1;
}
Entering Ex mode.  Type "visual" to go to Normal mode.
:wq
"struct1.c" 41L, 874C written
xkumapu@lxapp-2 gcc -o struct123 struct1.c
xkumapu@lxapp-2 ./struct123
How many students?
3
Segmentation fault
xkumapu@lxapp-2

你能帮我理解这个问题吗?

最佳答案

connections[i] = ...

这一行从变量 connections 读取,这是未初始化的。这会导致两个程序出现未定义的行为。 (这意味着任何事情都有可能发生,包括看起来“工作正常”。)

你可以通过这样做来解决这个问题

connections = malloc(n * sizeof *connections);

在循环之前。


顺便说一句:

  • <malloc.h>不是标准标题
  • 你不应该投malloc
  • 您应该检查错误(scanf 可能失败,malloc 可能失败,等等)
  • 1不是可移植退出状态/main返回值(1 表示 unix 和 windows 上的错误,但唯一的可移植代码是 0EXIT_SUCCESS 表示成功,EXIT_FAILURE 表示错误)。
  • 路过&connections[i]->namescanf %s错误:%s需要 char *但是&connections[i]->namechar (*)[32] (要解决此问题,请删除 & )

关于c - 访问函数中的双指针结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32419908/

相关文章:

c++ - 删除放置或非放置新

C - cs50.h GetString 错误

c++ - 使用 SSE 或 SSE3 在 ushort 数组中添加 uchar 值

c++ - 打印变量地址

C++ 使用指针传递动态创建的数组

c++ - 这段代码会做什么? (内存管理)

c - C编程内存复制和删除失败

c - 使用堆栈迭代快速排序

将输入的字符串与打印的函数进行比较

C 指针列表(双指针)