Passing the adress of a single item of a char array

  • Thread starter =?ISO-8859-1?Q?Dennis_Pl=F6ger?=
  • Start date
?

=?ISO-8859-1?Q?Dennis_Pl=F6ger?=

Hi all!

I'm currently having some problems parsing a char array in c++. (And
yes, I'm a half-newbie ;-)) Perhaps you can help me with this:

#include <iostream>

using std::cout;

void outchar(char *outcharstring)
{
cout << outcharstring;
}

void ftest(char *teststring)
{
int i=0;

while (teststring != '\0')
{

outchar(&teststring);
i++;

}

}

int main()
{


char test[]="Hello.";

ftest(test);

}

Of course, this is just a sample program. But basically I would like
to give a function, that needs the adress of a string, a char, that is
one item of another string.

The output of the code above is:
Hello.ello.llo.lo.o..

What I would like is just "Hello.". How can I accomplish this?

Thanks everybody.

Dennis
 
W

Wolfgang Senfter

What I would like is just "Hello.". How can I accomplish this?
void outchar(char *outcharstring)
{
cout << outcharstring;
}

void outchar(char outchar)
{
cout << outchar;
}

so you give only one char to the function. or use your function istead of
ftest
 
C

Christopher Benson-Manica

Dennis Plöger said:
I'm currently having some problems parsing a char array in c++. (And
yes, I'm a half-newbie ;-)) Perhaps you can help me with this:

I'll try...
void outchar(char *outcharstring)
{
cout << outcharstring;
}
void ftest(char *teststring)
{
int i=0;
while (teststring != '\0')
{
outchar(&teststring);
i++;
}
}

Of course, this is just a sample program. But basically I would like
to give a function, that needs the adress of a string, a char, that is
one item of another string.
The output of the code above is:
Hello.ello.llo.lo.o..

This is because you're giving the << operator a character pointer...

Hello.\0
^ <- Give this character pointer to cout and you get "Hello."

Hello.\0
^ <- Increment the character pointer - you still have a string, so
"ello." is what gets printed by outchar.

Hello.\0
^ <- Prints "llo." (etc.)

Did that help? cout has the put() member function that allows you to write
one character at a time - presumably what you wanted outchar() to do...

#include <iostream>

using namespace std;

void ftest( char *teststring )
{
int i=0;
char *cp=teststring;
while( *cp ) {
cout.put( *cp++ );
}
cout << endl;
}

int main( void )
{
char *test="Hello.";
ftest( test );
return( EXIT_SUCCESS );
}
 
C

Christopher Benson-Manica

Christopher Benson-Manica said:
return( EXIT_SUCCESS );

Um, I don't know which C++ header defines EXIT_SUCCESS - in C it was stdio.h.
If someone could tell me what it is, I'd appreciate it...
 
C

Christopher Benson-Manica

Christopher Benson-Manica said:
return( EXIT_SUCCESS );

Um, I don't know which C++ header defines EXIT_SUCCESS - in C it was stlib.h.
If someone could tell me what it is, I'd appreciate it...
 
C

Christopher Benson-Manica

Dennis Plöger said:
I'm currently having some problems parsing a char array in c++. (And
yes, I'm a half-newbie ;-)) Perhaps you can help me with this:

I'll try...
void outchar(char *outcharstring)
{
cout << outcharstring;
}
void ftest(char *teststring)
{
int i=0;
while (teststring != '\0')
{
outchar(&teststring);
i++;
}
}

Of course, this is just a sample program. But basically I would like
to give a function, that needs the adress of a string, a char, that is
one item of another string.
The output of the code above is:
Hello.ello.llo.lo.o..

This is because you're giving the << operator a character pointer...

Hello.\0
^ <- Give this character pointer to cout and you get "Hello."

Hello.\0
^ <- Increment the character pointer - you still have a string, so
"ello." is what gets printed by outchar.

Hello.\0
^ <- Prints "llo." (etc.)

Did that help? cout has the put() member function that allows you to write
one character at a time - presumably what you wanted outchar() to do...

#include <iostream>
#include <cstdlib>

using namespace std;

void ftest( char *teststring )
{
int i=0;
char *cp=teststring;
while( *cp ) {
cout.put( *cp++ );
}
cout << endl;
}

int main( void )
{
char *test="Hello.";
ftest( test );
return( EXIT_SUCCESS );
}
 
K

Kevin Goodsell

Christopher said:
Um, I don't know which C++ header defines EXIT_SUCCESS - in C it was stlib.h.
If someone could tell me what it is, I'd appreciate it...

It's still in <stdlib.h> (deprecated), and also in <cstdlib>.

-Kevin
 
?

=?ISO-8859-1?Q?Dennis_Pl=F6ger?=

Hi all!

Thank you for now.

The problem is, that I take the sample "outchar"-function from a
library (namely the sdlttf-library), which takes the argument as a *
char to generate a surface using a TrueType-font (as a backnote). So I
really need to give the function a single char.

Perhaps some other ideas?

Bye,
Dennis
 
C

Christopher Benson-Manica

Dennis Plöger said:
The problem is, that I take the sample "outchar"-function from a
library (namely the sdlttf-library), which takes the argument as a *
char to generate a surface using a TrueType-font (as a backnote). So I
really need to give the function a single char.

You can still do it the way I did - just call your font function instead of
cout.put() and it'll work like a charm, I believe.
 
K

Karl Heinz Buchegger

Dennis said:
Hi all!

Thank you for now.

The problem is, that I take the sample "outchar"-function from a
library (namely the sdlttf-library), which takes the argument as a *
char to generate a surface using a TrueType-font (as a backnote). So I
really need to give the function a single char.

I doubt it.
If a function takes a char* is an input parameter, you can bet your ass
that it wants a C-style string. That is: a sequence of characters
terminated with '\0'. Dont' let the functions name fool you.

(Of course, unless otherwise noted in the documentation).

So we can take it for granted that outchar() want's a full
blown C-style string. All you have is a single character
(which you get by extracting it from the original string).

Thus your strategy must be: create a C-style string from
that single character:

void ftest( char * teststring )
{
char Single[2];

Single[1] = '\0'; // terminate with '\0'. Thus setting
// Single[0] to a character will form
// a well formed C-style string

while( teststring != '\0' )
{
Single[0] = teststring;
outchar( Single );
i++;
}
}
 
?

=?ISO-8859-1?Q?Dennis_Pl=F6ger?=

Hi!
char Single[2];

Single[1] = '\0'; // terminate with '\0'. Thus setting
// Single[0] to a character will form
// a well formed C-style string

while( teststring != '\0' )
{
Single[0] = teststring;


That's it! Thank you very much.

Dennis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top