连接问题 [英] A concatenation problem

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

问题描述

以下不工作


int num = 0,

char * string;


string =" qwerty";

strcpy(string,num);

strcpy(string," .png");

The following is not working

int num = 0,
char *string;

string = "qwerty";
strcpy(string, num);
strcpy(string, ".png");

推荐答案

Caroline< pl *** @ letsdothatagain.com>这样说:
Caroline <pl***@letsdothatagain.com> spoke thus:
以下是行不通的


真的不是一个惊喜...

int num = 0,
char * string;
string =" qwerty" ;;


罚款和花花公子,但是......

strcpy(string,num);


....这是什么? strcpy的原型是


char * strcpy(char * dst,const char * src);


你认为什么时候发生什么?假装num是一个const char

*?它做任何有用的概率都很低。

strcpy(string," .png");
The following is not working
Not really a surprise...
int num = 0,
char *string; string = "qwerty";
Fine and dandy, but...
strcpy(string, num);
....what is this? The prototype for strcpy is

char * strcpy( char * dst, const char * src );

What do you suppose happens when you pretend that num is a const char
*? It certainly has a very low probability of doing anything useful.
strcpy(string, ".png");




同样如此。 ..字符串指向一个字符串文字,这不是

保证可以修改。 strcpy''ing到它肯定是一个糟糕的

计划。


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



And likewise... string points to a string literal, which is not
guaranteed to be modifiable. strcpy''ing to it is certainly a bad
plan.

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


" Caroline" < PL *** @ letsdothatagain.com>在消息中写道

news:da ************************** @ posting.google.c om ...
"Caroline" <pl***@letsdothatagain.com> wrote in message
news:da**************************@posting.google.c om...
以下不起作用

int num = 0,
char * string;

string =" qwerty" ;;
strcpy(string,num);
strcpy(string," .png");
The following is not working

int num = 0,
char *string;

string = "qwerty";
strcpy(string, num);
strcpy(string, ".png");




1.你的字符串指向常量 QWERTY" ;.你不能修改常量字符串的内容



2. strcpy(string,num)不符合函数的原型

strcpy()。它需要两个指向char的参数作为参数。你可能想要

sprintf()



1. Your string points to constant "qwerty". You cannot modify the contents
of a constant string.
2. strcpy(string, num) does not conform to the prototype of the function
strcpy(). It takes two pointers to char as arguments. You probably want
sprintf()


Caroline写道:
Caroline wrote:
以下是行不通的

int num = 0,
char * string;

string =" qwerty" ;;
strcpy(string,num);
strcpy(string, " .png");
The following is not working

int num = 0,
char *string;

string = "qwerty";
strcpy(string, num);
strcpy(string, ".png");




也不应该,原因有几个。


在你的代码中,`string ''指向一个字符串文字(qwerty),这是

不可修改。


函数strcpy()需要一个(const)字符*作为它的第二个参数,

你提供的,'num'',一个int。


即使你的用法是正确的,你也是' d将覆盖

字符串'指向的内容,这显然不是你想要的。


查找函数`sprintf()''。 *那就是你要找的东西。


HTH,

--ag


BTW - 标识符`string''保留用于实现。


-

-

Artie Gold - - 德克萨斯州奥斯汀



Nor should it be, for several reasons.

In your code, `string'' points to a string literal ("qwerty"), which is
not modifiable.

The function strcpy() expects a (const) char * as its second argument,
where you have provided, `num'', an int.

Even if your use had been correct, you''d be overwriting whatever
`string'' points to, which is evidently not what you want.

Look up the function `sprintf()''. *That''s* what you''re looking for.

HTH,
--ag

BTW - the identifier `string'' is reserved for the implementation.

--
--
Artie Gold -- Austin, Texas


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

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