在char数组上使用printf [英] Using printf on char array

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

问题描述

您好我正在学习C ++,而且我仍然试图掌握指针

和其他C / C ++概念。如果有人能解释这个代码有什么问题,我将不胜感激:


---开始代码块---


#include" stdio.h"

#include" string.h"


void printText(char c [] ){

int len = strlen(c);

for(int i = 0; i< len; i ++){

printf (c [i]);

}

}


void main()

{

char b [2];

b [0] =''a'';

b [1] =''b'';

while(1){

printText(b);

}

}

---结束代码块---


正如你所看到的,我要做的是做一个功能

接受一个char数组,然后打印整个数组,char by char。

这应该是微不足道的,对吧?我觉得我错过了一些主要的

概念:)请启发我:)


BTW这是编译器抛给我的:

错误C2664:''printf'':无法将参数1从''char''转换为

''conc char *''

从积分类型转换为指针类型需要

reinterpret_cast,C风格演员或函数风格演员

问候

Pontus F.

Hi I am learning C++ and I''m still trying to get a grip of pointers
and other C/C++ concepts. I would appreciate if somebody could explain
what''s wrong with this code:

---begin code block---

#include "stdio.h"
#include "string.h"

void printText(char c[]){
int len = strlen(c);
for (int i = 0; i < len; i++) {
printf(c[i]);
}
}

void main()
{
char b[2];
b[0] = ''a'';
b[1] = ''b'';
while(1){
printText(b);
}
}
---end code block---

As you can see, what I''m trying to do is to make a function which
accepts a char array and then prints the entire array, char by char.
This should be trivial, right? I feel like I''ve missed some major
concept here :) Enlighten me please :)

BTW this is what the compiler throws at me:
error C2664: ''printf'' : cannot convert parameter 1 from ''char'' to
''const char *''
Conversion from integral type to pointer type requires
reinterpret_cast, C-style cast or function-style cast
regards
Pontus F.

推荐答案

Pontus F写道:
Pontus F wrote:
嗨我正在学习C ++,我仍然想要抓住指针
和其他C / C ++概念。如果有人能够解释这段代码有什么问题,我将不胜感激:


它没有缩进。 :-)请在将代码发布到新闻组之前更改

TAB到(两个)空格。新闻阅读器在线路开头吃TAB。 :-(

---开始代码块---

#include" stdio.h"
#include" string.h"

void printText(char c []){
int len = strlen(c);
for(int i = 0; i< len; i ++){
printf(c [i]);
}
}
void main()
{/ b> char b [2];
b [0] =''a'';
b [1] =''b'';
while(1){
printText(b);
}
}
---结束代码块---

正如你所看到的,我要做的是创建一个接受char的函数数组,然后打印整个数组,char由char。
这应该是微不足道的,对吧?我觉得我已经错过了一些主要的概念:)请启发我:)

BTW这就是编译器抛出的对象:
错误C2664:''printf'':无法将参数1从''char''转换为
''const char *''<从积分类型到指针类型的转换需要reinterpret_cast,C-sty le cast或函数式演员
Hi I am learning C++ and I''m still trying to get a grip of pointers
and other C/C++ concepts. I would appreciate if somebody could explain
what''s wrong with this code:
It is not indented. :-) Please before you post code to newsgroups change
TABs to (two) spaces. Newsreaders eat TABs on the beginning of lines. :-(
---begin code block---

#include "stdio.h"
#include "string.h"

void printText(char c[]){
int len = strlen(c);
for (int i = 0; i < len; i++) {
printf(c[i]);
}
}

void main()
{
char b[2];
b[0] = ''a'';
b[1] = ''b'';
while(1){
printText(b);
}
}
---end code block---

As you can see, what I''m trying to do is to make a function which
accepts a char array and then prints the entire array, char by char.
This should be trivial, right? I feel like I''ve missed some major
concept here :) Enlighten me please :)

BTW this is what the compiler throws at me:
error C2664: ''printf'' : cannot convert parameter 1 from ''char'' to
''const char *''
Conversion from integral type to pointer type requires
reinterpret_cast, C-style cast or function-style cast




