结构和动态内存分配 [英] structs and dynamic memory allocation

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

问题描述

你好,

我对结构有疑问。我有一个struct(profil)

有一个指向另一个struct(point)的指针。 struct profil存储

坐标点。

问题是我不知道会有多少积分

最后的每一个结构,所以我必须动态地分配内存

,不幸的是不能使用固定大小的数组。


我想知道是否有更好的方法来访问struct成员(

分)。

使用p1->访问struct成员; p2-> x看起来相当丑陋且不安全。


下面是该程序的精简版,只是为了了解什么

我'我想做。我知道这不是一个纯粹的ANSI C定义

问题,但我无法在这个特殊问题中找到任何帮助

K& R也不在FAQ中。所以我赞成每一个提示。


最好的问候

罗马


#include< stdlib.h>

#include< stdio.h>



结构点

{

双x;

双y;

};


struct profil

{

struct point * pt;

struct profil * previous;

struct profil * next;

};


int main(无效)

{

int n;


struct profil * p1;

struct profil * begin;


n = 2; / *仅为此示例分配内存* /


if((p1 =(struct profil *)malloc(sizeof(struct profil)))== NULL)

{

printf(&\\; \ nMemory allocation failed\\\
");

返回1;

}


begin = p1;


if((p1-> pt =(struct point *)malloc(n * sizeof(struct point)) )== NULL)

{

printf(&\\; \ nMemory allocation failed\\\
);

返回1;

}


/ *将一些信息放入结构* /


p1-> pt-> x = 1265.75;

p1-> pt-> y = 150.73;


p1-> pt ++; / *丑陋,我知道。有一个更好的方法吗? * /


p1-> pt-> x = 550.55;

p1-> pt-> y = 350.33;


p1-> pt--;


/ *用结构* /

/ *代替打印结果,我打印结构* /


printf(" \ nStrukt 1:%f \ n",p1-> pt-> x);

printf(" \ nStrukt 1:%f \ n",p1-> pt-> y);


p1-> pt ++ ;


printf(" \ nStrukt 2:%f \ n",p1-> pt-> x);

printf( \ nStrukt 2:%f \ n,p1-> pt-> y);


返回0;

} < br>

hello,
I do have a question regarding structs. I have a struct (profil) which
has a pointer to another struct (point). The struct profil stores the
coordinates of points.
The problem is that I don''t know how many points
there will be in every struct in the end, so I have to allocate memory
dynamically for
them and can''t use an array of fixed size, unfortunately.

I would like to know if there is a better way to access struct members (the
points).
Accessing struct members with p1->p2->x seems rather ugly and unsafe.

Below is a stripped down version of the program, just to get an idea what
I''m trying to do. I''m aware that this is not a pure ANSI C definition
question, but I couldn''t find any help to this special problem neither in
K&R nor in the FAQ. So I apreciate every hint.

Best Regards
Roman

#include <stdlib.h>
#include <stdio.h>

/* struct with points, used by struct profil */

struct point
{
double x;
double y;
};

struct profil
{
struct point *pt;
struct profil *previous;
struct profil *next;
};

int main(void)
{
int n;

struct profil *p1;
struct profil *begin;

n=2; /* only to allocate memory for this example */

if((p1=(struct profil *) malloc(sizeof(struct profil)))==NULL)
{
printf("\nMemory allocation failed\n");
return 1;
}

begin=p1;

if((p1->pt=(struct point *) malloc(n*sizeof(struct point)))==NULL)
{
printf("\nMemory allocation failed\n");
return 1;
}

/* put some information into the struct */

p1->pt->x=1265.75;
p1->pt->y=150.73;

p1->pt++; /* ugly, I know. Is there a better way to do this? */

p1->pt->x=550.55;
p1->pt->y=350.33;

p1->pt--;

/* do something with the structs */
/* instead of printing results, I print the structs out */

printf("\nStrukt 1: %f\n", p1->pt->x);
printf("\nStrukt 1: %f\n", p1->pt->y);

p1->pt++;

printf("\nStrukt 2: %f\n", p1->pt->x);
printf("\nStrukt 2: %f\n", p1->pt->y);

return 0;
}

推荐答案

Roman Hartmann写道:
Roman Hartmann wrote:
我想知道是否有更好的方法来访问struct成员
(要点)。
使用p1-> p2-> x访问struct成员似乎相当丑陋且不安全。

[。 。 。
/ * struct with points,struct profil * /

struct point
{
double x;
double y;
};

struct profil
{struct struct * pt;
struct profil * previous;
struct profil * next;
} ;

int main(无效)
{
int n;

struct profil * p1;
struct profil * begin;

[。 。 。]
/ *将一些信息放入结构中* /

p1-> pt-> x = 1265.75;
p1-> pt-> y = 150.73;

p1-> pt ++; / *丑陋,我知道。有一个更好的方法吗? * /


是的,这个怎么样:


struct point * pt = p1-> pt;

pt [0] .x = 1265.75;

pt [0] .y = 150.73;

p1-> pt-> x = 550.55;
p1-> pt-> y = 350.33;

p1-> pt--;


pt [1] .x = 550.55;

pt [1] .y = 350.33;

/ *做点什么使用结构* /
/ *而不是打印结果,我打印出结构* /

printf(" \ nStrukt 1:%f \ n,p1-> ; pt-> x);
printf(" \ nStrukt 1:%f \ n",p1-> pt-> y);

p1-> ; pt ++;

printf(" \ nStrukt 2:%f \ n",p1-> pt-> x);
printf(" \ nStrukt 2 :%f \ n",p1-> pt-> y);

返回0;
}
I would like to know if there is a better way to access struct members
(the points).
Accessing struct members with p1->p2->x seems rather ugly and unsafe.
[. . .]
/* struct with points, used by struct profil */

struct point
{
double x;
double y;
};

struct profil
{
struct point *pt;
struct profil *previous;
struct profil *next;
};

int main(void)
{
int n;

struct profil *p1;
struct profil *begin;
[. . .]
/* put some information into the struct */

p1->pt->x=1265.75;
p1->pt->y=150.73;

p1->pt++; /* ugly, I know. Is there a better way to do this? */
Yes, how about this:

struct point *pt = p1->pt;
pt[0].x = 1265.75;
pt[0].y = 150.73;
p1->pt->x=550.55;
p1->pt->y=350.33;

p1->pt--;
pt[1].x = 550.55;
pt[1].y = 350.33;

/* do something with the structs */
/* instead of printing results, I print the structs out */

printf("\nStrukt 1: %f\n", p1->pt->x);
printf("\nStrukt 1: %f\n", p1->pt->y);

p1->pt++;

printf("\nStrukt 2: %f\n", p1->pt->x);
printf("\nStrukt 2: %f\n", p1->pt->y);

return 0;
}




int i;

for(i = 0; i< n; ++ i)

{

printf(" \ nStrukt%d:%f \ n",i + 1,pt [i] .x);

printf(" \ nStrukt%d:%f \ n",i + 1,pt [i] .y);

}


问候,


Russell Hanneken rg********@pobox.com

删除'g''从我的地址到给我发邮件。



int i;
for (i = 0; i < n; ++i)
{
printf("\nStrukt %d: %f\n", i + 1, pt[i].x);
printf("\nStrukt %d: %f\n", i + 1, pt[i].y);
}

Regards,

Russell Hanneken
rg********@pobox.com
Remove the ''g'' from my address to send me mail.


Russell Hanneken写道:
Russell Hanneken wrote:
Roman Hartmann写道:
Roman Hartmann wrote:

int main( void)


结构profil * p1;
struct profil * begin;

int main(void)
{
int n;

struct profil *p1;
struct profil *begin;


[。 。 。 ]
struct point * pt = p1-> pt;
pt [0] .x = 1265.75;
pt [0] .y = 150.73;

pt [1] .x = 550.55;
pt [1] .y = 350.33;

int i;

[ . . . ]
struct point *pt = p1->pt;
pt[0].x = 1265.75;
pt[0].y = 150.73;

pt[1].x = 550.55;
pt[1].y = 350.33;

int i;




哎呀; i和pt都需要在main的开头声明。除非

你编写的代码符合新的(1999)C标准(你可能不是b $ b),所以局部变量声明必须是

a块的开头,在任何非声明代码之前。


问候,


Russell Hanneken
rg********@pobox.com

删除来自我地址的''g'给我发邮件。



Oops; both i and pt need to be declared at the beginning of main. Unless
you''re writing code to conform to the new (1999) C standard (which you
probably aren''t), local variable declarations have to be at the beginning of
a block, before any non-declaration code.

Regards,

Russell Hanneken
rg********@pobox.com
Remove the ''g'' from my address to send me mail.




" Russell Hanneken" < RG ******** @ pobox.com> schrieb im Newsbeitrag

新闻:a4 ***************** @ newsread4.news.pas.earthl ink.net ...

"Russell Hanneken" <rg********@pobox.com> schrieb im Newsbeitrag
news:a4*****************@newsread4.news.pas.earthl ink.net...
Roman Hartmann写道:
Roman Hartmann wrote:
我想知道是否有更好的方法来访问struct成员
(要点)。
使用p1->访问struct成员; p2-> x看起来相当丑陋且不安全。
I would like to know if there is a better way to access struct members
(the points).
Accessing struct members with p1->p2->x seems rather ugly and unsafe.


[。 。 。]


[. . .]


/ * struct with points,struct profil * /

struct point
{
double x;
double y;
};

struct profil
{struct struct * pt;
struct profil * previous;
struct profil *下一个;
};

int main(无效)
{
int n;

struct profil * p1;
struct profil * begin;

/* struct with points, used by struct profil */

struct point
{
double x;
double y;
};

struct profil
{
struct point *pt;
struct profil *previous;
struct profil *next;
};

int main(void)
{
int n;

struct profil *p1;
struct profil *begin;


[。 。 。]


[. . .]


/ *将一些信息放入结构中* /

p1-> pt-> x = 1265.75;
p1- > pt-> y = 150.73;

p1-> pt ++; / *丑陋,我知道。有一个更好的方法吗? * /

/* put some information into the struct */

p1->pt->x=1265.75;
p1->pt->y=150.73;

p1->pt++; /* ugly, I know. Is there a better way to do this? */



是的,这个怎么样:

struct point * pt = p1-> pt;
pt [0] .x = 1265.75;
pt [0] .y = 150.73;



Yes, how about this:

struct point *pt = p1->pt;
pt[0].x = 1265.75;
pt[0].y = 150.73;




是的!

处理起来要容易得多现在这样点。



Yes!
It''s much easier to deal with the points now that way.

p1-> pt-> x = 550.55;
p1-> pt-> y = 350.33;

p1-> pt - ;
p1->pt->x=550.55;
p1->pt->y=350.33;

p1->pt--;



pt [1] .x = 550.55;
pt [1]。 y = 350.33;



pt[1].x = 550.55;
pt[1].y = 350.33;






/ *对结构做一些事情* /
/ *而不是打印结果,我打印结构* /

printf(\ nStrukt 1:%f \ n,p1-> pt-> x);
printf(" \ nStrukt 1:%f \ n",p1-> pt-> y);

p1-> pt ++;
printf(" \ nStrukt 2:%f \ n",p1-> pt-> x);
printf(" \ n Strukt 2:%f \ n",p1-> pt-> y);

返回0;
}

/* do something with the structs */
/* instead of printing results, I print the structs out */

printf("\nStrukt 1: %f\n", p1->pt->x);
printf("\nStrukt 1: %f\n", p1->pt->y);

p1->pt++;

printf("\nStrukt 2: %f\n", p1->pt->x);
printf("\nStrukt 2: %f\n", p1->pt->y);

return 0;
}



int i;
for(i = 0;我< N; ++ i)
{/> printf(" \ nStrukt%d:%f \ n",i + 1,pt [i] .x);
printf(" \ nStrukt%d:%f \ n",i + 1,pt [i] .y);
}

问候,
Russell Hanneken



int i;
for (i = 0; i < n; ++i)
{
printf("\nStrukt %d: %f\n", i + 1, pt[i].x);
printf("\nStrukt %d: %f\n", i + 1, pt[i].y);
}

Regards,
Russell Hanneken




非常感谢!

Roman



thanks a lot!
Roman


这篇关于结构和动态内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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