char *和char [] [英] char* and char[]

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

问题描述

大家好,


我是C的新手,所以请耐心等我:)


我需要创建一个数组在函数(局部变量)中,大小为

为1MB。由于局部变量存储在堆栈中,在运行时,我得到一个错误。


我尝试使用mallac()函数。这个函数只有在我为$ * b $ b为char *而不是char []分配内存时才有效。


我希望操作函数中的字符串,但是在使用char *时我听说过

,这个值并不意味着要改变。好像我使用char [],我可以根据需要多次更改值(我希望b $ b想要)。

有人可以告诉我如何正确地将内存分配给

本地变量(字符串),这样我就可以将字符串复制到这个变量,并且

以任何方式操纵它以及我希望多少次。


提前致谢,

mike79

Hello all,

I am a newbie to C, so please bear with me :)

I need to create an array in a function (a local variable) with size
of 1MB. Since local variables get stored on the stack, at run-time, I
get an error.

I tried using mallac() function. This function only works when I am
allocating memory to a char* and not a char[].

I wish to manipulate the string in my function, but I''ve heard that
when using char*, the value is not meant to be altered. Where as if I
use char[], I can change the value as many times as I like (which I
want to).

Could someone please tell me how to properly allocate memory to a
local variable (string), so I can copy a string to this variable, and
manipulate it any way and how ever many times I wish.

Thanks in advance,
mike79

推荐答案

Artie Gold写道:
Artie Gold wrote:
也许这会有所帮助:

