Querry aoubt struct包括字符串指针 [英] Querry aoubt struct including character strings pointer

查看:92
本文介绍了Querry aoubt struct包括字符串指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。

如果一个struct包含一个字符串,有两种方法可以定义结构,一个是字符数组,另一个是字符指针。

例如,

//结构包括字符串的程序,test1.c


#include< stdio.h>

#define LEN 20

struct info {

char first [LEN];

char最后[LEN];

int age;

};


struct pinfo {

char *首先;

char * last;

int age;

};


int main (无效)

{

struct info one = {" Opw"," Cde",22};

struct pinfo two = {" Tydu"," Gqa",33};

printf("%s%s is%d years old.\
",one.first,one.last,one .age);

printf(%s%s是%d岁。\ n,two.first,two.last,two.age);

返回0;

}


//结束测试t1.c


它可以工作。我知道一点,但不是很详细,第二个结构pinfo不好

在上面的代码中。例如,如果struct pinfo 2在main函数中定义为数组

two [1],则无法工作。我只想详细了解

的原因。

避免这个问题的更好方法是malloc()和另一个指针

指向结构,并复制字符串,然后释放它。是不是?


谢谢。


bowderyu

Hello, all.
If a struct contains a character strings, there are two methods to
define the struct, one by character array, another by character pointer.
E.g,
//Program for struct includeing character strings, test1.c

#include <stdio.h>
#define LEN 20

struct info {
char first[LEN];
char last[LEN];
int age;
};

struct pinfo {
char * first;
char * last;
int age;
};

int main(void)
{
struct info one = {"Opw", "Cde", 22};
struct pinfo two = {"Tydu", "Gqa", 33};
printf("%s %s is %d years old.\n", one.first, one.last, one.age);
printf("%s %s is %d years old.\n", two.first, two.last, two.age);
return 0;
}

//end test1.c

It can work.I know a little,but not very detail that the second struct pinfo is not good
in the above code. E.g, if the struct pinfo two is defined as an array
two[1] in the main function, it can not work. I just want to know the
reason in detail.

The better way to avoid this problem is malloc() and another pointer
point to the struct, and copy the strings, then free it. Is it right?

Thank you.

bowderyu

推荐答案

bowlderyu写道:
bowlderyu wrote:

你好,全部。

如果一个struct包含一个字符串,有两种方法

定义结构,逐个字符数组,另一个按字符指针定义。

例如,

//结构包括字符串的程序, test1.c

#include< stdio.h>

#define LEN 20

struct info {

char [LEN];

char last [LEN];

int age;

};


struct pinfo {

char * first;

char * last;

int age; < br $>
};


int main(无效)

