在C中具有结构的Malloc [英] Malloc with structs in C

查看:46
本文介绍了在C中具有结构的Malloc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构:

struct numbers_struct {
char numbers_array[1000];
};

struct numbers_struct numbers[some_size];

创建struct之后,有一个整数作为输入:

After creating struct, there is an integer number as an input:

scanf("%d",&size);

我需要使用 malloc(size)并指定数组编号的大小.(而不是some_size使用大小)

I need to use malloc(size) and specify the size of the array numbers. (instead of some_size use size)

在C语言中这样的事情可能吗?

Is something like this possible in C?

推荐答案

也许您可以这样做

 struct numbers_struct {
char numbers_array[1000];
};

scanf("%d",&size);

struct numbers_struct *numbers = malloc(sizeof(numbers_struct) * size);

这篇关于在C中具有结构的Malloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