malloc结构? [英] malloc a struct?

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

问题描述

大家好..


如果我想要malloc一个结构,请说如下:


struct myStruct1

{

int number;

char * string;

}


怎么样我这样做了吗?


我需要首先使用malloc myStruct,然后是malloc字符串吗?


我见过一些malloc命令,所以:


myStruct1 = malloc(sizeof(myStruct1));

myStruct1 = malloc(sizeof(* myStruct1));

myStruct1 = malloc(sizeof(myStruct1 *));


有谁能告诉我差异吗?


谢谢大家的帮助!非常感谢!

mike79

解决方案

mi **** @ iprimus.com.au (mike79)写道:

如果我想要malloc一个结构,请说明以下内容:

struct myStruct1
{
int number;
char * string;
}

我该怎么做?


struct myStruct1 * s = malloc(sizeof * s);


之后,你可以分配给s->分配的字符串内存对于

字符串,请说像

s-> string = malloc(strlen(myString)+ 1);

我会首先需要malloc myStruct,然后是malloc字符串?


这是最简单的方法。你绝对不能将
分配给一个尚未分配
的结构对象的字符串成员,尽管你可以为
string。

我见过一些malloc命令,所以是:


C没有命令。 malloc()是标准

C库中的函数。

myStruct1 = malloc(sizeof(myStruct1));


根据你上面的

定义,你必须是sizeof(struct myStruct1)。


另外,将结构和指针命名为结构'myStruct1'的对象是令人困惑的。


调用时malloc(),我建议在你分配的对象上使用sizeof运算符,而不是在类型上。例如,

*不要写这个:


int * x = malloc(sizeof(int)* 128); / *不要这样做! * /


相反,请这样写:


int * x = malloc(sizeof * x * 128);


这样做有几个理由:


