具有通过 void 函数调用分配的指针字段的结构

Struct with pointer fields allocated over void function call

我有一个如下所示的结构,我想在 void 函数中分配 structA 的成员,如下所示。基本上想使用指针在 void 函数上分配结构大小,然后在其他函数中使用该结构指针来访问字段。下面是我想如何编写代码的示例,但不清楚指针和结构的用法。在打印函数头中,我是使用结构指针还是只使用结构本身?

typedef struct A {  
    int *x;  
    int *y;  
} A;  

void allocateStruct(int sizeN, A *aType);
void printInfo(A aType);
  
int main() {   
    A genericA;  
    allocateStruct(5, genericA);  
    int x[5] = {2, 3, 4, 5, 6};  
    int y[5] = {12, 36, 40, 52, 23};   

    genericA.x = x;  
    genericA.y = y;  
    printInfo(genericA);   
}  

void allocateStruct(int sizeN, A* aType) {  
    aType.x = (int)malloc(sizeN * sizeof(int));   
    aType.y = (int)malloc(sizeN * sizeof(int));    
}  
 
void printInfo(A genericP) {
    ......  
}  

如果我编译你的代码

#include <stdlib.h> 

typedef struct A {  
    int *x;  
    int *y;  
} A;  

void allocateStruct(int sizeN, A *aType);
void printInfo(A aType);
  
int main() {   
    A genericA;  
    allocateStruct(5, genericA);  
    int x[5] = {2, 3, 4, 5, 6};  
    int y[5] = {12, 36, 40, 52, 23};   

    genericA.x = x;  
    genericA.y = y;  
    printInfo(genericA);   
}  

void allocateStruct(int sizeN, A* aType) {  
    aType.x = (int)malloc(sizeN * sizeof(int));   
    aType.y = (int)malloc(sizeN * sizeof(int));    
}  
 
void printInfo(A genericP) {
    /* */  
}  

我明白了

main.c: In function 'main':
main.c:13:23: error: incompatible type for argument 2 of 'allocateStruct'
     allocateStruct(5, genericA);
                       ^~~~~~~~

所以我调整调用以实际提供原型要求的指针。

#include <stdlib.h> 

typedef struct A {  
    int *x;  
    int *y;  
} A;  

void allocateStruct(int sizeN, A *aType);
void printInfo(A aType);
  
int main() {   
    A genericA;  
    allocateStruct(5, &genericA);  
    int x[5] = {2, 3, 4, 5, 6};  
    int y[5] = {12, 36, 40, 52, 23};   

    genericA.x = x;  
    genericA.y = y;  
    printInfo(genericA);   
}  

void allocateStruct(int sizeN, A* aType) {  
    aType.x = (int)malloc(sizeN * sizeof(int));   
    aType.y = (int)malloc(sizeN * sizeof(int));    
}  
 
void printInfo(A genericP) {
    /* */  
}  

然后我得到:

main.c: In function 'allocateStruct':
main.c:23:10: error: 'aType' is a pointer; did you mean to use '->'?
     aType.x = (int)malloc(sizeN * sizeof(int));
          ^

所以我就是这么做的:

#include <stdlib.h> 

typedef struct A {  
    int *x;  
    int *y;  
} A;  

void allocateStruct(int sizeN, A *aType);
void printInfo(A aType);
  
int main() {   
    A genericA;  
    allocateStruct(5, &genericA);  
    int x[5] = {2, 3, 4, 5, 6};  
    int y[5] = {12, 36, 40, 52, 23};   

    genericA.x = x;  
    genericA.y = y;  
    printInfo(genericA);   
}  

void allocateStruct(int sizeN, A* aType) {  
    aType->x = (int)malloc(sizeN * sizeof(int));   
    aType->y = (int)malloc(sizeN * sizeof(int));    
}  
 
void printInfo(A genericP) {
    /* */  
} 

然后我得到:

main.c: In function 'allocateStruct':
main.c:23:16: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
     aType->x = (int)malloc(sizeN * sizeof(int));
                ^

所以我按照 C 的一般建议进行操作,而不是强制转换 malloc:

#include <stdlib.h> 

typedef struct A {  
    int *x;  
    int *y;  
} A;  

void allocateStruct(int sizeN, A *aType);
void printInfo(A aType);
  
int main() {   
    A genericA;  
    allocateStruct(5, &genericA);  
    int x[5] = {2, 3, 4, 5, 6};  
    int y[5] = {12, 36, 40, 52, 23};   

    genericA.x = x;  
    genericA.y = y;  
    printInfo(genericA);   
}  

void allocateStruct(int sizeN, A* aType) {  
    aType->x = malloc(sizeN * sizeof(int));   
    aType->y = malloc(sizeN * sizeof(int));    
}  
 
void printInfo(A genericP) {
    /* */  
} 

结果:没有错误,没有警告。

现在测试,使用给定的打印函数原型和我能想到的第一个实现:

#include <stdlib.h>
#include <stdio.h>

typedef struct A {  
    int *x;  
    int *y;  
} A;  

void allocateStruct(int sizeN, A *aType);
void printInfo(A aType);
  
int main() {   
    A genericA;  
    allocateStruct(5, &genericA);  
    int x[5] = {2, 3, 4, 5, 6};  
    int y[5] = {12, 36, 40, 52, 23};   

    genericA.x = x;  
    genericA.y = y;  
    printInfo(genericA);   
}  

void allocateStruct(int sizeN, A* aType) {  
    aType->x = malloc(sizeN * sizeof(int));   
    aType->y = malloc(sizeN * sizeof(int));    
}  
 
void printInfo(A genericP) {
    printf("%i %i\n",  genericP.x[0], genericP.y[0] );
} 

我得到了令人满意的输出:

2 12

或与您提到的替代方案相同的输出:

void printInfo2(A* genericP) {
    printf("%i %i\n",  genericP->x[0], genericP->y[0] );
} 

称为 printInfo2(&genericA);

诚然,正如 yano 正确指出的那样,这有点太直截了当了。它从 main 覆盖分配的指针,这表示内存泄漏。

我不得不猜测目的,但我同意 yanos 的假设,即实际上某些值的复制(而不是指针)最有意义。

作为单个值的例子(保留大部分分配的内存未初始化,如果你不填写/* more */),在main()中做:

    genericA.x[0] = x[0];  
    genericA.y[0] = y[0];  
    /* more */

同样的输出。