functions returning *char

R

Rick

Hi,

I was wondering, can it be safely assumed that any function that returns
a *char will return a NULL terminated string? Or does the function
explicitly mention this always (and the ones that don't do not return
null terminated strings?)

Rick
 
J

Joona I Palaste

Rick said:
I was wondering, can it be safely assumed that any function that returns
a *char will return a NULL terminated string? Or does the function
explicitly mention this always (and the ones that don't do not return
null terminated strings?)

No, from merely seeing that a function returns a char*, you can't say
anything whether it will return a null-terminated string. It might, or
it might not.
 
M

Mark A. Odell

Rick said:
I was wondering, can it be safely assumed that any function that returns
a *char will return a NULL terminated string? Or does the function
explicitly mention this always (and the ones that don't do not return
null terminated strings?)

No, I would not assume char *func() returns a C string (null terminated).
What if you have a block of memory you want to return a pointer to? You
need to read the function description to know if it returns a null
terminated string.
 
T

Tristan Miller

Greetings.

I was wondering, can it be safely assumed that any function that returns
a *char will return a NULL terminated string? Or does the function
explicitly mention this always (and the ones that don't do not return
null terminated strings?)

Nope. A function returning a *char might not even return something pointing
to a string at all (i.e., when it returns the pointer value NULL). Always
read the function's documentation (or the code itself, if there is no
documentation) to determine what conclusions can be drawn regarding the
return value.

Note that you appear to be confusing some terminology with respect to
strings and pointers -- NULL (in capital letters) refers to a specific
pointer value denoting that the pointer does not point to any memory in
particular. On the other hand, the "null" in the phrase "null-terminated
string" refers to the character '\0' (named NUL in ASCII, though of course
you might not be using an ASCII system), which is used in C as an
end-of-string marker. The character '\0' and the pointer value NULL might
technically have the same integral value from a human's point of view, but
as they are of different types they cannot be considered equal or used
(either in code or in natural language) interchangeably.

Regards,
Tristan
 
D

Dan Pop

In said:
I was wondering, can it be safely assumed that any function that returns
a *char will return a NULL terminated string?

There is no such thing as a NULL terminated string. There is no
connection whatsoever between the null character and the NULL macro.
Furthermore, all C strings are null terminated, by definition. But a
char pointer need not point to a string.
Or does the function
explicitly mention this always (and the ones that don't do not return
null terminated strings?)

The function specification must explicitly mention it. Even strncpy may
not return a pointer to a string (if there was not enough space for the
null character). AFAICT, this is the only example from the standard C
library.

Dan
 
D

Dan Pop

In said:
No, I would not assume char *func() returns a C string (null terminated).
What if you have a block of memory you want to return a pointer to?

Well, malloc and friends return a void pointer...
You need to read the function description to know if it returns a null
terminated string.

Correct.

Dan
 
I

Irrwahn Grausewitz

Ron Croonenberg said:
Nope, it returns the pointer to a string...that's it.

No, it returns a pointer to a character, which may or may not happen to
be the first character in a null-terminated string. /That's/ it. ;-)

BTW, please don't top-post, thank you.

Regards
 
K

Keith Thompson

Rick said:
I was wondering, can it be safely assumed that any function that
returns a *char will return a NULL terminated string? Or does the
function explicitly mention this always (and the ones that don't do
not return null terminated strings?)

A function that returns a *char is probably the result of a typo, and
is unlikely to return anything.

A function that returns a char*, on the other hand, may or may not
return a pointer to a nul-terminated string. If you're lucky, the
function's documentation will tell you (and if you haven't read the
documentation, why are you calling it in the first place?).

If your question is limited to functions in the C standard library,
the standard or any decent C reference book should tell you what the
function is expected to return.
 
P

pete

Dan said:
There is no such thing as a NULL terminated string. There is no
connection whatsoever between the null character and the NULL macro.
Furthermore, all C strings are null terminated, by definition. But a
char pointer need not point to a string.


The function specification must explicitly mention it.
Even strncpy may not return a pointer to a string
(if there was not enough space for the null character).
AFAICT, this is the only example from the standard C
library.

