char *和char [英] char* and char

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

问题描述




是什么区别:


char * a =新字符;


char * b = new char [10];


char c [10];

问候

BN < br>

Hi,

whats the difference between:

char* a = new char;

char* b = new char[10];

char c [10];
Regards
BN

推荐答案

" Ying Yang" <易****** @ hotmail.com>在消息中写道

新闻:3f ******** @ news.iprimus.com.au ...
"Ying Yang" <Yi******@hotmail.com> wrote in message
news:3f********@news.iprimus.com.au...

最新区别:

char * a =新char;

char * b = new char [10];


''a''和'b''都是指向char的指针。也就是说,它们每个都包含一个char的内存中的

地址。在''b''的情况下,在第一个之后恰好有另外一个
9个字符,但是程序员应该知道

(包括使用删除[]代表''b''而只是'删除'代表''a''以后

删除它们时)。一旦创建,编译器将两个指针视为

相同。

char c [10];
Hi,

whats the difference between:

char* a = new char;

char* b = new char[10];
''a'' and ''b'' are both pointers to a char. That is, they each contain the
address in memory of a char. In the case of ''b'', there happens to be another
9 chars after the first one, but it is up to the programmer to be aware of
that (including using "delete[]" for ''b'' and just "delete" for ''a'' later
when you delete them). Once created, the compiler treats both pointers the
same.
char c [10];




' 'c''是10个字符的数组。如果你使用''c''期望char *,那么

编译器将通过获取数组中第一个char的地址来创建char *。但''c''本身就是一个数组,而不是一个指针,所以它与''''和''''的类型不同。


DW



''c'' is an array of 10 chars. If you use ''c'' where a char * is expected, the
compiler will create the char * by taking the address of the first char in
the array. But ''c'' itself is an array, not a pointer, so it is a different
type from ''a'' and ''b''.

DW


Ying Yang写道:
Ying Yang wrote:


是什么区别:

char * a = new char;


a是一个指向字符的指针,由表达式new char初始化。

表达式new char为免费

存储中的一个字符分配内存(根据当前的措辞) (标准)通过无所事事将其初始化为
。所以a将指向那个随机的免费

商店中的角色(有时称为堆)。


如果新的char不能为char类型的一个对象分配内存,它将抛出

一个std :: bad_alloc异常,它将被适当的

ctach-handler捕获,或者会导致系统调用一个名为terminate()的函数,

默认情况下会调用一个名为abort()的函数,并且会停止程序执行的


char * b = new char [10];


" b"是一个指向字符的指针,由表达式new char [10]初始化。

表达式new char [10]为10

字符分配一块连续内存免费商店和(根据

标准的当前措辞)通过什么都不做来初始化它。在初始化之后b将* b $ b指向那10个随机中的第一个。免费商店中的角色

(有时称为堆)。


休息与以前相同。

char c [10 ];
Hi,

whats the difference between:

char* a = new char;
"a" is a pointer to a character, initialized by the expression new char.
The expression new char allocates memory for one character from the free
store and (according to the current wording of the standard) initialized it
by doing nothing. So "a" will point at that "random" character in the free
store (sometimes called heap).

If new char cannot allocate memory for one object of type char it will throw
an std::bad_alloc exception, which will be either caught by an appropriate
ctach-handler or will cause the system to call a function named terminate(),
which will by default call a function named abort() and that one will stop
the programs'' execution.
char* b = new char[10];
"b" is a pointer to a character, initialized by the expression new char[10].
The expression new char[10] allocates a block of continuous memory for 10
character from the free store and (according to the current wording of the
standard) initialized it by doing nothing. After initialization "b" will
point at the first of those 10 "random" characters in the free store
(sometimes called heap).

Rest is the same as before.
char c [10];




这个可以是很多东西。因此,让我们跳过初始化和意义

(因为它可以是名称空间作用域或自动或成员声明)和

让我们只选择类型。


" c"是一个包含10个字符的数组。这意味着它具有类型:数组

10个字符。当使用c时在某些表达式中它将(所谓的)

衰减成指向该数组的第一个元素的指针。在这种情况下,

的行为与b非常相似。之前,除了属于

" c"的存储区域之外。不一定是来自免费商店:


c [2]; // c的第三个元素
b [2]; // b的第三个元素

重要的是要注意虽然很多次c将表现为指向它所代表的数组的第一个元素的

指针,它不是一个。为了我们的方便,它可以表现为一个(在某些表达式中)。


-

Attila aka WW



This one can be many things. So let''s skip initialization and meaning
(because it can be namespace scope or automatic or member declaration) and
let''s just go for the type.

"c" is an array of 10 characters. This means that it has the type: array of
10 characters. When using "c" in certain expressions it will (so-called)
decay into a pointer to the first element of that array. In such a case it
will behave much like "b" before, except that the memory area belonging to
"c" is not necessarily from the free-store:

c[2]; // third element of c
b[2]; // third element of b

It is important to note that while many many times "c" will behave as a
pointer to the first element of the array it represents, it is not one. It
just can behave as one (in certain expressions) for our convenience.

--
Attila aka WW





Ying Yang写道:


Ying Yang wrote:


最新区别:

char * a =新字符;

a

+ ------- + + --- +

| ø------------------> | |

+ ------- + + --- +

char * b = new char [10];


b

+ ------- + + --- + --- + --- + --- + --- + --- + --- + - - + --- + --- +

| ø------------------> | | | | | | | | | | |

+ ------- + + --- + --- + --- + --- + --- + --- + --- + --- + --- + --- +

char c [10];

Hi,

whats the difference between:

char* a = new char;
a
+-------+ +---+
| o------------------>| |
+-------+ +---+
char* b = new char[10];

b
+-------+ +---+---+---+---+---+---+---+---+---+---+
| o------------------>| | | | | | | | | | |
+-------+ +---+---+---+---+---+---+---+---+---+---+
char c [10];




c

+ - - + --- + --- + --- + --- + --- + --- + --- + --- + --- +

| | | | | | | | | | |

+ --- + --- + --- + --- + --- + --- + --- + --- + --- + --- +


HTH


-

Karl Heinz Buchegger
kb ****** @ gascad.at


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

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