char*

M

mlt

I have a function that returns a char*. But how do I print the whole string
that the char* points to? I don't know at forehand how long the string is.
 
V

vippstar

If it's a proper string, terminated with a null character, then you can
just use printf("%d\n", string). If it isn't null-terminated, and you
don't know the length of it, you need to rethink your design.

It should be s, not d. "%s\n"
Typo probably.
 
R

Richard Tobin

mlt said:
I have a function that returns a char*. But how do I print the whole string
that the char* points to? I don't know at forehand how long the string is.

If it's a proper string, terminated with a null character, then you can
just use printf("%d\n", string). If it isn't null-terminated, and you
don't know the length of it, you need to rethink your design.

-- Richard
 
R

Richard Bos

mlt said:
I have a function that returns a char*. But how do I print the whole string
that the char* points to? I don't know at forehand how long the string is.

If it's a string (that is, an array of chars _terminated by a '\0'_),
just pass it to any string printing function. They use the '\0' to
determine when to stop.
If it's any array of chars, not necessarily with a '\0' at the end, then
it's not a C string, and you will have to get whatever function passed
you that pointer to also pass you the size.

Richard
 
R

Richard Tobin

If it's a proper string, terminated with a null character, then you can
just use printf("%d\n", string). If it isn't null-terminated, and you
don't know the length of it, you need to rethink your design.
[/QUOTE]
It should be s, not d. "%s\n"
Typo probably.

Oops, yes, sorry!

-- Richard
 
C

CBFalconer

mlt said:
I have a function that returns a char*. But how do I print the
whole string that the char* points to? I don't know at forehand
how long the string is.

Try this out:

#include <stdio.h>
char *s;
...
s = yourfunction(whatever);
puts(s);
... /* s still points to the string if needed */
 
M

Martin Ambuhl

mlt said:
I have a function that returns a char*. But how do I print the whole
string that the char* points to? I don't know at forehand how long the
string is.

You don't need to know:

#include <stdio.h>

char *function_that_returns_pointer_to_char(void)
{
static char s[] = "This is the string pointed to.";
return s;
}

int main(void)
{
char *t;
printf("[output]\n"
"I am about to call a function that returns a char *\n");
t = function_that_returns_pointer_to_char();
printf("I'm back. The function return a pointer to this:\n"
"\"%s\".\n" "(Without the quotes, of course.)\n", t);
return 0;
}


[output]
I am about to call a function that returns a char *
I'm back. The function return a pointer to this:
"This is the string pointed to.".
(Without the quotes, of course.)
 
M

Martin Ambuhl

Richard said:
If it's a proper string, terminated with a null character, then you can
just use printf("%d\n", string).

He means "%s\n", of course. This comes from having 's' next to 'd' on
many keyboards.
 
R

Richard Nixon

mlt said:
I have a function that returns a char*. But how do I print the whole
string that the char* points to? I don't know at forehand how long the
string is.

You don't need to know:

#include <stdio.h>

char *function_that_returns_pointer_to_char(void)
{
static char s[] = "This is the string pointed to.";
return s;
}

int main(void)
{
char *t;
printf("[output]\n"
"I am about to call a function that returns a char *\n");
t = function_that_returns_pointer_to_char();
printf("I'm back. The function return a pointer to this:\n"
"\"%s\".\n" "(Without the quotes, of course.)\n", t);
return 0;
}


[output]
I am about to call a function that returns a char *
I'm back. The function return a pointer to this:
"This is the string pointed to.".
(Without the quotes, of course.)

I really enjoy posts like this you can snip wholesale and compare your
results and look at the finer points of syntax, as with adding the quotes
in the printf.


F:\gfortran\source>gcc -o martin martin1.c

F:\gfortran\source>martin
[output]
I am about to call a function that returns a char *
I'm back. The function return a pointer to this:
"This is the string pointed to.".
(Without the quotes, of course.)

F:\gfortran\source>
--
Richard Milhous Nixon

A good memory and a tongue tied in the middle is a combination which gives
immortality to conversation.
~~ Mark Twain
 
P

Pranav

how do I print the whole string that the char* points to?String is an array of NULL terminated characters, So you need not
worry about its length while printing. C's string printing features
will take care of that by just printing things until it encounters a
NULL character.
 
C

Chris Dollin

Pranav said:
String is an array of NULL terminated characters, So you need not
worry about its length while printing. C's string printing features
will take care of that by just printing things until it encounters a
NULL character.

Nitpick: NUL, not NULL. (In C `NULL` refers to a macro used to denote
the null pointer constant. While it may /happen/ that `char c = NULL;`
does The Expected Thing, it induces confusion.)
 
