Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Using printf on char array

Reply
Thread Tools

Using printf on char array

 
 
Pontus F
Guest
Posts: n/a
 
      10-13-2003
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.
 
Reply With Quote
 
 
 
 
WW
Guest
Posts: n/a
 
      10-13-2003
Pontus F wrote:
> 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


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


 
Reply With Quote
 
 
 
 
Default User
Guest
Posts: n/a
 
      10-13-2003
Pontus F wrote:
>
> 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


See above.



Brian Rodenborn
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      10-13-2003
WW wrote:

> It is not indented. Please before you post code to newsgroups change
> TABs to (two) spaces. Newsreaders eat TABs on the beginning of lines.



Some newsreaders. It was indented for me.



Brian Rodenborn
 
Reply With Quote
 
WW
Guest
Posts: n/a
 
      10-14-2003
Default User wrote:
> WW wrote:
>
>> It is not indented. Please before you post code to newsgroups
>> change TABs to (two) spaces. Newsreaders eat TABs on the beginning
>> of lines.

>
> Some newsreaders. It was indented for me.


Yeah. I was very surprised that (for example) the one coming with KDE has
this same "feature". I wanted to look at the RFCs, but after the reference
to the 55th other RFC I gave up.

--
WW aka Attila


 
Reply With Quote
 
Ron Samuel Klatchko
Guest
Posts: n/a
 
      10-14-2003
Pontus F <--> wrote in message news:<(E-Mail Removed)>. ..
> 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:
>
> void printText(char c[]) {
> int len = strlen(c);
> for (int i = 0; i < len; i++) {
> printf(c[i]);
> }
> }
>
> 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


Well, since you're posting on comp.lang.c++, my first answer is to say
don't bother with the C compatible formatting library. It's neither
type safe nor extensible so it's a pain to use.

Instead you should look into using C++ iostreams. Instead of worry
abouting printf format strings, all you would have to do is convert
the printf to:

cout << c[i];

and the compiler will make sure the object is properly displayed.

But while C++ iostreams are easier to use, I have never found a good
way to integrate i18ned message catalogs with them, so another answer
is that sticking with C output functions are the way to go.

