将值分配给char数组 [英] Assigning values to char arrays

查看:67
本文介绍了将值分配给char数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


这里是一个基本问题。假设我已经声明了两个变量,


char * a,** b;


然后我可以给一个像

a =" hello world" ;;


问题是,我应该如何为b分配值?一个简单的


b [0] =" string";


导致分段错误。


非常感谢。


问候,Emyl。

解决方案

emyl< ; kw **** @ yahoo.com写道:


大家好,


这里是一个基本问题。假设我已经声明了两个变量,


char * a,** b;



b是一个指向char指针的指针。


>

然后我可以给一个像一个像


a =" hello world" ;;


的问题是,我应该如何赋值给b'一个简单的


b [0] =" string" ;;



* b =" s";


>

导致分段错误。


答案非常感谢。


问候,Emyl。


emyl说:


大家好,


这里是一个基本问题。假设我已经声明了两个变量,


char * a,** b;


然后我可以给一个像

a =" hello world" ;;


问题是,我应该如何为b分配值?一个简单的


b [0] =" string";


导致分段错误。


非常感谢。



您的定义:


char * a,** b;


为名为a的指向char的指针保留足够的存储空间,并将名为b的指针指向char的指针保留足够的存储空间。


a =" ; hello world" ;;


为这个指向char的指针赋值,有问题的值是给定第一个字符的

地址字符串文字。


但是b [0] =" string" ;;是一个问题,不是因为

语法有什么问题,而是因为你做了一个不正确的假设。


b是一个指针-to-pointer-to-char,但你没有指向任何指向char的指针,所以它目前是不确定的。 B [0] ="串英寸;

*不是*试图给b一个值。这是试图给出价值

b [0]。但是b [0]没有意义,除非b有一个有意义的值。


你可以用几种方式给出有意义的价值,但最明显的是b $ b b b是为它分配一些新的内存:


#include< stdlib.h>


/ *为某些指针分配内存-char * /

char ** cpalloc(size_t n)

{

char ** ptr = malloc(n * sizeof * ptr) ;

if(ptr!= NULL)

{

while(n--)

{

ptr [n] = NULL;

}

}

返回ptr;

}


#include< stdio.h>


int main(无效)

{

char * a,** b;

a =&它的口袋里有什么东西?;

b = cpalloc(2);

if(b!= NULL)

{

b [0] =" string";

b [1 ] =没有;


printf("%s \ n",a);

printf("%s或%s \ n,b [0], b [1]);


免费(b);

}

返回0;

}


小心。上面写的cpalloc函数不为字符串分配存储空间
,仅用于指向char的指针集合。指向char

的指针足以指向一个字符串,但不能用于存储它。


-

Richard Heathfield < http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日


emyl写道:


>

here这是一个基本问题。假设我已经声明了两个变量,

char * a,** b;

然后我可以给出类似值的值

a =" ; hello world" ;;

问题是,我应该如何为b分配值?一个简单的

b [0] =" string";

导致分段错误。



" char ** b;"声明指向char的指针。您可以使用b =& a;来初始化它。 (如果声明是

存在)。那么** b是[0]。


但请注意,你初始化< a =" hello world" ;;>

会留下一个指向不可修改的字符串。


-

Chuck F(cinefalconer at maineline dot net)

可用于咨询/临时嵌入式和系统。

< http://cbfalconer.home.att.net>


-

发表于来自 http://www.teranews.com 的免费Usenet帐户


Hi all,

here''s an elementary question. Assume I have declared two variables,

char *a, **b;

I can then give a value to a like

a="hello world";

The question is, how should I assign values to b? A simple

b[0]="string";

results in a segmentation fault.

Answers greatly appreciated.

Regards, Emyl.

解决方案

emyl <kw****@yahoo.comwrites:

Hi all,

here''s an elementary question. Assume I have declared two variables,

char *a, **b;

b is a pointer to a char pointer.

>
I can then give a value to a like

a="hello world";

The question is, how should I assign values to b? A simple

b[0]="string";

*b = "s";

>
results in a segmentation fault.

Answers greatly appreciated.

Regards, Emyl.


emyl said:

Hi all,

here''s an elementary question. Assume I have declared two variables,

char *a, **b;

I can then give a value to a like

a="hello world";

The question is, how should I assign values to b? A simple

b[0]="string";

results in a segmentation fault.

Answers greatly appreciated.

Your definition:

char *a, **b;

reserves sufficient storage for a pointer-to-char named a, and a
pointer-to-pointer-to-char named b.

a="hello world";

assigns a value to this pointer-to-char, the value in question being the
address of the first character in the given string literal.

But b[0]="string"; is a problem, not because there''s anything wrong with
the syntax, but because you''ve made an incorrect assumption.

b is a pointer-to-pointer-to-char, but you haven''t pointed it to any
pointers-to-char, so it is currently indeterminate. b[0]="string"; is
*not* an attempt to give a value to b. It is an attempt to give a value to
b[0]. But b[0] is meaningless unless b has a meaningful value.

You can give b a meaningful value in any of several ways, but the most
obvious is to allocate some fresh memory for it:

#include <stdlib.h>

/* allocate memory for some pointers-to-char */
char **cpalloc(size_t n)
{
char **ptr = malloc(n * sizeof *ptr);
if(ptr != NULL)
{
while(n--)
{
ptr[n] = NULL;
}
}
return ptr;
}

#include <stdio.h>

int main(void)
{
char *a, **b;
a = "what has it got in its pocketses?";
b = cpalloc(2);
if(b != NULL)
{
b[0] = "string";
b[1] = "nothing";

printf("%s\n", a);
printf("%s or %s\n", b[0], b[1]);

free(b);
}
return 0;
}

Be careful. The cpalloc function written above does not allocate storage
for strings, only for a collection of pointers to char. A pointer to char
is sufficient for pointing at a string, but not for storing it.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


emyl wrote:

>
here''s an elementary question. Assume I have declared two variables,
char *a, **b;
I can then give a value to a like
a="hello world";
The question is, how should I assign values to b? A simple
b[0]="string";
results in a segmentation fault.

"char **b;" declares a pointer to a pointer to char. You could
initialize it with "b = &a;" (provided the a declaration is
present). Then **b is a[0].

However note that your initialization of <a = "hello world";>
leaves a pointing to an unmodifiable string.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com


这篇关于将值分配给char数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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