void some_function(){
/ *以下是指向一个字符串文字* /
char * cannot_be_altered ="这个文字不能改变;

/ *以下是一个'char'的数组,不能改变
^^^^^^

ITYM可以(但是固定大小)* /
char can_be_altered [] =此文本可以更改;

/ *下面为39个字符的stri分配了足够的空间ng
和地址(如果成功)在char * /
char * can_also_be_altered = malloc(40)的指针中的地方;
if(can_also_be_altered!= NULL)
strcpy( can_also_be_altered,此文本也可以更改;

/ *以下调用未定义的行为* /
strcpy(cannot_be_altered,fooled ya!);

/ *以下是完全正常* /
can_be_altered [0] =''T'';

/ *以下调用未定义的行为* /
strcpy (can_be_altered,某些字符串的任意长度超过
之前的内容);

/ *以下内容完全正常* /
can_also_be_altered [0 ] ='''T'';

/ * ......依旧...... * /
}
Perhaps this will help:

void some_function() {
/* the following is a pointer to a string literal */
char * cannot_be_altered = "this text cannot be altered";

/* the following is an array of `char''s that cannot be altered ^^^^^^
ITYM can (but is of fixed size) */
char can_be_altered[] = "this text can be altered";

/* the following allocated enough space for a 39 character string
and places that address (if successful) in a pointer to char */
char * can_also_be_altered = malloc(40);
if (can_also_be_altered != NULL)
strcpy(can_also_be_altered, "this text can also be altered";

/* the following invokes undefined behavior */
strcpy(cannot_be_altered, "fooled ya!");

/* the following is perfectly OK */
can_be_altered[0] = ''T'';

/* the following invokes undefined behavior */
strcpy(can_be_altered, "some string of arbitrary length exceeding "
"what was there before");

/* the following is perfectly OK */
can_also_be_altered[0] = ''T'';

/* ... and so on ... */
}




-

Bertrand Mollinier Toublet

现实存在 - Richard Heathfield,2003年7月1日



--
Bertrand Mollinier Toublet
"Reality exists" - Richard Heathfield, 1 July 2003


Bertrand Mollinier Toublet写道:
Bertrand Mollinier Toublet wrote:
Artie Gold写道:
Artie Gold wrote:
也许这会有所帮助:

void some_function(){
/ *以下是指向字符串文字的指针* /
char * cannot_be_altered ="这个文字不能改变;

/ *以下是一个'char'的数组,不能改变
^^^^^^
ITYM可以
Perhaps this will help:

void some_function() {
/* the following is a pointer to a string literal */
char * cannot_be_altered = "this text cannot be altered";

/* the following is an array of `char''s that cannot be altered
^^^^^^
ITYM can




Doh!

当然!


--ag


[当然这可以导致对代码中的评论进行长时间的讨论。

希望它不会;-)]



Doh!
Of course!

--ag

[Of course this _could_ lead to a long discussion on comments in code.
Hopefully it won''t ;-)]

(但是具有固定的大小)* /
char can_be_altered [] ="此文本可以更改;

/ *以下分配足够的空间容纳39个字符的字符串
和地址(如果成功)放在指向char * /
char * can_also_be_altered = malloc(40)的指针中;
if(can_also_be_altered!= NULL)
strcpy(can_also_be_altered,此文本也可以更改;

/ *以下调用未定义的行为* /
strcpy(cannot_be_altered," fooled ya!" );

/ *以下是完全正常* /
can_be_altered [0] =''T'';

/ *以下调用未定义的行为* /
strcpy(can_be_altered,一些任意长度的字符串超过
之前的内容);

/ *以下内容完全正常* /
can_also_be_altered [0] =''T'';

/ * ......依旧...... * /
}
(but is of fixed size) */
char can_be_altered[] = "this text can be altered";

/* the following allocated enough space for a 39 character string
and places that address (if successful) in a pointer to char */
char * can_also_be_altered = malloc(40);
if (can_also_be_altered != NULL)
strcpy(can_also_be_altered, "this text can also be altered";

/* the following invokes undefined behavior */
strcpy(cannot_be_altered, "fooled ya!");

/* the following is perfectly OK */
can_be_altered[0] = ''T'';

/* the following invokes undefined behavior */
strcpy(can_be_altered, "some string of arbitrary length exceeding "
"what was there before");

/* the following is perfectly OK */
can_also_be_altered[0] = ''T'';

/* ... and so on ... */
}




-

Artie Gold - 德克萨斯州奥斯汀



--
Artie Gold -- Austin, Texas


A rtie Gold< ar ******* @ austin.rr.com>在消息新闻中写道:< 3F ************** @ austin.rr.com> ...
Artie Gold <ar*******@austin.rr.com> wrote in message news:<3F**************@austin.rr.com>...
mike79写道:
大家好,

我是C的新手,所以请耐心等待我:)

我需要在函数(局部变量)中创建一个大小的数组
1MB。由于局部变量存储在堆栈中,在运行时,我收到错误。

我尝试使用mallac()函数。这个函数只有在我为char *而不是char []分配内存时才有效。

我希望在我的函数中操作字符串,但我听说过
使用char *时,该值不应更改。好像我使用char [],我可以随意更改值(我想要的)。
Hello all,

I am a newbie to C, so please bear with me :)

I need to create an array in a function (a local variable) with size
of 1MB. Since local variables get stored on the stack, at run-time, I
get an error.

I tried using mallac() function. This function only works when I am
allocating memory to a char* and not a char[].

I wish to manipulate the string in my function, but I''ve heard that
when using char*, the value is not meant to be altered. Where as if I
use char[], I can change the value as many times as I like (which I
want to).



啊,我看看你困惑的地方!

也许这会有所帮助:

void some_function(){
/ *以下是指向字符串文字的指针* /
char * cannot_be_altered ="此文本无法更改" ;;

/ *以下是一个无法更改的`char'数组
(但是固定大小)* /
char can_be_altered [] ="此文本可以更改;

/ *下面为39个字符的字符串分配了足够的空间
和地方指向char * /
char * can_also_be_altered = malloc(40)的指针(如果成功);
if(can_also_be_altered!= NULL)
strcpy(can_also_be_altered," this text can也可以改变;

/ *以下调用未定义的行为* /
strcpy(cannot_be _altered,fooled ya!);

/ *以下内容非常好* /
can_be_altered [0] =''T'';
/ *以下调用未定义的行为* /
strcpy(can_be_altered,某些字符串的任意长度超过
之前的内容);

/ *以下完全没问题* /
can_also_be_altered [0] =''T'';



Ah, I see where you''re confused!

Perhaps this will help:

void some_function() {
/* the following is a pointer to a string literal */
char * cannot_be_altered = "this text cannot be altered";

/* the following is an array of `char''s that cannot be altered
(but is of fixed size) */
char can_be_altered[] = "this text can be altered";

/* the following allocated enough space for a 39 character string
and places that address (if successful) in a pointer to char */
char * can_also_be_altered = malloc(40);
if (can_also_be_altered != NULL)
strcpy(can_also_be_altered, "this text can also be altered";

/* the following invokes undefined behavior */
strcpy(cannot_be_altered, "fooled ya!");

/* the following is perfectly OK */
can_be_altered[0] = ''T'';

/* the following invokes undefined behavior */
strcpy(can_be_altered, "some string of arbitrary length exceeding "
"what was there before");

/* the following is perfectly OK */
can_also_be_altered[0] = ''T'';




怎么样

int main(){

char * p =" hello";

char c [5] =" hello";

c [1 ] =''y''; / *不确定这个!* /

p = c; / *也不确定这个!* /

返回0 ;

}

不是var''c''内存是只读的!

另外p = c;不应该是一个问题吗?


- 拉维


}



how about
int main (){
char *p = "hello";
char c[5] = "hello";
c[1] = ''y'';/* Not sure on this one !*/
p = c;/* Not sure on this one too !*/
return 0;
}
Isnt it that var ''c'' memory is read only !
Also p=c; shouldnt be a problem right ?

- Ravi

}


这篇关于char *和char []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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