char和char *之间的混淆以及与Arrays的连接 [英] confused between char and char* and connection to Arrays

查看:72
本文介绍了char和char *之间的混淆以及与Arrays的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解数组和char。 Stroustrup说,这是一个

字符串文字:


这是一个字符串文字


他说它的类型为/ const char /。他还说,因为保持与以前的C& C定义的兼容性。 C ++,它也是一个

CHAR POINTER a.k.a / char * /。但是这是一个错误,它不能在直到运行时捕获
,使用这样的

指针来修改字符串文字:

char * name =" Plato" // name是一个5个字符的数组

name [0] =''R''//错误

告诉我我是对还是错


#1 - p是6个CHARS的数组(包括''\ 0''),对吧?


#2 - p指向名字a.ka的第一个CHAR,''P'' 柏拉图。


#3 - 它与/ const char name完全相同[] =" Plato" /


#4 - 怎么样/ const char * [] =" Plato" /。这代表什么?

解决方案

* arnuld:


i am试图理解数组和char。 Stroustrup说,这是一个

字符串文字:


这是一个字符串文字


他说它的类型为/ const char /。



我怀疑他是这么说的。元素类型是''const char''。数组是''char const [n]''类型的
,其中n是

字符串文字中的字符数+ 1,终止的+1零字节。


他还说,因为保持了与以前C& C的定义相比的
兼容性。 C ++,它也是一个

CHAR POINTER a.k.a / char * /。



我怀疑他是这么说的。该数组可以转换为''char *''。


但是这是一个错误,不能是
在直到运行时捕获,使用这样的

指针修改字符串文字:


char * name =" Plato" // name是一个5 char的数组
name [0] =''R''//错误



是。


告诉我是对还是错


#1 - p是6个CHARS的数组(包括''\ 0''),对吗?



''p'的定义在哪里?


也许你的意思是''name''。


''name''上面是一个指向数组第一个元素的指针

of 6 chars。


#2 - p指向名字a.ka的第1个CHAR,P。 "柏拉图" ;.



假设你的意思是''name'',是的。


#3 - 它与/ const char name完全相同[] =" Plato" /



No.


#4 - 怎么样/ const char * [] =" Plato" /。这代表什么?



这不应该编译。

-

答:因为它弄乱了人们的订单通常阅读文字。

问:为什么这么糟糕?

A:热门帖子。

问:什么是最多的在usenet和电子邮件中烦人的事情?


3月29日下午1点28分,Alf P. Steinbach < a ... @ start.nowrote:


* arnuld:


这是一个字符串文字


他说它的类型为/ const char /。



我怀疑他是这么说的。元素类型是''const char''。数组是''char const [n]''类型的
,其中n是

字符串文字中的字符数+ 1,终止的+1零字节。



你的措辞比他明确(呃):-)


他还说,因为保持了与以前C& C的定义相比的
兼容性。 C ++,它也是一个

CHAR POINTER a.k.a / char * /。



我怀疑他是这么说的。该数组可转换为''char *''。



它不相同但可兑换。那么/

char const /和/ char * /之间的区别在哪里?


告诉我是对还是错


#1 - p是6个CHARS的数组(包括''\''') , 对 ?



''p'的定义在哪里?


也许你的意思是''name''。



是的,确切地说,我为混淆而道歉。


''name' '上面是一个指针指向数组的第一个元素

的6个字符。



谢谢


#3 - 它是*与/ const char name完全相同[] =" Plato" /



No.



i已经问过了:差异在哪里?


#4 - / const char怎么样* [] =" Plato" /。这代表什么?



这不应该编译。



