473,388 Members | 1,385 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

char*

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.

Oct 13 '08 #1
17 2154
In article <48***********************@news.sunsite.dk>,
mlt <as**@asd.comwrote:
>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

--
Please remember to mention me / in tapes you leave behind.
Oct 13 '08 #2
On Oct 13, 6:27 pm, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <48f36390$0$90266$14726...@news.sunsite.dk>,

mlt <a...@asd.comwrote:
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.
It should be s, not d. "%s\n"
Typo probably.
Oct 13 '08 #3
"mlt" <as**@asd.comwrote:
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
Oct 13 '08 #4
In article <ca**********************************@u46g2000hsc. googlegroups.com>,
<vi******@gmail.comwrote:
>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.
Oops, yes, sorry!

-- Richard

--
Please remember to mention me / in tapes you leave behind.
Oct 13 '08 #5
mlt wrote:
>
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 */

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Oct 13 '08 #6
mlt wrote:
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.)
Oct 13 '08 #7
Richard Tobin wrote:
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.

Oct 13 '08 #8
On Mon, 13 Oct 2008 17:39:42 -0400, Martin Ambuhl wrote:
mlt wrote:
>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
Oct 14 '08 #9
>how do I print the whole string that the char* points to?
>I don't know at forehand how long the string is.
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.

Oct 14 '08 #10
Pranav wrote:
>>how do I print the whole string that the char* points to?
I don't know at forehand how long the string is.
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.)

--
'It changed the future .. and it changed us.' /Babylon 5/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Oct 14 '08 #11
On Tue, 14 Oct 2008 08:06:22 +0100, Chris Dollin wrote:
Pranav wrote:
>>>how do I print the whole string that the char* points to?
I don't know at forehand how long the string is.
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.)
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
Oct 14 '08 #12
Richard Nixon wrote:
On Tue, 14 Oct 2008 08:06:22 +0100, Chris Dollin wrote:
....
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?
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".
Oct 14 '08 #13
Chris Dollin wrote:
Pranav wrote:
>>>how do I print the whole string that the char* points to?
I don't know at forehand how long the string is.
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.)
In C, a string is terminated by a "null character".

--
pete
Oct 14 '08 #14
On Tue, 14 Oct 2008 15:39:41 -0700 (PDT), ja*********@verizon.net wrote:
Richard Nixon wrote:
>On Tue, 14 Oct 2008 08:06:22 +0100, Chris Dollin wrote:
...
>>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?
[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 <stdio.hdefiniert.
>
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.
--
Richard Milhous Nixon

All humor is derrived from pain, ergo nothing in Heaven is funny.
~~ Mark Twain
Oct 15 '08 #15
Richard Nixon wrote:
On Tue, 14 Oct 2008 15:39:41 -0700 (PDT), ja*********@verizon.net wrote:
....
>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 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.
Oct 15 '08 #16
James Kuyper said:
Richard Nixon wrote:
<snip>
>
>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.
It's a cinch for the fish:

"Expenditure, and I become further verdeutchern. One sees thereby the cause
of the error. The German Expenditure " reference on; if zero " and " the
short circuit." Correctly is not 102, what in the §5.4 Ami_version is, but
99. 99 jet military planes, those were good warriors Held themselves for
Captain Kirk , but, I think of you, and leave ' nen fly."

Even allowing for a certain amount of mis-translation, I think it's pretty
clear that the text adds no value to this discussion. He's just trying to
yank your chain (and failing).

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 15 '08 #17
On 15 Oct, 01:55, Richard Nixon <rich...@example.invalidwrote:
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
Oct 15 '08 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Christopher Benson-Manica | last post by:
I need a smart char * class, that acts like a char * in all cases, but lets you do some std::string-type stuff with it. (Please don't say to use std::string - it's not an option...). This is my...
5
by: Alex Vinokur | last post by:
"Richard Bos" <rlb@hoekstra-uitgeverij.nl> wrote in message news:4180f756.197032434@news.individual.net... to news:comp.lang.c > ben19777@hotmail.com (Ben) wrote: > > 2) Structure casted into an...
5
by: Sona | last post by:
I understand the problem I'm having but am not sure how to fix it. My code passes two char* to a function which reads in some strings from a file and copies the contents into the two char*s. Now...
2
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
12
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
4
by: Paul Brettschneider | last post by:
Hello all, consider the following code: typedef char T; class test { T *data; public: void f(T, T, T); void f2(T, T, T);
16
by: s0suk3 | last post by:
This code #include <stdio.h> int main(void) { int hello = {'h', 'e', 'l', 'l', 'o'}; char *p = (void *) hello; for (size_t i = 0; i < sizeof(hello); ++i) {
29
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.