字串缓冲区 [英] string buffer

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

问题描述

大家好,

假设我们定义了一个字符串缓冲区(数组),就像这样,


char array [] =" hello world" ;;

char buf [256]


有时,我注意到我们要么使用,


1. array( buf)

或使用,

2.& array(& buf)

或使用

3 。& array [0](& buf [0])


作为数组的起始地址,


例如,


strcpy(buf,array);

strcpy(& buf,array);

....


我想知道3种方法之间的区别,以及哪种方法是最正确的?

提前感谢,

乔治

解决方案

George2写道:


>

大家好,


假设我们定义了一个字符串缓冲区(数组),就像这样,


char array [ ] =你好orld;

char buf [256]


有时,我注意到我们要么使用,


1 。array(buf)

或者使用,

2.& array(& buf)

或者使用

3.& array [0](& buf [0])


作为数组的起始地址,


例如,


strcpy(buf,array);

strcpy(& buf,array);

...


我想知道3种方法之间的差异,以及哪种方法是最正确的?



strcpy的参数类型是:指向char的指针。


char * strcpy(char * restrict s1, const char * restrict s2);


当buff和数组用作参数时,

将它们的类型转换为指向char的指针。


(& buf)的类型是256个字符数组的指针,

因此'参数的参数类型错误。


N869

6.3.2其他操作数

6.3.2.1左值和函数指示符


[# 3]除非它是sizeof运算符的操作数或

一元&运算符,或者是一个字符串文字,用于初始化一个数组,一个类型为数组

类型的表达式被转换为一个带有类型的表达式``指向

类型的指针''''指向数组的初始元素

对象而不是左值。如果数组对象具有

寄存器存储类,则行为未定义。


-

pete


George2 schrieb:


char array [] =" hello world";

char buf [256]


有时,我注意到我们要么使用,


1. array(buf)

或者使用,

2.& array(& buf)

或者使用

3.& array [0](& buf [0])


作为数组的起始地址,



1和3在语义上是相同的。我更喜欢1.


2对于strcpy是错误的,正如已经指出的那样。它应该在

最少产生大多数编译器的警告。


问候,

Johannes


-

" Viele der Theorien der Mathematiker sind falsch und klar

Gottesl?¤sterlich。 Ich vermute,dass diese falschen Theorien genau

deshalb so geliebt werden。 - Prophet und Vision?¤rHansJoss aka

HJP in de.sci.mathematik< 47 ********************* *@news.sunrise.ch>


12月10日上午9:16,Johannes Bauer< dfnsonfsdu ... @ gmx.dewrote:
< blockquote class =post_quotes>
George2 schrieb:


char array [] =" hello world";

char buf [256]


有时,我注意到我们要么使用


1.数组(buf)

或使用,

2.& array(& buf)

或使用

3.& array [0](& buf [0])



我试过(buf,array)工作我试过((char *)buf,(const char

*)数组)也正在工作..但是当我尝试(& buf,& array)它

给出了警告..


但我觉得mpost app。语法是strcpy((char *)buf,(const

char *)数组)?


问候

Vikas


Hello everyone,
Suppose we defined a string buffer (array), like this,

char array[] = "hello world";
char buf[256]

Sometimes, I noticed that we either use,

1. array (buf)
or use,
2. &array (&buf)
or use
3. &array[0] (&buf[0])

as the beginning address of the array,

example like,

strcpy (buf, array);
strcpy (&buf, array);
....

I am wondering the differences between the 3 approaches, and which
approach is the most correct?
thanks in advance,
George

解决方案

George2 wrote:

>
Hello everyone,

Suppose we defined a string buffer (array), like this,

char array[] = "hello world";
char buf[256]

Sometimes, I noticed that we either use,

1. array (buf)
or use,
2. &array (&buf)
or use
3. &array[0] (&buf[0])

as the beginning address of the array,

example like,

strcpy (buf, array);
strcpy (&buf, array);
...

I am wondering the differences between the 3 approaches, and which
approach is the most correct?

The type of the parameters of strcpy is: pointer to char.

char *strcpy(char * restrict s1, const char * restrict s2);

When buff and array are used as arguments,
their types are converted to pointers to char.

The type of (&buf) is pointer to array of 256 char,
so that''s the wrong type of argument for the parameter.

N869
6.3.2 Other operands
6.3.2.1 Lvalues and function designators

[#3] Except when it is the operand of the sizeof operator or
the unary & operator, or is a string literal used to
initialize an array, an expression that has type ``array of
type'''' is converted to an expression with type ``pointer to
type'''' that points to the initial element of the array
object and is not an lvalue. If the array object has
register storage class, the behavior is undefined.

--
pete


George2 schrieb:

char array[] = "hello world";
char buf[256]

Sometimes, I noticed that we either use,

1. array (buf)
or use,
2. &array (&buf)
or use
3. &array[0] (&buf[0])

as the beginning address of the array,

1 and 3 are semantically identical. I prefer 1.

2 is wrong for strcpy, as was already pointed out to you. It should at
least yield a warning in most compilers.

Greetings,
Johannes

--
"Viele der Theorien der Mathematiker sind falsch und klar
Gottesl?¤sterlich. Ich vermute, dass diese falschen Theorien genau
deshalb so geliebt werden." -- Prophet und Vision?¤r Hans Joss aka
HJP in de.sci.mathematik <47**********************@news.sunrise.ch>


On Dec 10, 9:16 am, Johannes Bauer <dfnsonfsdu...@gmx.dewrote:

George2 schrieb:

char array[] = "hello world";
char buf[256]

Sometimes, I noticed that we either use,

1. array (buf)
or use,
2. &array (&buf)
or use
3. &array[0] (&buf[0])

I have tried (buf,array) working i tried ((char *)buf,(const char
*)array ) that also is working..but when i tried (&buf,&array) it
given warning..

But i think the mpost app. syntax would be strcpy((char *)buf,(const
char *)array)?

Regards
Vikas


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

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