:-(,我是一个可怜的程序员


谢谢Alf


* arnuld:


>


>>#3 - 它与/ const char name完全相同[] =" Plato" /


No.




i已经问过它:差异在哪里?



char const a [] ="柏拉图" ;;

char const * p =" Pluto";


a是一个包含六个字符的数组.sizeof(a)= 6(或者,更多细节,6 * 1,

其中1是char的大小,每个定义的大小为1。


p是指向第一个的指针一个大小字符数组的元素。

sizeof(p)=指针的大小在你的系统上,可能是4,它

不取决于大小完全是阵列。


一个你可以用数组而不是用指针做的事情是找到

元素的数量:


size_t const aSize = sizeof(a)/ sizeof (A [0]); // 6


尝试使用指针:


size_t const uhuh = sizeof(p)/ sizeof(p [0]) ; //指针的大小。


(最后一个表达式计算指针大小的原因是

,p [0]的定义意味着* (p + 0)根据定义,* p表示

是一个char,根据定义,它的大小为1,因此sizeof(p [0])为1.)

-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?


i am trying to understand arrays and char. Stroustrup says, this a
string literal:

"this is a string literal"

he says it is of type /const char/. he also says, because of keeping
compatibilities with previous definitions of C & C++, it is also a
CHAR POINTER a.k.a /char*/. however it is an error, which can not be
caught at untill runtime, to modify a string literal using such
pointer:
char* name = "Plato" // name is an array of 5 char
name[0] = ''R'' // error
tell me whether i am right or wrong

# 1 - p is an array of 6 CHARS (including ''\0'') , right ?

# 2 - p points to the 1st CHAR, ''P'' of name a.ka. "Plato".

# 3 - it is *exactly same as /const char name[] = "Plato"/

#4 - what about /const char*[] = "Plato"/. what does this represent ?

解决方案

* arnuld:

i am trying to understand arrays and char. Stroustrup says, this a
string literal:

"this is a string literal"

he says it is of type /const char/.

I doubt he says that. The element type is ''const char''. The array is
of type ''char const [n]'' where n is the number if characters in the
string literal + 1, the +1 for the terminating zero byte.

he also says, because of keeping
compatibilities with previous definitions of C & C++, it is also a
CHAR POINTER a.k.a /char*/.

I doubt he says that. The array is convertible to ''char*''.

however it is an error, which can not be
caught at untill runtime, to modify a string literal using such
pointer:
char* name = "Plato" // name is an array of 5 char
name[0] = ''R'' // error

Yes.

tell me whether i am right or wrong

# 1 - p is an array of 6 CHARS (including ''\0'') , right ?

Where is the definition of ''p''?

Perhaps you mean ''name''.

''name'' above is a pointer that points to the first element of an array
of 6 chars.

# 2 - p points to the 1st CHAR, ''P'' of name a.ka. "Plato".

Assuming you mean ''name'', yes.

# 3 - it is *exactly same as /const char name[] = "Plato"/

No.

#4 - what about /const char*[] = "Plato"/. what does this represent ?

That should not compile.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


On Mar 29, 1:28 pm, "Alf P. Steinbach" <a...@start.nowrote:

* arnuld:

"this is a string literal"

he says it is of type /const char/.


I doubt he says that. The element type is ''const char''. The array is
of type ''char const [n]'' where n is the number if characters in the
string literal + 1, the +1 for the terminating zero byte.

you wording is clear(er) than him :-)

he also says, because of keeping
compatibilities with previous definitions of C & C++, it is also a
CHAR POINTER a.k.a /char*/.


I doubt he says that. The array is convertible to ''char*''.


it is not same but it is convertible. so where the difference between /
char const/ and /char*/ lies ?

tell me whether i am right or wrong

# 1 - p is an array of 6 CHARS (including ''\0'') , right ?


Where is the definition of ''p''?

Perhaps you mean ''name''.

yes, exactly, i apologize for putting confusion.

''name'' above is a pointer that points to the first element of an array
of 6 chars.

thanks

# 3 - it is *exactly same as /const char name[] = "Plato"/


No.


i have already asked it: "where the difference lies ?"

#4 - what about /const char*[] = "Plato"/. what does this represent ?


That should not compile.

:-(, i am a poor programmer

thanks Alf


* arnuld:

>

>># 3 - it is *exactly same as /const char name[] = "Plato"/

No.



i have already asked it: "where the difference lies ?"

char const a[] = "Plato";
char const* p = "Pluto";

a is an array of six chars. sizeof(a) = 6 (or, in more detail, 6*1,
where 1 is the size of a char, which has size 1 per definition).

p is a pointer to the first element of an array of size chars.
sizeof(p) = the size of a pointer is on your system, probably 4, and it
depends not on the size of the array at all.

One thing you can do with the array and not with the pointer is to find
the number of elements:

size_t const aSize = sizeof(a)/sizeof(a[0]); // 6

Try the same with the pointer:

size_t const uhuh = sizeof(p)/sizeof(p[0]); // Size of a pointer.

(The reason the last expression evaluates to the size of a pointer is
that p[0] by definition means *(p+0) which by definition means *p which
is a char which by definition has size 1, so that sizeof(p[0]) is 1.)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


这篇关于char和char *之间的混淆以及与Arrays的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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