英文:
Changing a double pointer which is in a struct
问题
I'm trying to change the x value in this code but I'm getting segmentation fault.
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int **x;
} Type;
int main() {
int a = 1;
Type *type = malloc(sizeof(Type));
type->x[0] = &a;
return 0;
}
英文:
I'm trying to change the x value in this code but I'm getting segmentation fault.
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int **x;
} Type;
int main() {
int a = 1;
Type *type = malloc(sizeof(Type));
type->x[0] = &a;
return 0;
}
答案1
得分: 0
if you want an array of pointers to ints
int main() {
int a = 1;
Type *type = malloc(sizeof(Type));
type->x = malloc(sizeof(int*) * 10)) ;// say we need 10
type->x[0] = &a;
return 0;
}
英文:
if you want an array of pointers to ints
int main() {
int a = 1;
Type *type = malloc(sizeof(Type));
type->x = malloc(sizeof(int*) * 10)) ;// say we need 10
type->x[0] = &a;
return 0;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论