char*

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mlt

    char*

    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.

  • Richard Tobin

    #2
    Re: char*

    In article <48f36390$0$902 66$14726298@new s.sunsite.dk>,
    mlt <asdf@asd.comwr ote:
    >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.

    Comment

    • vippstar@gmail.com

      #3
      Re: char*

      On Oct 13, 6:27 pm, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
      In article <48f36390$0$902 66$14726...@new s.sunsite.dk>,
      >
      mlt <a...@asd.comwr ote:
      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.

      Comment

      • Richard Bos

        #4
        Re: char*

        "mlt" <asdf@asd.comwr ote:
        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

        Comment

        • Richard Tobin

          #5
          Re: char*

          In article <ca35c467-7410-44de-9fc8-9e079fcc1379@u4 6g2000hsc.googl egroups.com>,
          <vippstar@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.

          Comment

          • CBFalconer

            #6
            Re: char*

            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(wh atever);
            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.

            Comment

            • Martin Ambuhl

              #7
              Re: char*

              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_r eturns_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.)

              Comment

              • Martin Ambuhl

                #8
                Re: char*

                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.

                Comment

                • Richard Nixon

                  #9
                  Re: char*

                  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_r eturns_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\sou rce>gcc -o martin martin1.c

                  F:\gfortran\sou rce>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\sou rce>
                  --
                  Richard Milhous Nixon

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

                  Comment

                  • Pranav

                    #10
                    Re: char*

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

                    Comment

                    • Chris Dollin

                      #11
                      Re: char*

                      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

                      Comment

                      • Richard Nixon

                        #12
                        Re: char*

                        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

                        Comment

                        • jameskuyper@verizon.net

                          #13
                          Re: char*

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

                          Comment

                          • pete

                            #14
                            Re: char*

                            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

                            Comment

                            • Richard Nixon

                              #15
                              Re: char*

                              On Tue, 14 Oct 2008 15:39:41 -0700 (PDT), jameskuyper@ver izon.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ütz e benutzt, um hervorzuheben, daß dies ein spezieller Wert
                              für einen Zeiger ist. NULL ist is in <stdio.hdefinie rt.
                              >
                              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

                              Comment

                              Working...