*如果你改变了'x'指向的类型,它'''更改malloc()调用也不需要



这在大型程序中更是一个问题,但它仍然是

方便小一点。


*考虑一个对象的大小使得编写语句

不易出错。您可以验证sizeof语法是否正确无需查看声明。

myStruct1 = malloc(sizeof(* myStruct1));


这样更好,虽然我再给结构和

对象不同的名字。

myStruct1 = malloc(sizeof(myStruct1 *));




错误。

-

int main(void ){char p [] =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz。\

\ n",* q =" kl BIcNBFr.NKEzjwCIxNJC" ;; int i = sizeof p / 2; char * strchr( ); int putchar(\

); while(* q){i + = strchr(p,* q ++) - p; if(i> =(int)sizeof p)i- = sizeof p-1; putchar(p [i] \

);}返回0;}




更好地定义这样的结构:


typedef struct {

int number;

char * string;

} myStruct1;


到malloc你可以这样做,创建你的指针。


myStruct1 * name =(myStruct *)malloc (sizeof(myStruct1));


你应该检查malloc是否也返回null也是为了错误

处理


引用结构指针中的元素只需使用这种语法。

name-> number = x;

和malloc结构中的指针:


name-> string = malloc(数字);


数字是你想要的字符


- >当你处理指向结构的指针时使用它,否则它通常是''structname.structmember''语法。


" mike79" < MI **** @ iprimus.com.au>在消息中写道

新闻:49 ************************* @ posting.google.co m ... < blockquote class =post_quotes>大家好..

如果我想要malloc一个结构,请说如下:

struct myStruct1
{
int number;
char * string;
}

我该怎么做?

我需要首先使用malloc myStruct,然后是malloc字符串吗?

我看过一些malloc命令,所以是:

myStruct1 = malloc(sizeof(myStruct1));
myStruct1 = malloc(sizeof(* myStruct1)) ;
myStruct1 = malloc(sizeof(myStruct1 *));

有谁能告诉我差异吗?

谢谢大家的帮助!非常感谢!
mike79



[top-posting更正]


Jason写道:


" mike79" < MI **** @ iprimus.com.au>在消息中写道
新闻:49 ************************* @ posting.google.co m ...
< blockquote class =post_quotes>大家好..

如果我想要malloc一个结构,请说如下:

struct myStruct1
{
int number;
char * string;
}

我该怎么做?

我需要首先使用malloc myStruct,然后是malloc字符串吗?

我看过一些malloc命令,所以是:

myStruct1 = malloc(sizeof(myStruct1));
myStruct1 = malloc(sizeof(* myStruct1)) ;
myStruct1 = malloc(sizeof(myStruct1 *));

有谁能告诉我差异吗?

谢谢大家的帮助!非常感谢!


更好地定义这样的结构:

typedef struct {
int number;
char * string;
} myStruct1 ;对于malloc,你可以这样做,创建你的指针。

myStruct1 * name =(myStruct *)malloc(sizeof(myStruct1));


演员是不必要的,可以隐藏失败的错误

#include< stdlib.h>


风格上,这会更好:


myStruct1 * name = malloc(sizeof * name);


as,如果类型应该碰巧改变了,你只需要在* one *的地方更改它



你应该检查一下malloc是否也为
错误返回null处理

引用struct指针中的元素只需使用这个
语法。 name-> number = x;
和malloc结构中的指针:

name-> string = malloc(number);

数字是字符你想要


当然,你应该真的这样:


name-> string = malloc(number + 1);


来说明C风格字符串中的尾随空字符。

- >在你处理结构
的指针时使用,否则它是通常的''structname.structmember''语法。




HTH,
- g

-

Artie Gold - 德克萨斯州奥斯汀

哦,对于过去的美好时光老垃圾邮件。


hi all..

if I wanted to malloc a struct, say the following:

struct myStruct1
{
int number;
char *string;
}

how would I do this?

Would I need to first malloc myStruct, then malloc string?

Ive seen some malloc commands around, so are:

myStruct1 = malloc(sizeof (myStruct1));
myStruct1 = malloc(sizeof (* myStruct1));
myStruct1 = malloc(sizeof (myStruct1 *));

Can anyone tell me the difference please??

Thank you all for your help! much appreciated!
mike79

解决方案

mi****@iprimus.com.au (mike79) writes:

if I wanted to malloc a struct, say the following:

struct myStruct1
{
int number;
char *string;
}

how would I do this?
struct myStruct1 *s = malloc (sizeof *s);

After that, you can assign to s->string memory allocated for the
string, say with something like
s->string = malloc (strlen (myString) + 1);
Would I need to first malloc myStruct, then malloc string?
That''s the most straightforward approach. You definitely can''t
assign to the string member of a structure object that hasn''t yet
been allocated, although you could allocate the memory for the
string.
Ive seen some malloc commands around, so are:
C doesn''t have commands. malloc() is a function in the standard
C library.
myStruct1 = malloc(sizeof (myStruct1));
That''d have to be sizeof (struct myStruct1) based on your
definition above.

Also, it is confusing to name both the structure and a pointer to
an object of the structure''s type `myStruct1''.

When calling malloc(), I recommend using the sizeof operator on
the object you are allocating, not on the type. For instance,
*don''t* write this:

int *x = malloc (sizeof (int) * 128); /* Don''t do this! */

Instead, write it this way:

int *x = malloc (sizeof *x * 128);

There''s a few reasons to do it this way:

* If you ever change the type that `x'' points to, it''s not
necessary to change the malloc() call as well.

This is more of a problem in a large program, but it''s still
convenient in a small one.

* Taking the size of an object makes writing the statement
less error-prone. You can verify that the sizeof syntax is
correct without having to look at the declaration.
myStruct1 = malloc(sizeof (* myStruct1));
That''s better, although again I''d give the structure and the
object different names.
myStruct1 = malloc(sizeof (myStruct1 *));



Wrong.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}



better to define struct like this:

typedef struct {
int number;
char *string;
}myStruct1;

to malloc you can do this, creating your pointer.

myStruct1 *name = (myStruct *) malloc(sizeof(myStruct1));

you are supposed to check to see if malloc returns null also for error
handling

to reference elements within your struct pointer just use this syntax.
name->number = x;
and to malloc the pointer within the struct:

name->string = malloc(number);

number being characters u want

-> is used when u are dealing with a pointer to a structure otherwise its
the usual ''structname.structmember'' syntax.

"mike79" <mi****@iprimus.com.au> wrote in message
news:49*************************@posting.google.co m...

hi all..

if I wanted to malloc a struct, say the following:

struct myStruct1
{
int number;
char *string;
}

how would I do this?

Would I need to first malloc myStruct, then malloc string?

Ive seen some malloc commands around, so are:

myStruct1 = malloc(sizeof (myStruct1));
myStruct1 = malloc(sizeof (* myStruct1));
myStruct1 = malloc(sizeof (myStruct1 *));

Can anyone tell me the difference please??

Thank you all for your help! much appreciated!
mike79



[top-posting corrected]

Jason wrote:


"mike79" <mi****@iprimus.com.au> wrote in message
news:49*************************@posting.google.co m...

hi all..

if I wanted to malloc a struct, say the following:

struct myStruct1
{
int number;
char *string;
}

how would I do this?

Would I need to first malloc myStruct, then malloc string?

Ive seen some malloc commands around, so are:

myStruct1 = malloc(sizeof (myStruct1));
myStruct1 = malloc(sizeof (* myStruct1));
myStruct1 = malloc(sizeof (myStruct1 *));

Can anyone tell me the difference please??

Thank you all for your help! much appreciated!

better to define struct like this:

typedef struct {
int number;
char *string;
}myStruct1;

to malloc you can do this, creating your pointer.

myStruct1 *name = (myStruct *) malloc(sizeof(myStruct1));
The cast is unnecessary and can hide the error of failing to
#include <stdlib.h>

Stylistically, this would be better:

myStruct1 *name = malloc(sizeof *name);

as, if the type should happen to change, you only need to change it
in *one* place.

you are supposed to check to see if malloc returns null also for error handling

to reference elements within your struct pointer just use this syntax. name->number = x;
and to malloc the pointer within the struct:

name->string = malloc(number);

number being characters u want
Of course, you should really make that:

name->string = malloc(number + 1);

to account for the trailing null character in C-style strings.

-> is used when u are dealing with a pointer to a structure otherwise its the usual ''structname.structmember'' syntax.



HTH,
--ag
--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.


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

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