2D结构阵列 [英] 2D array of structures

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

问题描述

您好,


我想知道如何使用realloc()调整这些结构数组的大小?


#include< stdio.h> ;

#include< stdlib.h>

#define FIRST 7


typedef struct {

char * name;

int i;

int j;

}结构;


STRUCTURE ** p_structure;


int main(){


p_structure =(STRUCTURE **)malloc(FIRST * sizeof(STRUCTURE) );

if(p_structure == NULL){

printf(无法分配内存,退出...);

返回1;

}

}


提前谢谢


Svata

Hello,

I wonder how to resize such array of structures using realloc()?

#include <stdio.h>
#include <stdlib.h>
#define FIRST 7

typedef struct {
char *name;
int i;
int j;
} STRUCTURE;

STRUCTURE **p_structure;

int main() {

p_structure = (STRUCTURE **) malloc(FIRST * sizeof(STRUCTURE));
if ( p_structure == NULL ) {
printf("Failed to allocate memory, exiting...");
return 1;
}
}

Thank you in advance

Svata

推荐答案

svata< sv ****** @ centrum.czwrote:
svata <sv******@centrum.czwrote:

我想知道如何使用realloc()调整这样的结构数组?
I wonder how to resize such array of structures using realloc()?


#include< stdio.h>

#include< stdlib.h>

#define FIRST 7
#include <stdio.h>
#include <stdlib.h>
#define FIRST 7


typedef struct {

char * name;

int我;

int j;

}结构;
typedef struct {
char *name;
int i;
int j;
} STRUCTURE;


STRUCTURE ** p_structure;
STRUCTURE **p_structure;


