我从main函数调用init_latent_variables并将指针传递给sample类型的结构SAMPLE
int main(){
    SAMPLE sample;
    sample = read_struct_examples();
    init_latent_variables(&sample);
    return 0;
}
SAMPLE read_struct_examples() {
    SAMPLE sample;        
    sample.examples = (EXAMPLE *) malloc(1*sizeof(EXAMPLE));
    if(!sample.examples) die("Memory error.");
    return(sample); 
}
以下是函数定义。此函数中的内存分配工作正常,但原始变量 inmain保持不变。
void init_latent_variables(SAMPLE *sample) {
    sample->examples[0].h.h_is = (int *) malloc(5*sizeof(int));
    if(!sample->examples[0].h.h_is) die("Memory error.");       
}
以下是结构定义:
typedef struct latent_var {
  int *h_is;
} LATENT_VAR;
typedef struct example {
  LATENT_VAR h;
} EXAMPLE;
typedef struct sample {
  EXAMPLE *examples;
} SAMPLE;
我传递一个指向结构的指针是对的吗?可以在C中这样做吗?
更新:不确定之前出了什么问题。清理并重新编译似乎有效。感谢并为浪费您的时间道歉。已经标记了这个问题,以便版主可以删除它。