初学者:结构...... [英] beginner: structures...

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

问题描述

Cna有人指出我正确的方向,我在.h

文件中有一个结构:


typedef struct mdata

{

int names [500];

int dates [500];

int years [500];

} m_Data;


和我的.c文件中我声明了一个指向这个结构的指针并放在一边

内存来保存它:


m_Data * firstlist;

firstlist =(m_Data *)malloc(sizeof(m_Data));


我是什么我想知道是否有一种方法可以将每个元素从固定大小的500改为用户定义的每个元素?b $ b大小?我猜我可以制作元素指针但是我在每个元素上使用malloc

,如果是这样的话就不会改变
$ b $的大小b struture我已经放弃了初始内存

''malloc(sizeof(m_data))''。你可以看到我有点困惑,任何

想法?


问候,

Rory。

解决方案

鉴于你的原创。下面的结构没有办法改变

内存已经分配给结构元素的
。固定在''500''


如果你想改变mem。分配给

运行时的结构元素你需要先定义原始结构元素

作为指针。

喜欢 -


typedef struct mdata

{

int * names;

int * dates;

int * ages;

} m_Data;


然后在你的程序中你可以分配内存(是的,使用malloc)来支付
取决于其使用场景的那些元素。

Ex:my_Data-> name = malloc(sizeof(m_Data-> name));

[给定my_Data被声明作为指向m_Data的指针。]


获取一本C-book并阅读关于指针的部分!


- Ravi
< blockquote class =post_quotes> Cna有人指出我正确的方向,我在.h
文件中有一个结构:

typedef struct mdata
{
int names [500];
int dates [500];
int ages [500];
} m_Data;

在我的.c文件中我声明了指向这个结构的指针并放在一边
记住它::m_Data * firstlist;
firstlist =(m_Data *)malloc(sizeof(m_Data));

我在想什么如果有一种方法可以将每个元素从固定大小的500改变为用户定义的大小?我猜我可以制作元素指针,但是我在每个元素上使用malloc
,如果是这样的话,那就不会改变我已经放入的
struture的大小抛开
''malloc(sizeof(m_data))''的初始记忆。你可以看到我有点困惑,任何
想法?

问候,
Rory。




嗨Rory,


如果你想在你的结构中想要可变大小的成员那么你就可以选择使用指针(即使用指针) ,malloc和co。)所以你有:


typedef struct mdata

{

int * names;

int * dates;

int * years;

} m_Data;


定义一个可能是好的用于确定结构尺寸的功能,

例如,带原型

m_Data * mdata_init(size_t names_sz,size_t dates_sz,size_t ages_sz);


使用* _sz参数执行必要的mallocs和

返回指向调用者的

指针。

之前你仍然使用(m_Data *)malloc调用将指针

返回给调用者,因为这是占用内存成本的原因

你的int指针和

所以可以一致地确定。


你可能还想写一个mdata_destroy来执行必要的

也可以释放。


如果有疑问只需写几个简短的程序来试试想法

并发布任何

你仍然有疑问。


hth,

ajm。


rory写道:


Cna有人指出我正确的方向,我在.h
文件中有一个结构:

typedef struct mdata
{
int names [500];
int dates [500];
int ages [500];
} m_Data;

和我的.c文件我声明了一个指向这个结构的指针并放下内存来保存它:

m_Data * firstlist;
f irstlist =(m_Data *)malloc(sizeof(m_Data));


#include< stdlib.h>

firstlist = malloc(sizeof * firstlist);


malloc调用不需要引用m_Data。

演员可以隐藏你的失败#include< stdlib.h>


我是什么我想知道它是否
如果有一种方法可以将每个元素从固定大小的500改变为用户定义的大小?


是的。

我猜我可以制作元素指针但是我在每个元素上使用malloc



是的。

如果是这样的话,那就不会改变
struture的大小,我已经把它放在了
''malloc(sizeof(m_data))''。


No.

结构只有三个指针。

你可以看到我有点困惑,任何
想法?