printf() is a very powerful command. It can take multiple arguments
and combine them according to a format string. This format string
tells how to interpret the arguments and any special instructions
(such as print an integer right justified to 8 spaces with leading
0's, etc). The full description of printf formatting strings is quite
large so I'm not going to attempt to summarize for you.

But for simply outputting strings or characters, printf is way
overkill. If you want to learn that family of functions, you should
also learn about putc(), fputc(), puts() and fputs(). Any decent C
book should cover those for you.

samuel
 
Reply With Quote
 
Pontus F
Guest
Posts: n/a
 
      10-14-2003
Thanks to everyone who replied. I realize that printf was overkill for what
i was trying to do . Anyway after reading up on how to use the
cin/cout/printf functions i rewrote the code into this (now correctly
indented, sorry about that in the original post):

#include <iostream>
#include <string>
using namespace std;

void printText(char c[]){
cout << c << "\n";
}

int main() {
char myString[20];
while(1){
cout << "Text to print: ";
cin >> myString;
printText(myString);
}
return 0;
}

Which works alot better
Thanks again
Pontus F.


"WW" <(E-Mail Removed)> skrev i meddelandet
news:bmfa4l$dv2$(E-Mail Removed)...
> Pontus F wrote:
> > 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

>
> 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
>
>



 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      10-14-2003
Pontus F wrote:
>
> Thanks to everyone who replied. I realize that printf was overkill for what
> i was trying to do . Anyway after reading up on how to use the
> cin/cout/printf functions i rewrote the code into this (now correctly
> indented, sorry about that in the original post):


Don't top-post. Your replies belong following properly trimmed quotes.

> #include <iostream>
> #include <string>
> using namespace std;


This looks better.

> void printText(char c[]){
> cout << c << "\n";
> }


This is not quite the same. You are putting a newline after every
character output. Doesn't sound like what you really want, but maybe it
is.

> int main() {
> char myString[20];


Hmmm. You include the <string> header, but then use char buffers. You
don't end up using anything from <string>.

> while(1){
> cout << "Text to print: ";
> cin >> myString;


Here you have no overflow protection if someone enters more than 19
characters. Also, cin delimits input by white space, so if the entry at
the console is:

Hello World!

Then only the "Hello" would be processed.

> printText(myString);


At least the UB from an unterminated char buffer is eliminated.

> }
> return 0;
> }
>
> Which works alot better


Somewhat, but it's still brittle. I'd use std::string in place of that
char buffer at your stage. That eliminates one big hole. Then use
getline() read in an entire line rather than a word.

What book are you using?



Brian Rodenborn
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      10-14-2003
Default User wrote:

> > void printText(char c[]){
> > cout << c << "\n";
> > }

>
> This is not quite the same. You are putting a newline after every
> character output. Doesn't sound like what you really want, but maybe it
> is.


Sorry, misread that. Never mind.



Brian Rodenborn
 
Reply With Quote
 
Pontus F
Guest
Posts: n/a
 
      10-14-2003

"Default User" <(E-Mail Removed)> skrev i meddelandet
news:(E-Mail Removed)...
> Pontus F wrote:
> >
> > Thanks to everyone who replied. I realize that printf was overkill for

what
> > i was trying to do . Anyway after reading up on how to use the
> > cin/cout/printf functions i rewrote the code into this (now correctly
> > indented, sorry about that in the original post):

>
> Don't top-post. Your replies belong following properly trimmed quotes.


My bad, sorry.

> > #include <iostream>
> > #include <string>
> > using namespace std;

>
> This looks better.
>
> > void printText(char c[]){
> > cout << c << "\n";
> > }

>
> This is not quite the same. You are putting a newline after every
> character output. Doesn't sound like what you really want, but maybe it
> is.
>
> > int main() {
> > char myString[20];

>
> Hmmm. You include the <string> header, but then use char buffers. You
> don't end up using anything from <string>.


sorry I just cut out the part of the code that is relevant, but forgot that
#include.
I use string in another part of my program.


> > while(1){
> > cout << "Text to print: ";
> > cin >> myString;

>
> Here you have no overflow protection if someone enters more than 19
> characters. Also, cin delimits input by white space, so if the entry at
> the console is:
>
> Hello World!
>
> Then only the "Hello" would be processed.


Hmm yes that's true. I didn't notice that since I've only tried to output
single
words so far. BTW, the program is supposed to output text via the parallel
port to a vacuum flourescent display (VFD). The program is now fully
functional . Oh, if I enter more than 20 chars, nothing in particular
happens.
Doesn't my program care if I overflow an array? Is the array size perhaps
dynamically redefined automagically?
The text output to the VFD isn't affected by this though, I truncate the
char array
in another part of the program.

> > printText(myString);

>
> At least the UB from an unterminated char buffer is eliminated.
>
> > }
> > return 0;
> > }
> >
> > Which works alot better

>
> Somewhat, but it's still brittle. I'd use std::string in place of that
> char buffer at your stage. That eliminates one big hole. Then use
> getline() read in an entire line rather than a word.


Thanks

> What book are you using?


I started with "C++ for dummies" by Stephen Davis which wasn't very
good IMHO. Perhaps I'm not the right kind of dummy .
Now reading "Thinking in C++" by Bruce Eckel instead, I like this
one a lot better.

Pontus F


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
length of 2D Array >> char **myString= (char **) malloc (sizeof (char *)); davidb C++ 6 09-01-2006 05:57 PM
length of 2D Array >> char **myString= (char **) malloc (sizeof (char *)); davidb C++ 0 09-01-2006 03:22 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
Problem- strcat with char and char indexed from char array aldonnelley@gmail.com C++ 3 04-20-2006 07:32 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM



Advertisments