c - 右对齐另一个结构内部的结构内部的 int
c - Right justifying an int inside of a struct inside of another struct
如何从结构中的结构打印出某些内容。我想打印出“$30”。我收到分段错误。
typedef struct {
int cost;
} prod_t;
typedef struct {
prod_t *c;
} a_t;
int
main(int agrc, char **argv){
a_t *storage = NULL;
char buffer[1000];
storage->c->cost = 30;
sprintf(buffer, "$%d", storage->c->cost);
printf("%6s\n",buffer);
return 0;
}
a_t *storage = NULL;
您需要为所有指针分配内存。取消引用 uninitialized/NULL 指针会导致未定义的行为。
a_t *storage = malloc(sizeof(a_t));
a_t->c = malloc(sizeof(prod_t));
如何从结构中的结构打印出某些内容。我想打印出“$30”。我收到分段错误。
typedef struct {
int cost;
} prod_t;
typedef struct {
prod_t *c;
} a_t;
int
main(int agrc, char **argv){
a_t *storage = NULL;
char buffer[1000];
storage->c->cost = 30;
sprintf(buffer, "$%d", storage->c->cost);
printf("%6s\n",buffer);
return 0;
}
a_t *storage = NULL;
您需要为所有指针分配内存。取消引用 uninitialized/NULL 指针会导致未定义的行为。
a_t *storage = malloc(sizeof(a_t));
a_t->c = malloc(sizeof(prod_t));