使用像你想的那样的指针。


-

pete


Cna anyone point me in the right direction, I have a struture in my .h
file:

typedef struct mdata
{
int names[500];
int dates[500];
int ages[500];
}m_Data;

and in my .c file I declare a pointer to this strucutre and put aside
memory to hold it:

m_Data* firstlist;
firstlist = (m_Data*)malloc(sizeof(m_Data));

What I''m wondering about it is if there is a way that I can change each
of the elements from being a fixed size of 500 to a being user defined
size? I''m guessing I can make the elements pointers but do I use malloc
on each of the elemnnts, and if so isn''t that changing the size of the
struture in which I have already put aside an initial memory of
''malloc(sizeof(m_data))''. As you can see I''m a little confused, any
ideas?

regards,
Rory.

解决方案

Given your orig. structure below there is no way to change the
memory already
allocated to the structural elements. Its fixed at ''500''

If you want to change the mem. allocated to the structure elements at
runtime you need to define the original structural elements
as pointers first .
like -

typedef struct mdata
{
int *names;
int *dates;
int *ages;
}m_Data;

then in your program you can allocate memory (yes, using malloc) to
those elements depending on its usage scenario.
Ex: my_Data->name = malloc (sizeof (m_Data->name));
[given my_Data is declared as a pointer to m_Data.]

Get a C-book and read the section on pointers !

- Ravi

Cna anyone point me in the right direction, I have a struture in my .h
file:

typedef struct mdata
{
int names[500];
int dates[500];
int ages[500];
}m_Data;

and in my .c file I declare a pointer to this strucutre and put aside
memory to hold it:

m_Data* firstlist;
firstlist = (m_Data*)malloc(sizeof(m_Data));

What I''m wondering about it is if there is a way that I can change each
of the elements from being a fixed size of 500 to a being user defined
size? I''m guessing I can make the elements pointers but do I use malloc
on each of the elemnnts, and if so isn''t that changing the size of the
struture in which I have already put aside an initial memory of
''malloc(sizeof(m_data))''. As you can see I''m a little confused, any
ideas?

regards,
Rory.




Hi Rory,

If you want variable sized members within your structure then you have
no choice but to use pointers (i.e., malloc and co.) so you have:

typedef struct mdata
{
int *names;
int *dates;
int *ages;
} m_Data;

it might be good to define a function to dimension your structure,
e.g., with prototype

m_Data * mdata_init(size_t names_sz, size_t dates_sz, size_t ages_sz);

which uses the *_sz arguments to perform the necessary mallocs and
return the
pointer to the caller. you still use the (m_Data *)malloc call before
returning the pointer
to the caller since this is accounts for the memory cost of holding
your int pointers and
so can be determined consistently.

you might also want to write a mdata_destroy to perform the necessary
frees too.

if in doubt just write a couple of short programs to try out the ideas
and post any
queries you still have.

hth,
ajm.


rory wrote:


Cna anyone point me in the right direction, I have a struture in my .h
file:

typedef struct mdata
{
int names[500];
int dates[500];
int ages[500];
}m_Data;

and in my .c file I declare a pointer to this strucutre and put aside
memory to hold it:

m_Data* firstlist;
firstlist = (m_Data*)malloc(sizeof(m_Data));
#include <stdlib.h>
firstlist = malloc(sizeof *firstlist);

The reference to m_Data is not needed with the malloc call.
The cast can hide your failure to #include <stdlib.h>

What I''m wondering about it is
if there is a way that I can change each
of the elements from being a fixed size of 500
to a being user defined size?
Yes.
I''m guessing I can make the elements pointers but do I use malloc
on each of the elemnnts,
Yes.
and if so isn''t that changing the size of the
struture in which I have already put aside an initial memory of
''malloc(sizeof(m_data))''.
No.
The structure will only hold three pointers.
As you can see I''m a little confused, any
ideas?



Use pointers like you were thinking.

--
pete


这篇关于初学者:结构......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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