R

Richard Nixon

Nitpick: NUL, not NULL. (In C `NULL` refers to a macro used to denote
the null pointer constant. While it may /happen/ that `char c = NULL;`
does The Expected Thing, it induces confusion.)

This happens to be a topic I discussed in yahoo chat, back when there was
intellible life there.

I usually find definitions for macros by brute force, but I don't have that
option now. I would guess that it's defined in stdlib.h?

What does a person do if he's trying to find a definition for a macro
without many of the usual tools?
--
Richard Milhous Nixon

Always acknowledge a fault frankly. This will throw those in authority off
guard and allow you opportunity to commit more.
~~ Mark Twain
 
J

jameskuyper

Richard said:
This happens to be a topic I discussed in yahoo chat, back when there was
intellible life there.

I usually find definitions for macros by brute force, but I don't have that
option now. I would guess that it's defined in stdlib.h?

What does a person do if he's trying to find a definition for a macro
without many of the usual tools?

You can find all the information that you'll normally need to know
about standard macros like NULL by reading any good textbook for the
language, such as "The C Programming Language", Kernighan & Ritchie,
2nd edition. You could also read the standard, but it's written as a
requirements specification; it's not well written for use as textbook.
For non-standard macros, read the documentation for the package that
defines those macros.

However, if you're using multiple non-standard headers, you'll need to
figure out which one was the one that defined the macro. In that case,
the answer to your question depends upon which things you consider to
be "the usual tools", and which of those tools is actually unavailable
- you need to explain what you mean by that in more detail. Every
good answer I can think of to your question relies upon at least one
thing that I would consider to be one of "the usual tools".
 
R

Richard Nixon

[reordered, for thematic reasons]
However, if you're using multiple non-standard headers, you'll need to
figure out which one was the one that defined the macro. In that case,
the answer to your question depends upon which things you consider to
be "the usual tools", and which of those tools is actually unavailable
- you need to explain what you mean by that in more detail. Every
good answer I can think of to your question relies upon at least one
thing that I would consider to be one of "the usual tools".

I find the index for my german printing of K&R2 better than its english
counterpart. For starters, the german version goes all the way to Z, while
my english is starting to show signs of a hard life that includes me
throwing it like a frisbee when it reaches the breaking point. The back
cover became a letter to Julianna.

I think the german version is especially telling here, as the only entry
is:

NULL, Test weglassen 55, 102

s. 99: Die symbolische konstante NULL wird oft statt Null als
Gedächtnisstütze benutzt, um hervorzuheben, daß dies ein spezieller Wert
für einen Zeiger ist. NULL ist is in said:
You can find all the information that you'll normally need to know
about standard macros like NULL by reading any good textbook for the
language, such as "The C Programming Language", Kernighan & Ritchie,
2nd edition. You could also read the standard, but it's written as a
requirements specification; it's not well written for use as textbook.
For non-standard macros, read the documentation for the package that
defines those macros.

That sounds like catechism.

I have a correction for the german Ausgabe, und ich werde mich weiterhin
verdeutchern. Man sieht dabei die Ursache des Fehlers. Die deutsche
Ausgabe hat ein Referenz auf " if zero " und dabei " the short circuit."

Richtig is nicht 102, welch im §5.4 Ami_version steckt, sondern 99.

99 Duesenjaeger, die waren gute Krieger
Hielten sich fuer Captain Kirk

, aber, ich denke an Euch,
und lass 'nen fliegen.
 
J

James Kuyper

Richard said:
That sounds like catechism.

I can't imagine why. I said nothing to suggest a religious attitude
toward either the texts or the authors I described. They are human text
written by human authors; one of them by a committee - with all the
potential for error that implies.
I have a correction for the german Ausgabe, ...

I studied German for several years two decades ago, but I've never used
that knowledge very much. If it's too difficult for you to translate
the German text you quoted into English, it's even harder for me. I
don't trust the quality of my translation, and am therefore unwilling to
comment on it. I suspect that many (most?) of the people monitoring this
group have even less idea than I do about what you're trying to say.
 
N

Nick Keighley

I find the index for my german printing of K&R2 better than its english
counterpart.  For starters, the german version goes all the way to Z, while
my english is starting to show signs of a hard life that includes me
throwing it like a frisbee when it reaches the breaking point.  The back
cover became a letter to Julianna.

don't post when drunk
 

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
474,078
Messages
2,570,572
Members
47,204
Latest member
MalorieSte

Latest Threads

Top