By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,213 Members | 1,105 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,213 IT Pros & Developers. It's quick & easy.

Passing the adress of a single item of a char array

P: n/a
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[i] != '\0')
{

outchar(&teststring[i]);
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
Jul 19 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
pl*****@pharmlog.de (=?ISO-8859-1?Q?Dennis_Pl=F6ger?=) writes:

Hello,
int main()
{
char test[]="Hello."; ftest(test); } What I would like is just "Hello.". How can I accomplish this?


What about calling outchar instead of ftest?

Bye,
Chris Dams
Jul 19 '05 #2

P: n/a
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
Jul 19 '05 #3

P: n/a

"Chris Dams" <ch****@gamow.sci.kun.nl> wrote in message
news:bk**********@gamow.sci.kun.nl...
pl*****@pharmlog.de (=?ISO-8859-1?Q?Dennis_Pl=F6ger?=) writes:

Hello,
int main()
{


char test[]="Hello.";

ftest(test);

}

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


What about calling outchar instead of ftest?


Great idea! Probably the OP wants to output the string character by
character, what do you think?

Chris
Jul 19 '05 #4

P: n/a
Dennis Plöger <pl*****@pharmlog.de> spoke thus:
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[i] != '\0')
{
outchar(&teststring[i]);
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 );
}

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cyberspace.org |
Jul 19 '05 #5

P: n/a
Christopher Benson-Manica <at***@nospam.cyberspace.org> spoke thus:
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...

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cyberspace.org |
Jul 19 '05 #6

P: n/a
Christopher Benson-Manica <at***@nospam.cyberspace.org> spoke thus:
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...

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cyberspace.org |
Jul 19 '05 #7

P: n/a
Dennis Plöger <pl*****@pharmlog.de> spoke thus:
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[i] != '\0')
{
outchar(&teststring[i]);
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 );
}

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cyberspace.org |
Jul 19 '05 #8

P: n/a
Christopher Benson-Manica wrote:
Christopher Benson-Manica <at***@nospam.cyberspace.org> spoke thus:

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


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

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #9

P: n/a
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
Jul 19 '05 #10

P: n/a
Dennis Plöger <pl*****@pharmlog.de> spoke thus:
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.

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cyberspace.org |
Jul 19 '05 #11

P: n/a


Dennis Plöger wrote:

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[i] != '\0' )
{
Single[0] = teststring[i];
outchar( Single );
i++;
}
}

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #12

P: n/a
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[i] != '\0' )
{
Single[0] = teststring[i];


That's it! Thank you very much.

Dennis
Jul 19 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.