你需要学习printf!一个教程,(快速浏览)好像是一个很好的是 http://cplus.about.com/library/weekly/aa032302a.htm

int printf(const char * format,arg1, arg2,arg3,......);


您没有给出格式字符串,而是要打印的参数!


我可以给你解决方案,但我会成为一个臭味。请阅读

教程(这是一个简短的页面),如果你无法弄清楚

怎么办,请回到这里。我想如果我只是告诉你在那里输入什么,你就不会从中学到任何东西。


-

WW aka Attila



You need to learn printf! One tutorial, which (with a fast glance) seems to
be good is http://cplus.about.com/library/weekly/aa032302a.htm

int printf(const char *format, arg1, arg2, arg3, ......);

You did not give the format string, but the argument you want to print!

I could give you the solution, but I will be a stinker. Please read the
tutorial (it is a short page) and get back here if you could not figure out
what to do. I think if I just tell you what to type there you won''t learn
anything from it.

--
WW aka Attila


Pontus F写道:
Pontus F wrote:

您好我正在学习C ++而我仍然想要获得一个掌握指针
和其他C / C ++概念。


你所拥有的几乎是C,虽然它可以用作C ++。选择一个

语言。

如果有人能解释这个代码有什么问题,我将不胜感激:

- -begin代码块---

#include" stdio.h"
#include" string.h"


#include< stdio.h>

#include< string.h>


void printText (char c []){
int len = strlen(c);
for(int i = 0; i< len; i ++){
printf(c [i]);


获取一本书,然后查找printf()函数的签名。它

需要char *作为它的第一个参数。废弃所有

此代码打印字符串也毫无意义。有printf()格式。

}
}




main()总是返回int。

{
char b [2];
b [0] =''a'';
b [1] =''b '';
while(1){
printText(b);


上面的函数正在使用char缓冲区的strlen(),

要求它是一个以null结尾的C样式字符串。你没有。

你的程序将无法正常工作。你可以传递大小,但你最好用字符串来支付



你想要完成什么?

正如你所看到的,我正在尝试做的是创建一个函数,它接受一个char数组,然后打印整个数组,char由char。
这应该是微不足道的,对吧?我觉得我错过了一些主要的概念:)请给我启发:))

Hi I am learning C++ and I''m still trying to get a grip of pointers
and other C/C++ concepts.
What you have is pretty much C, although it will work as C++. Pick a
language.
I would appreciate if somebody could explain
what''s wrong with this code:

---begin code block---

#include "stdio.h"
#include "string.h"
#include <stdio.h>
#include <string.h>

void printText(char c[]){
int len = strlen(c);
for (int i = 0; i < len; i++) {
printf(c[i]);
Get a book, then look up the signature of the printf() function. It
requires a char* as its first argument. It''s also pointless to waste all
this code printing a string. There are printf() formats for that.
}
}

void main()
main() returns int ALWAYS.
{
char b[2];
b[0] = ''a'';
b[1] = ''b'';
while(1){
printText(b);
Your function above was taking strlen() of the char buffer, which
requires it to be a null-terminated C-style string. You don''t have that.
Your program will not work. You could pass in the size, but you are
better off with strings.

What are you trying to accomplish?
As you can see, what I''m trying to do is to make a function which
accepts a char array and then prints the entire array, char by char.
This should be trivial, right? I feel like I''ve missed some major
concept here :) Enlighten me please :)




见上文。


Brian Rodenborn



See above.

Brian Rodenborn


WW写道:
WW wrote:
它没有缩进。 :-)请在将代码发布到新闻组之前将TAB更改为(两个)空格。新闻阅读器在线路开头吃TAB。 : - (
It is not indented. :-) Please before you post code to newsgroups change
TABs to (two) spaces. Newsreaders eat TABs on the beginning of lines. :-(



一些新闻阅读器。它为我缩进。


Brian Rodenborn


Some newsreaders. It was indented for me.

Brian Rodenborn


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

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