非常简单的问题 [英] Extremely Simple Question

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

问题描述

这有什么问题,而且......你不能宣布char变量?


#include< iostream>


int main()

{

int a =" * * * * * * * * \ n";

std :: cout<< a;

返回EXIT_SUCCESS;

}


thx

解决方案

Mike Lundell写道:

这有什么问题,并且..你不能声明char变量?



blah blah blah


oops,对不起决斗帖...我的时间已关闭在我的盒子上所以我没有请看最近的

帖子。


Mike Lundell写道:

这有什么问题,并且..你不能宣布char变量?

#include< iostream>



int a =" * * * * * * * * \ n";
std :: cout<< a;
返回EXIT_SUCCESS;
}

thx



您无法将字符串转换为int。


" * * * * * * * * \ n"来自char *类型,

这意味着你可以将这个

字符串表示为第一个字符的指针。

一个函数或方法,用这个字符串处理

,将从这个

的第一个指针开始,然后沿着这个

字符串,直到它找到结束

字符串,标记为0(不要将
与0混合,这将是

字符0。


你可以做什么,有些像是


int a =''k'';


将''k'转换成'ASCII'

数。这就是为什么你也可以做'/ b'这样的事情''('''''<''b'')std :: cout<<

" hello world"''。

这只适用于单个字符

而不是字符串。


解决方案:替换


int a =" * * * * * * * * \ n" ;;


with


char * a =" * * * * * * * * \ n" ;;


再见鲍里斯





Allan Bruce写道:


char * a =" * * * * * * * * \ n" ;;

再见鲍里斯

这也错了 - 你需要



为什么?

char a [] =" * * * * * * * * \ n" ;;



char * a = new char [16] =" * * * * * * * * \ n" ;;


我的朋友,这是严重的错误。你不能在C ++中分配

数组,甚至不能分配字符数组。


char * a = strcpy(new char [17]," * * * * * * * * \ n");


可行,但大部分时间都不是好主意。




唯一可以做得更好的事情


char * a =" * * * * * * * * \ n" ;;


是将指针的数据类型替换为


const char * a =" * * * * * * * * \ n" ;;


但除此之外,鲍里斯是完全正确的。


-

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


what''s wrong with this, and .. can you not declare "char" variables?

#include <iostream>

int main()
{
int a = "* * * * * * * *\n";
std::cout << a;
return EXIT_SUCCESS;
}

thx

解决方案

Mike Lundell wrote:

what''s wrong with this, and .. can you not declare "char" variables?


blah blah blah

oops, sorry for duel post.. my time is off on my box so i didn''t see the
post as most recent.


Mike Lundell wrote:

what''s wrong with this, and .. can you not declare "char" variables?

#include <iostream>

int main()
{
int a = "* * * * * * * *\n";
std::cout << a;
return EXIT_SUCCESS;
}

thx


You cannot convert a string to an int.

"* * * * * * * *\n" is from type char*,
which means that you can represent this
string as a pointer on the first character.
A function or method, that would handle
with this string, would start from this
first pointer and would go along this
string until it finds the end of the
string, which is marked with 0 (don''t
mix it up with "0", which would be the
character "0").

What you can do, is somethind like

int a = ''k'';

which convert the ''k'' into it''s ASCII
number. That''s why you can also do
things like ''if (''a'' < ''b'') std::cout <<
"hello world"''.
This works only with single characters
and not strings.

solution: replace

int a = "* * * * * * * *\n";

with

char* a = "* * * * * * * *\n";

bye Boris




Allan Bruce wrote:


char* a = "* * * * * * * *\n";

bye Boris

This is wrong too - you either need



why?
char a[] = "* * * * * * * *\n";

or

char *a = new char[16] = "* * * * * * * *\n";
That, my friend, is seriously wrong. You can''t assign
arrays in C++, not even character arrays.

char *a = strcpy( new char[17], "* * * * * * * *\n" );

would work, but it''s not a good idea most of the time.



The only thing that could be made better in

char* a = "* * * * * * * *\n";

is to replace the data type of the pointer to

const char* a = "* * * * * * * *\n";

but other then that, Boris is completely correct.

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


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

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