simple pointers question

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

    #16
    Re: simple pointers question

    Pietro Cerutti said:
    Richard Heathfield wrote:
    >Pietro Cerutti said:
    >>>>
    ><snip>
    >>
    >>>But I still dont get it. It seems this function is for comparing
    >>>the size of strings/arrays.
    >>From the man pages:
    >>
    >The man pages are not normative, and in this case they were
    >misleading.
    >>
    ><snip>
    >>
    >>>How can i use this to initialize 'samples' from what the 'temp'
    >>>pointer is poiting to?
    >>>>
    >>using strncpy..
    >>
    >Wrong.
    >
    It depends on how you interpret the sencente:
    No, it doesn't. The OP's requirements are not yet clear, but there are
    lots of things I can identify out of hand as being *not* what he needs,
    and this list certainly includes a katharometer, a half-submerged
    patamar, a relief map of the Metropolitan District of South Humberside,
    a light green balmacaan with extra pockets, and strncpy.

    <snip>

    --
    Richard Heathfield <http://www.cpax.org.uk >
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

    Comment

    • ryanselk@gmail.com

      #17
      Re: simple pointers question

      On Jun 25, 12:00 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
      Pietro Cerutti said:
      >
      >
      >
      Richard Heathfield wrote:
      Pietro Cerutti said:
      >
      >ryans...@gmail .com wrote:
      >
      <snip>
      >
      >>But I still dont get it. It seems this function is for comparing
      >>the size of strings/arrays.
      >From the man pages:
      >
      The man pages are not normative, and in this case they were
      misleading.
      >
      <snip>
      >
      >>How can i use this to initialize 'samples' from what the 'temp'
      >>pointer is poiting to?
      >
      >using strncpy..
      >
      Wrong.
      >
      It depends on how you interpret the sencente:
      >
      No, it doesn't. The OP's requirements are not yet clear, but there are
      lots of things I can identify out of hand as being *not* what he needs,
      and this list certainly includes a katharometer, a half-submerged
      patamar, a relief map of the Metropolitan District of South Humberside,
      a light green balmacaan with extra pockets, and strncpy.
      >
      <snip>
      >
      --
      Richard Heathfield <http://www.cpax.org.uk >
      Email: -www. +rjh@
      Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
      "Usenet is a strange place" - dmr 29 July 1999

      Im sorry guys I was not clean and now I realize it.
      I now see what you guys where talking about, and you are correct that
      I was way off.


      I have:

      char *temp;
      //Lets say that this is pointing to the word "Samples"

      char *temp2
      //Lets say that this is pointing to the number '32'

      int samples;
      //null

      I need a statement that does this:

      If (temp1 is equal to the word 'samples')
      set samples to the value of temp2 (32).


      So far I got (with your help)


      if (strcmp(temp, "samples") == 0) //if temp == samples
      {
      //makes samples = temp2

      }

      but im unsure of how to get the middle statement. Thanks again guys I
      hope this is clearer. I reduced my code down to what i thought was
      minimal but looking back it wasn't enough code to work off of
      initially. Thanks!

      Comment

      • Default User

        #18
        Re: simple pointers question

        ryanselk@gmail. com wrote:
        On Jun 25, 12:00 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
        --
        Richard Heathfield <http://www.cpax.org.uk >
        Email: -www. +rjh@
        Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
        "Usenet is a strange place" - dmr 29 July 1999
        PLEASE TRIM SIGNATURES.
        I have:
        >
        char *temp;
        //Lets say that this is pointing to the word "Samples"
        >
        char *temp2
        //Lets say that this is pointing to the number '32'
        Why do you have string? Why not an integer in the first place? Is this
        user input or something?
        int samples;
        //null
        >
        I need a statement that does this:
        >
        If (temp1 is equal to the word 'samples')
        set samples to the value of temp2 (32).
        >
        >
        So far I got (with your help)
        >
        >
        if (strcmp(temp, "samples") == 0) //if temp == samples
        {
        //makes samples = temp2
        >
        }
        Please fix your indents. Your code is hard to read.
        but im unsure of how to get the middle statement. Thanks again guys I
        hope this is clearer. I reduced my code down to what i thought was
        minimal but looking back it wasn't enough code to work off of
        initially. Thanks!
        The easiest way, but arguably the worst, is to use the atoi() function.
        That is not a robust function, there's no way to tell whether a string
        contained "0" or if there was an error. You also can't tell if there
        was a partial conversion ("123skiddoo ").

        Better, but more trouble, is strtol(). You can read up on it here:

        <http://www.mkssoftware .com/docs/man3/strtol.3.asp>

        You'll need to pass an endptr address, check that, then check errno.
        That's some things for you to look at. Let us know how it's going.



        Brian

        Comment

        • ryanselk@gmail.com

          #19
          Re: simple pointers question

          On Jun 25, 12:29 pm, "Default User" <defaultuse...@ yahoo.comwrote:
          ryans...@gmail. com wrote:
          On Jun 25, 12:00 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
          --
          Richard Heathfield <http://www.cpax.org.uk >
          Email: -www. +rjh@
          Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
          "Usenet is a strange place" - dmr 29 July 1999
          >
          PLEASE TRIM SIGNATURES.
          >
          I have:
          >
          char *temp;
          //Lets say that this is pointing to the word "Samples"
          >
          char *temp2
          //Lets say that this is pointing to the number '32'
          >
          Why do you have string? Why not an integer in the first place? Is this
          user input or something?
          >
          >
          >
          int samples;
          //null
          >
          I need a statement that does this:
          >
          If (temp1 is equal to the word 'samples')
          set samples to the value of temp2 (32).
          >
          So far I got (with your help)
          >
          if (strcmp(temp, "samples") == 0) //if temp == samples
          {
          //makes samples = temp2
          >
          }
          >
          Please fix your indents. Your code is hard to read.
          >
          but im unsure of how to get the middle statement. Thanks again guys I
          hope this is clearer. I reduced my code down to what i thought was
          minimal but looking back it wasn't enough code to work off of
          initially. Thanks!
          >
          The easiest way, but arguably the worst, is to use the atoi() function.
          That is not a robust function, there's no way to tell whether a string
          contained "0" or if there was an error. You also can't tell if there
          was a partial conversion ("123skiddoo ").
          >
          Better, but more trouble, is strtol(). You can read up on it here:
          >
          <http://www.mkssoftware .com/docs/man3/strtol.3.asp>
          >
          You'll need to pass an endptr address, check that, then check errno.
          That's some things for you to look at. Let us know how it's going.
          >
          Brian
          Thanks guys! I got it working fine using the atoi function,

          But I will be looking into the strtol one as well as I suppose a
          partial conversion is likely with my program. (its a program that
          splits strings and reads certain parts of a big header file, kind of a
          pain in the ass haha).

          Comment

          • Richard Heathfield

            #20
            Re: simple pointers question

            Pietro Cerutti said:

            <snip>
            If temp2 points to an array of chars where the first is '3', the
            second is '2' and the third is '\0', then the function atoi() from
            stdlib.h may help.
            >
            samples = atoi(temp2);
            Whilst that's fine provided temp2 does indeed point to "32", it is
            nevertheless a bad idea, in general, to advocate atoi, because its
            behaviour on overflow is undefined. It would be far better to use
            strtol.

            --
            Richard Heathfield <http://www.cpax.org.uk >
            Email: -www. +rjh@
            Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
            "Usenet is a strange place" - dmr 29 July 1999

            Comment

            • Pietro Cerutti

              #21
              Re: simple pointers question

              Richard Heathfield wrote:
              Pietro Cerutti said:
              >
              <snip>
              >
              >If temp2 points to an array of chars where the first is '3', the
              >second is '2' and the third is '\0', then the function atoi() from
              >stdlib.h may help.
              >>
              >samples = atoi(temp2);
              >
              Whilst that's fine provided temp2 does indeed point to "32", it is
              nevertheless a bad idea, in general, to advocate atoi, because its
              behaviour on overflow is undefined. It would be far better to use
              strtol.
              >
              Thanx for pointing it out.

              --
              Pietro Cerutti

              PGP Public Key:

              Comment

              • Keith Thompson

                #22
                Re: simple pointers question

                ryanselk@gmail. com writes:
                [...]
                I have:
                >
                char *temp;
                //Lets say that this is pointing to the word "Samples"
                >
                char *temp2
                //Lets say that this is pointing to the number '32'
                >
                int samples;
                //null
                >
                I need a statement that does this:
                >
                If (temp1 is equal to the word 'samples')
                set samples to the value of temp2 (32).
                >
                >
                So far I got (with your help)
                >
                >
                if (strcmp(temp, "samples") == 0) //if temp == samples
                {
                //makes samples = temp2
                >
                }
                >
                but im unsure of how to get the middle statement. Thanks again guys I
                hope this is clearer. I reduced my code down to what i thought was
                minimal but looking back it wasn't enough code to work off of
                initially. Thanks!
                One more thing I forgot to mention. I see that you have a variable
                named samples, and a string with the value "samples". You should be
                aware that there's no way to get from one to the other, unless you
                explicitly code it yourself; variable names exist only in your C
                source file, not in the program itself. I think you already know, but
                I thought I should mention it.

                --
                Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                "We must do something. This is something. Therefore, we must do this."
                -- Antony Jay and Jonathan Lynn, "Yes Minister"

                Comment

                • tom lewton

                  #23
                  Re: simple pointers question

                  On 25 Jun 2007 at 17:49, Richard Heathfield wrote:
                  >What's wrong with
                  >>
                  >strncmp(temp , "samples", sizeof("samples "));
                  >
                  Nothing, particularly, except that it is identical in meaning to
                  strcmp(temp, "samples") - am I to take it that you enjoy typing?
                  you seem to enjoy typing yourself, if the code you post here is anything
                  to go on: frequent verbosity along the lines of

                  if(p!=NULL) {
                  singlestatement ();
                  }

                  instead of

                  if(p)
                  singlestatement ();

                  Comment

                  • Richard Heathfield

                    #24
                    Re: simple pointers question

                    tom lewton said:
                    On 25 Jun 2007 at 17:49, Richard Heathfield wrote:
                    >>What's wrong with
                    >>>
                    >>strncmp(tem p, "samples", sizeof("samples "));
                    >>
                    >Nothing, particularly, except that it is identical in meaning to
                    >strcmp(temp, "samples") - am I to take it that you enjoy typing?
                    >
                    you seem to enjoy typing yourself, if the code you post here is
                    anything to go on: frequent verbosity along the lines of
                    >
                    if(p!=NULL) {
                    singlestatement ();
                    }
                    >
                    instead of
                    >
                    if(p)
                    singlestatement ();
                    Yes, it does seem like that, doesn't it? But in my defence I offer the
                    following:

                    1) I reserve my use of if(foo) to situations where foo is clearly
                    supposed to represent a Boolean concept - for example, I'll happily
                    write if(isprint((uns igned char)ch) and be glad of the saving. But
                    since pointers are not an on/off thing, I prefer not to pretend they
                    are, when using "if". I do this for readability reasons; I do find that
                    it makes the code quicker to read and understand.

                    2) It is not uncommon for people to maintain this:

                    if(condition)
                    singlestatement ();

                    into this:

                    if(condition)
                    singlestatement ();
                    otherstatement( );

                    when what they intended was:

                    if(condition)
                    {
                    singlestatement ();
                    otherstatement( );
                    }

                    I have found it easier to put the braces in right at the start, as it
                    saves me debugging time later.

                    So whilst I agree that my style does cost extra typing time, I am
                    content that it saves me time overall.

                    In the OP's case, however, we had a construct that was in fact
                    equivalent to a strcmp, but this was not self-evident - it took longer
                    to write *and* longer to read, and I suspect it would take longer to
                    maintain, too.

                    --
                    Richard Heathfield <http://www.cpax.org.uk >
                    Email: -www. +rjh@
                    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                    "Usenet is a strange place" - dmr 29 July 1999

                    Comment

                    Working...