{

struct info one = {" Opw,Cde,22};

struct pinfo two = {" Tydu"," Gqa&q uot;,33};

printf("%s%s is%d years old.\
",one.first,one.last,one.age);

printf("%s%s是%d years old.\,two.first,two.last,two.age);

返回0;

}


//结束test1.c


它可以工作。我知道一点,但不是很详细在上面的代码中,第二个结构pinfo不好

。例如,如果struct pinfo 2在main函数中定义为数组

two [1],则无法工作。我只想详细了解

的原因。
Hello, all.
If a struct contains a character strings, there are two methods to
define the struct, one by character array, another by character pointer.
E.g,
//Program for struct includeing character strings, test1.c

#include <stdio.h>
#define LEN 20

struct info {
char first[LEN];
char last[LEN];
int age;
};

struct pinfo {
char * first;
char * last;
int age;
};

int main(void)
{
struct info one = {"Opw", "Cde", 22};
struct pinfo two = {"Tydu", "Gqa", 33};
printf("%s %s is %d years old.\n", one.first, one.last, one.age);
printf("%s %s is %d years old.\n", two.first, two.last, two.age);
return 0;
}

//end test1.c

It can work.I know a little,but not very detail that the second struct pinfo is not good
in the above code. E.g, if the struct pinfo two is defined as an array
two[1] in the main function, it can not work. I just want to know the
reason in detail.



它会正常工作。


你问题的答案取决于你对不好的定义。

如果你想要一个已知最大长度为

的可变字符串,那么第一种形式就可以了。第二个是好的,如果你想要常量字符串并且将是

如果更改为更安全


struct pinfo {

const char *第一个;

const char * last;

int age;

};

It will work fine.

The answer to your question depends on your definition of "not good".
The first form is OK if you want mutable strings with a known maximum
length. The second is OK if you want constant strings and would be
safer if changed to

struct pinfo {
const char * first;
const char * last;
int age;
};


避免这个问题的更好方法是malloc()和另一个指针

指向结构,并复制字符串,然后释放它。是不是?
The better way to avoid this problem is malloc() and another pointer
point to the struct, and copy the strings, then free it. Is it right?



你想避免哪个问题?


-

Ian Collins

Which problem are you trying to avoid?

--
Ian Collins


Ian Collins< ia ****** @ hotmail.comwrites:

const char *,good sugesstion。


我不知道为什么它不适用于两个[1]的情况,所以我想

避免使用char数组。但现在,它看起来很好。


请你给它更多解释一下吗?

我的意思是为什么对于非常数字符串不安全。


我在考虑,似乎很清楚。


现在,我必须离开。


无论如何,谢谢。
Ian Collins <ia******@hotmail.comwrites:
const char *, good sugesstion.

I don''t know why it can not work for the case of two[1], so I want to
avoid using char array. But now, it seems fine.

Would you please give more explanation about it?
I mean for why not safe for non-constant strings.

I am thinking about, seems clearly.

Now, I have to leave.

Anyway, thank you .

bowlderyu写道:
bowlderyu wrote:

>你好,全部。
如果一个struct包含一个字符串,有两种方法可以定义结构,一种是字符数组,另一种是字符指针。
例如,
//包含字符串的结构程序,test1 .c

#include< stdio.h>
#define LEN 20

结构信息{
char first [LEN];
char last [LEN];
int age;
};

struct pinfo {
char * first;
char * last;
int age;
};

int main(void)
{
struct info one = {" Opw", " Cde",22};
struct pinfo two = {" Tydu"," Gqa",33};
printf("%s%s is%d years old.\ n",one.first,one.last,one.age);
printf("%s%s is%d years old.\
,two.first,two.last,two.age );
返回0;
}
//结束test1.c

它可以工作。我知道一点,但不是很细节在上面的代码中,第二个结构pinfo不好。例如,如果struct pinfo 2在主函数中被定义为数组
两个[1],则它无法工作。我只是想详细了解
的原因。
>Hello, all.
If a struct contains a character strings, there are two methods to
define the struct, one by character array, another by character pointer.
E.g,
//Program for struct includeing character strings, test1.c

#include <stdio.h>
#define LEN 20

struct info {
char first[LEN];
char last[LEN];
int age;
};

struct pinfo {
char * first;
char * last;
int age;
};

int main(void)
{
struct info one = {"Opw", "Cde", 22};
struct pinfo two = {"Tydu", "Gqa", 33};
printf("%s %s is %d years old.\n", one.first, one.last, one.age);
printf("%s %s is %d years old.\n", two.first, two.last, two.age);
return 0;
}

//end test1.c

It can work.I know a little,but not very detail that the second struct pinfo is not good
in the above code. E.g, if the struct pinfo two is defined as an array
two[1] in the main function, it can not work. I just want to know the
reason in detail.



它会正常工作。


答案你的问题取决于你对不好的定义。

如果你想要一个已知最大长度为

的可变字符串,那么第一种形式就可以了。第二个是好的,如果你想要常量字符串并且将是

如果更改为更安全


struct pinfo {

const char *第一个;

const char * last;

int age;

};

It will work fine.

The answer to your question depends on your definition of "not good".
The first form is OK if you want mutable strings with a known maximum
length. The second is OK if you want constant strings and would be
safer if changed to

struct pinfo {
const char * first;
const char * last;
int age;
};


>避免这个问题的更好方法是malloc()和另一个指针
指向结构,并复制字符串,然后释放它。是不是?
>The better way to avoid this problem is malloc() and another pointer
point to the struct, and copy the strings, then free it. Is it right?



你想避免哪个问题?

Which problem are you trying to avoid?


bowlderyu写道:
bowlderyu wrote:

你好,全部。

如果一个struct包含一个字符串,那么有两种方法可以定义结构,一个是字符数组,另一个是字符指针。

例如,

//结构包含字符串的程序,test1.c

#include< stdio.h>

#define LEN 20

结构信息{

char first [LEN];

char last [LEN];

int age ;

};


struct pinfo {

char * first;

char * last;

int age;

};


int main(无效)

{

struct info one = {" Opw"," Cde",22};
Hello, all.
If a struct contains a character strings, there are two methods to
define the struct, one by character array, another by character pointer.
E.g,
//Program for struct includeing character strings, test1.c

#include <stdio.h>
#define LEN 20

struct info {
char first[LEN];
char last[LEN];
int age;
};

struct pinfo {
char * first;
char * last;
int age;
};

int main(void)
{
struct info one = {"Opw", "Cde", 22};



在这种情况下,您创建的数组的内容可以安全地修改为
,这些数组存储为一个的一部分。

In this case, you are creating arrays whose contents can be safely
modified, which are stored as part of ''one''.


struct pinfo two = {" Tydu"," Gqa",33};
struct pinfo two = {"Tydu", "Gqa", 33};



在这种情况下,您将创建两个未命名的数组,其内容不能安全地修改
。你在''two''中存储指向这些数组的指针。

In this case, you are creating two unnamed arrays, whose contents cannot
be safely modified. You are storing pointers to those arrays in ''two''.


printf("%s%s is%d years old.\ n,one .first,one.last,one.age);

printf("%s%s is%d years old.\ n,two.first,two.last,two.age );

返回0;

}


//结束test1.c


它可以工作。我知道一点,但不是很详细,第二个结构pinfo不好
printf("%s %s is %d years old.\n", one.first, one.last, one.age);
printf("%s %s is %d years old.\n", two.first, two.last, two.age);
return 0;
}

//end test1.c

It can work.I know a little,but not very detail that the second struct pinfo is not good



没有理由为什么它不应该'' t。

There''s no reason why it shouldn''t be.

上面代码中的
。例如,如果struct pinfo 2在main函数中定义为数组

two [1],则无法工作。我只是想详细了解

的原因。
in the above code. E.g, if the struct pinfo two is defined as an array
two[1] in the main function, it can not work. I just want to know the
reason in detail.



如果您更改声明两个的方式:


struct pinfo two [1] = {" Tydu" ;,Gqa,33};


然后你还必须改变你使用两个的方式:


printf( %s%s是%d岁。\ n,

两个[0]。第一个,两个[0] .last,两个[0] .age);


假设您进行了匹配的更改,如上所述,没有理由为什么它不会起作用。长度为1的数组通常是没有意义的,但是它们是完全合法的。

If you change how you declare "two":

struct pinfo two[1] = {"Tydu", "Gqa", 33};

Then you also have to change how you use "two":

printf("%s %s is %d years old.\n",
two[0].first, two[0].last, two[0].age);

Assuming you made matching changes, as above, there''s no reason why it
shouldn''t work. Arrays of length 1 are generally pointless, but they''re
perfectly legal.


避免这个问题的更好方法是malloc( )和另一个指针

指向结构,并复制字符串,然后释放它。这样对吗?
The better way to avoid this problem is malloc() and another pointer
point to the struct, and copy the strings, then free it. Is it right?



更好的意义何在?它比上面显示的任何一种方法都复杂得多。

。如果字符串需要是
可写,但我有一个固定的最大长度,我会使用第一种方法。如果字符串不需要可写,我会使用第二个

方法,尽管我会首先更改

,最后是const char * "而不是char *。


我唯一一次使用malloc()方法的是,如果字符串的长度为

在编译时已知,并且具有较大的上限。

Better in what sense? It''s a lot more complicated than either method
you''ve shown above. I''d use the first method if the strings need to be
writable, but have a small fixed maximum length. I''d use the second
method if the strings don''t need to be writable, though I would change
first and last to be "const char*" rather than "char*".

The only time I would ever use the malloc() approach is if the length of
the strings was not known at compile time, and has a large upper limit.


这篇关于Querry aoubt struct包括字符串指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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