strstr, strtok, strchr, strrchr, and strpbrk
can all return either a null pointer or a pointer to a string.
 
P

Peter Nilsson

Irrwahn Grausewitz said:
No, it returns a pointer to a character, which may or may not happen to
be the first character in a null-terminated string. /That's/ it. ;-)

Under C99, a non-null char * points an array of indeterminate number
of chars as a pointer to a single object can be treated as an array of
1 such object.

[BTW, strings are null-terminated by definition.]
 
I

Irrwahn Grausewitz

Under C99, a non-null char * points an array of indeterminate number
of chars as a pointer to a single object can be treated as an array of
1 such object.

Which doesn't make it a string, that's the point.
[BTW, strings are null-terminated by definition.]

Right, but to avoid possible confusion or misunderstandings I prefer the
term "null-terminated string" when talking about C strings.

Well, of course I could say: "array of indeterminate number of
characters holding a character sequence terminated by a null character",
but that's really bulky.

Regards
 
I

Irrwahn Grausewitz

pete said:
strstr, strtok, strchr, strrchr, and strpbrk
can all return either a null pointer or a pointer to a string.

Right, but AFAICT Dan's concern was about functions that return a
valid pointer to a character that is not part of a null-terminated
string.

Regards
 
I

Irrwahn Grausewitz

Irrwahn Grausewitz said:
(e-mail address removed) (Peter Nilsson) wrote:
[BTW, strings are null-terminated by definition.]

Right, but to avoid possible confusion or misunderstandings I prefer the
term "null-terminated string" when talking about C strings.

Well, of course I could say: "array of indeterminate number of
characters holding a character sequence terminated by a null character",
but that's really bulky.

More precisely I could call it: "A contiguous sequence of characters
terminated by and including the first null character."

or even: "Thing defined in ISO/IEC 9899:1999 7.1.1#1 first sentence".

;-)

Regards
 
D

Dan Pop

In said:
strstr, strtok, strchr, strrchr, and strpbrk
can all return either a null pointer or a pointer to a string.

But whenever they return a valid pointer value, it's a pointer to a
string. That was my point.

Dan
 
D

Dan Pop

In said:
Irrwahn Grausewitz said:
(e-mail address removed) (Peter Nilsson) wrote:
[BTW, strings are null-terminated by definition.]

Right, but to avoid possible confusion or misunderstandings I prefer the
term "null-terminated string" when talking about C strings.

Well, of course I could say: "array of indeterminate number of
characters holding a character sequence terminated by a null character",
but that's really bulky.

More precisely I could call it: "A contiguous sequence of characters
terminated by and including the first null character."

or even: "Thing defined in ISO/IEC 9899:1999 7.1.1#1 first sentence".

;-)

Which is... "string" ;-) So, there is no risk of confusion or
misundestandings: anyone who doesn't get it is too confused for this
newsgroup, anyway ;-)

In a looser context, you can say "C string" to avoid any confusion.

Dan
 
I

Irrwahn Grausewitz

Which is... "string" ;-) So, there is no risk of confusion or
misundestandings: anyone who doesn't get it is too confused for this
newsgroup, anyway ;-)

You're probably right on this. :)
In a looser context, you can say "C string" to avoid any confusion.

Which seems to have the best accuracy/brevity ratio of all.

BTW, I have to tune my guitar's D string; it sounds horrible. ;-)
 
C

Christopher Benson-Manica

Irrwahn Grausewitz said:
BTW, I have to tune my guitar's D string; it sounds horrible. ;-)

Why not just replace it with a B string? Well, wait, I have no idea whether
strings even existed in B ;) I'm sure a C++ string would be too expensive,
and a C# string would probably sound just as bad, so maybe you should just
stick with your D string since it has character(s).
 
I

Irrwahn Grausewitz

Christopher Benson-Manica said:
Why not just replace it with a B string? Well, wait, I have no idea whether
strings even existed in B ;) I'm sure a C++ string would be too expensive,
and a C# string would probably sound just as bad, so maybe you should just
stick with your D string since it has character(s).

And it doesn't change the overall tuning (C# would be a disaster).
 

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,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top