pointer to string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • junky_fellow@yahoo.co.in

    #1

    pointer to string

    guys,

    I need to declare a pointer to a string. Which of the following is
    better and why?

    char *pStr;
    unsigned char *pStr;

    thanks for any help ...
  • Richard Heathfield

    #2
    Re: pointer to string

    junky_fellow@ya hoo.co.in said:
    guys,
    >
    I need to declare a pointer to a string. Which of the following is
    better and why?
    >
    char *pStr;
    unsigned char *pStr;
    A string, by definition, is "a contiguous sequence of characters terminated
    by and including the first null character".

    The Standard uses the phrase "pointer to string" to refer to a pointer to
    the first character in the string.

    Thus, we have three candidates - the two you mentioned, and signed char *:

    (a) char *p;
    (b) unsigned char *q;
    (c) signed char *r;

    Of these, (a) wins lots of points for being the form expected by strcpy,
    strlen, strchr, strstr, strpbrk, strtok, etc etc. (b) wins a few points
    for being able legally to point (after a suitable conversion) to any
    object, and so it can certainly point to a string if need be!, and (c)
    wins no points at all.

    If you're dealing with strings /as/ strings, always use (a).

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

    Comment

    • Nick Keighley

      #3
      Re: pointer to string

      On 18 Apr, 07:03, "junky_fel...@y ahoo.co.in"
      <junky_fel...@y ahoo.co.inwrote :
       I need to declare a pointer to a string. Which of the following is
      better and why?
      >
      char *pStr;
      unsigned char *pStr;
      in C a string is zero terminated array of char. So char* seems
      sensible. Since string literals are unmodifiable consider
      using const char*. If you are dealing with "bytes" (or octets)
      from a comms link or socket consider unsigned char*.

      char my_str [] = "hello";
      char *my_other_str = "there";
      char *str_ptr = my_str;
      unsigned char *packet;

      packet = read_from_sock (sock_handle);


      --
      Nick Keighley



      Comment

      • Herbert Rosenau

        #4
        Re: pointer to string

        On Fri, 18 Apr 2008 06:03:52 UTC, "junky_fellow@y ahoo.co.in"
        <junky_fellow@y ahoo.co.inwrote :
        guys,
        >
        I need to declare a pointer to a string. Which of the following is
        better and why?
        Depends what you really needs. Does it not matter if the chars in the
        string are signed or not?
        char *pStr;
        You would like to define that any char is definitely unsigned and you
        don't trust that your environment makes char definitely unsigned,
        except you tells it otherwise?
        unsigned char *pStr;
        You have to be sure that any char in the string is signed?

        signed char *pStr;

        However I use a compiler option to have any string unsigned as default
        - except I use 'signed' explicity. You will have to ask the
        documentation of your compiler for the flag to define 'char' as 'be
        always unsigned'. Typical usase of a char is unsigned for printout
        (display on screen). Typical use for using char as short short
        mathematical data type is 'signed char'. As the standard lets it
        completely to the implementation to use signed or unsigned char when
        'char' is used the behavior for 'char' is either 'signed char' or
        'unsigned char' depending on the documentation of your compiler.

        On other hand you compiler will have a flag (e.g. #pragma or a
        commandline option) to set a specific behavior of (pointer to) 'char'.
        At lest it is on you, the developer, to be sure to have a clearly
        specified behavior of (un)singned behavior for the whole program and
        use explicity 'signed' respective 'unsigned char' for cases you must
        use the contrary to the (selected) default.

        That


        --
        Tschau/Bye
        Herbert

        Visit http://www.ecomstation.de the home of german eComStation
        eComStation 1.2 Deutsch ist da!

        Comment

        • sid

          #5
          Re: pointer to string

          On Apr 18, 11:03 am, "junky_fel...@y ahoo.co.in"
          <junky_fel...@y ahoo.co.inwrote :
          guys,
          >
           I need to declare a pointer to a string. Which of the following is
          better and why?
          >
          char *pStr;
          unsigned char *pStr;
          >
          thanks for any help ...
          I am not quite sure but I would like to specify that the unsigned is
          compiler dependent and if you think that you can use all of the bits,
          the compiler may implicitly convert it into signed. So always be
          careful when you use an unsigned data type.

          Comment

          • Chris Dollin

            #6
            Re: pointer to string

            sid wrote:
            On Apr 18, 11:03 am, "junky_fel...@y ahoo.co.in"
            <junky_fel...@y ahoo.co.inwrote :
            >guys,
            >>
            > I need to declare a pointer to a string. Which of the following is
            >better and why?
            >>
            >char *pStr;
            >unsigned char *pStr;
            >>
            >thanks for any help ...
            >
            I am not quite sure but I would like to specify that the unsigned is
            compiler dependent and if you think that you can use all of the bits,
            the compiler may implicitly convert it into signed. So always be
            careful when you use an unsigned data type.
            What?

            I don't understand what you're trying to say -- can you give a more
            specific example?

            --
            "- born in the lab under strict supervision -", /Genetesis/

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

            Comment

            • kooladi

              #7
              Re: pointer to string

              On Apr 18, 11:03 am, "junky_fel...@y ahoo.co.in"
              <junky_fel...@y ahoo.co.inwrote :
              guys,
              >
              I need to declare a pointer to a string. Which of the following is
              better and why?
              >
              char *pStr;
              unsigned char *pStr;
              >
              thanks for any help ...
              Both "should" work...however. ..
              The concept of signs makes no sense to me when applied to a String. If
              you want to declare a pointer to a string, go with char* .
              If,however,you want to manipulate a decimal value and you think char(a
              signed char) is too small and Int is too big, then go for unsigned
              char.

              Besides if you do something like unsigned char* pStr = "Hi There",
              some compliers might throw an assignment type mismatch warning.


              Adi

              Comment

              • Chris Dollin

                #8
                Re: pointer to string

                kooladi wrote:
                On Apr 18, 11:03 am, "junky_fel...@y ahoo.co.in"
                <junky_fel...@y ahoo.co.inwrote :
                >guys,
                >>
                > I need to declare a pointer to a string. Which of the following is
                >better and why?
                >>
                >char *pStr;
                >unsigned char *pStr;
                >>
                >thanks for any help ...
                >
                Both "should" work...however. ..
                The concept of signs makes no sense to me when applied to a String.
                That's OK, because its not the string that's signed; it's the
                component characters.
                If
                you want to declare a pointer to a string, go with char* .
                Because that's what the C string operations expect.
                If,however,you want to manipulate a decimal value and you think char(a
                signed char) is too small and Int is too big, then go for unsigned
                char.
                What's "decimal" got to do with it? How would you represent -1 as
                an unsigned char?

                Don't forget the flavours of `short`.
                Besides if you do something like unsigned char* pStr = "Hi There",
                some compliers might throw an assignment type mismatch warning.
                Shouldn't they /all/?

                --
                "It was the first really clever thing the King had /Alice in Wonderland/
                said that day."

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

                Comment

                • Keith Thompson

                  #9
                  Re: pointer to string

                  Nick Keighley <nick_keighley_ nospam@hotmail. comwrites:
                  On 18 Apr, 07:03, "junky_fel...@y ahoo.co.in"
                  <junky_fel...@y ahoo.co.inwrote :
                  > I need to declare a pointer to a string. Which of the following is
                  >better and why?
                  >>
                  >char *pStr;
                  >unsigned char *pStr;
                  >
                  in C a string is zero terminated array of char. So char* seems
                  sensible. Since string literals are unmodifiable consider
                  using const char*. If you are dealing with "bytes" (or octets)
                  from a comms link or socket consider unsigned char*.
                  [...]

                  If you're dealing with sequences of octets, they're probably not
                  terminated by a '\0', and therefore they don't constitute a string.

                  Of course you *might* happen to have an externally generated data
                  format consisting of a sequence of unsigned bytes terminated by a zero
                  byte, but such things aren't very common as far as I know; the size of
                  such a format is usually determined in some other way, particularly if
                  it needs to be able to hold arbitrary data. (And of course plain char
                  might be unsigned, but that's beside the point.)

                  --
                  Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
                  Nokia
                  "We must do something. This is something. Therefore, we must do this."
                  -- Antony Jay and Jonathan Lynn, "Yes Minister"

                  Comment

                  • Keith Thompson

                    #10
                    Re: pointer to string

                    Chris Dollin <chris.dollin@h p.comwrites:
                    kooladi wrote:
                    [...]
                    >If,however,y ou want to manipulate a decimal value and you think char(a
                    >signed char) is too small and Int is too big, then go for unsigned
                    >char.
                    >
                    What's "decimal" got to do with it? How would you represent -1 as
                    an unsigned char?
                    <correct_but_no t_helpfulUCHAR_ MAX </correct_but_not _helpful>

                    --
                    Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
                    Nokia
                    "We must do something. This is something. Therefore, we must do this."
                    -- Antony Jay and Jonathan Lynn, "Yes Minister"

                    Comment

                    Working...