int main(){
int main() {



Make that

int main(void)


因为你的main()不带参数。

Make that

int main( void )

since your main() takes no arguments.


p_structure =(结构**)malloc(FIRST * sizeof(STRUCTURE));
p_structure = (STRUCTURE **) malloc(FIRST * sizeof(STRUCTURE));



不要强制转换malloc()的返回值,它只会隐藏你的错误

如果你忘记包含< stdlib.h中取代。但是,更重要的是,

你在这里为7(FIRST)这样的结构分配内存。 malloc()返回

a指向此内存开头的指针,其类型为''STRUCTURE *''

(malloc()实际上返回一个void指针但是 STRUCTURE *''是指向该内存的指针的

正确类型。但是你把它分配给了一个类型为''STRUCTURE **'的指针,而不是
。结合您的

使用单词2D array在subjct行中,导致怀疑

你实际上不想为某些具有类似属性的东西分配内存作为一系列结构,但有些东西更多compli-

cated和类似于此类结构的2D数组。这就是你的b $ b绝对不会得到这个分配,无论你指定malloc()返回值的

指针的类型。所以,而不是

开始猜测你可能有什么打算我认为最好是

要求你更清楚地说明你打算在这里做什么:do

你只想要一个简单的结构集合的内存,或者你想要这样的结构的二维数组的
。在第一种情况下,你可以修复美元。你的程序只需将''p_structure''定义为


STTRUCTURE * p_structure;


也许你也可能想要替换


p_structure = malloc(FIRST * sizeof * p_structure);


因为那个你不必改变那条线的方式,如果你的话,b $ b应该决定稍后改变''p_structure'的类型。


你还写了一些关于realloc()的内容,但我看不到任何

在您发布的代码中使用或提及realloc()。但是,

当然,你可以使用realloc()来调整你从malloc()获得的内存量
- 这就是realloc()的发明

for。

Don''t cast the return value of malloc(), it only hides your mistake
should you have forgotten to include <stdlib.h>. But, more important,
you allocate here memory for 7 (FIRST) such structures. malloc() returns
a pointer to the start of this memory, which is of type ''STRUCTURE *''
(malloc() actually returns a void pointer but "STRUCTURE *'' is the
correct type of a pointer to that memory). But you assign it instead
to a pointer that has type ''STRUCTURE **''. That, combined with your
use of the words "2D array" in the subjct line, leads to the suspicion
that you actually don''t want to allocate memory for something that has
similar properties as an array of stuctures, but something more compli-
cated and similar to a 2D array of such structures. And that''s what you
definitely won''t get with that allocation, whatever the type of the
pointer you assign the return value of malloc() to. So, instead of
starting to guess what you might have intended I think it''s better to
ask you to specify a bit more clearly what you intend to do here: do
you just want memory for a simple set of structures or do you want
something like a 2D array of such structures. In the first case you
could "repair" your program by simply defining ''p_structure'' as

STTRUCTURE *p_structure;

Perhaps you thenn also might want to replace the line for the allo-
cation by

p_structure = malloc(FIRST * sizeof *p_structure);

because that way you don''t have to change that line anymore if you
should decide to change the type of ''p_structure'' sometime later.

You also write something about realloc() but I can''t see any
use or mentioning of realloc() in the code you posted. But, of
course, you can use realloc() to resize the amount of memory
you obtained from malloc() - that''s what realloc() was invented
for.


if(p_structure == NULL){

printf("无法分配内存) ,退出...);

返回1;

}
if ( p_structure == NULL ) {
printf("Failed to allocate memory, exiting...");
return 1;
}



这是好的看看你检查malloc()返回了什么!

It''s good to see that you check that what malloc() returned!


}
}



因为main()返回一个int,这里有一个缺失的行返回

值...

问候,Jens

-

\ Jens Thoms Toerring ___ jt@toerring.de

\ __________________________ http://toerring.de


Jens Thoms Toerring wrot e:
Jens Thoms Toerring wrote:

svata< sv ****** @ centrum.czwrote:
svata <sv******@centrum.czwrote:



< ; snip>

<snip>


> p_structure =(STRUCTURE **)malloc(FIRST * sizeof(STRUCTURE));
>p_structure = (STRUCTURE **) malloc(FIRST * sizeof(STRUCTURE));



< snip>

<snip>


指向该内存的指针的正确类型。但是你把它分配给了一个类型为''STRUCTURE **'的指针,而不是
。结合您的

使用单词2D array在subjct行中,导致怀疑

你实际上不想为某些具有类似属性的东西分配内存作为一系列结构,但有些东西更多compli-

cated和类似于此类结构的2D数组。这就是你的b $ b
correct type of a pointer to that memory). But you assign it instead
to a pointer that has type ''STRUCTURE **''. That, combined with your
use of the words "2D array" in the subjct line, leads to the suspicion
that you actually don''t want to allocate memory for something that has
similar properties as an array of stuctures, but something more compli-
cated and similar to a 2D array of such structures. And that''s what you



< snip>


我同意你所说的,但是在发布回来之前,OP还应该阅读comp.lang.c常见问题解答中的第6节

。第6节包括为二维数组之类的东西动态分配空间的方式。


常见问题解答可在 http://c-faq.com找到/

-

Flash Gordon

<snip>

I agree with what you''ve said, but the OP should also read section 6 of
the comp.lang.c FAQ before posting back here. Section 6 includes ways of
dynamically allocating space for things that work like 2D arrays. The
FAQ can be found at http://c-faq.com/
--
Flash Gordon


svata:
svata:

STRUCTURE ** p_structure;
STRUCTURE **p_structure;



这是一个指向STRUCTURE的指针。


(我反对使用ALL CAPS除了宏之外的其他任何东西)


This is a pointer to a pointer to a STRUCTURE.

(I''m against the use of ALL CAPS for anything other than macros)


int main(){


p_structure =(STRUCTURE **) malloc(FIRST * sizeof(STRUCTURE));
int main() {

p_structure = (STRUCTURE **) malloc(FIRST * sizeof(STRUCTURE));



这没有意义。


你有一个指向STRUCTURE指针的指针。这会向我建议

你将要存储下列任何一个:


(1)指向a的指针的地址结构。

(2)数组的第一个元素的地址,其中每个元素

是指向结构的指针。


如果它是上述两个中的任何一个,则为malloc。调用应该看起来像

类似于:


p_structure = malloc(sizeof(STRUCTURE *));

或:

p_structure = malloc(12 * sizeof(STRUCTURE *));


请注意,我使用的是STRUCTURE *的大小。 $

当然,最好把它们中的两个写成:


p_structure = malloc(sizeof * p);


p_structure = malloc(12 * sizeof * p);


-


Frederick Gotham


This doesn''t make sense.

You have a pointer to a pointer to a STRUCTURE. This would suggest to me
that you''re going to store either of the following in it:

(1) The address of a pointer to a STRUCTURE.
(2) The address of the first element of an array, whereby each element
is a pointer to a STRUCTURE.

If it were either of the two above, the "malloc" invocation should look
something like:

p_structure = malloc( sizeof(STRUCTURE*) );
or:
p_structure = malloc( 12 * sizeof(STRUCTURE*) );

Note that I use the size of a "STRUCTURE*" rather than the size of a
"STRUCTURE".

Of course, it''s better to write the two of them as:

p_structure = malloc(sizeof*p);

p_structure = malloc(12 * sizeof*p);

--

Frederick Gotham


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

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