473,320 Members | 1,870 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,320 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 2151
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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.