strtoint

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

    #1

    strtoint

    does this function exist in c? on any equivalent? that convert string to
    integer?

    how do I convert a array to a "string"
    example
    char arr['a','b','c'];
    char *arr2;

    how can I make arr2 = arr ?



  • Emmanuel Delahaye

    #2
    Re: strtoint

    Profetas wrote on 25/07/04 :[color=blue]
    > does this function exist in c? on any equivalent? that convert string to
    > integer?
    >
    > how do I convert a array to a "string"
    > example
    > char arr['a','b','c'];[/color]

    This is not C.

    char arr[] = {'a','b','c'};

    But it's not a string.
    [color=blue]
    > char *arr2;
    >
    > how can I make arr2 = arr ?[/color]

    arr2 is just a pointer. You are allowed to make it point to an array f
    char, but it doesn't make it a string.

    Bare in mind that in C, '=' is the affectation operator, and that '=='
    is the equal operator (works with numerical values)

    If you want to build a string with the caracters of the array of char,
    you can do this :

    /* allocate room enough */
    size_t const len = sizeof arr;
    char *arr2 = malloc (len + 1); /* <stdlib.h> */

    if (arr2 != NULL)
    {
    /* make the string */
    *arr2 = 0;
    strncat (arr2, arr, len);

    <...>

    /* when finished */
    free (arr2), arr2 = NULL;
    }

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

    "C is a sharp tool"

    Comment

    • Martin Ambuhl

      #3
      Re: strtoint

      Profetas wrote:
      [color=blue]
      > does this function exist in c? on any equivalent? that convert string to
      > integer?
      >
      > how do I convert a array to a "string"
      > example
      > char arr['a','b','c'];
      > char *arr2;
      >
      > how can I make arr2 = arr ?
      >[/color]
      Here we have an example of someone who has not bothered to check even
      the most elementary textbook.

      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>

      int main(void)
      {
      char arr[3] = "abc";
      char *arr1, *arr2, *arr3, *arr4;
      size_t i;

      arr1 = arr;
      printf("I have set arr1=arr. Let's check:\n");
      for (i = 0; i < sizeof arr; i++)
      printf("arr[%lu] = %c, arr1[%lu] = %c\n", (unsigned long) i,
      arr[i], (unsigned long) i, arr1[i]);
      putchar('\n');

      if (!(arr2 = malloc(sizeof arr))) {
      fprintf(stderr, "malloc for arr2 failed. Quitting.\n");
      exit(EXIT_FAILU RE);
      }
      memcpy(arr2, arr, sizeof arr);
      printf("I have allocated space for arr2, and copied arr to it.\n"
      "Let's check:\n");
      for (i = 0; i < sizeof arr; i++)
      printf("arr[%lu] = %c, arr2[%lu] = %c\n", (unsigned long) i,
      arr[i], (unsigned long) i, arr2[i]);
      putchar('\n');
      free(arr2);

      if (!(arr3 = malloc(1 + sizeof arr))) {
      fprintf(stderr, "malloc for arr3 failed. Quitting.\n");
      exit(EXIT_FAILU RE);
      }
      memcpy(arr3, arr, sizeof arr);
      arr3[sizeof arr] = 0;
      printf("In neither of the above do we have a string.\n"
      "I have allocated space for arr3, and copied arr "
      "and a terminating 0 to it.\n" "Let's check:\n");
      for (i = 0; i < sizeof arr; i++)
      printf("arr[%lu] = %c, arr3[%lu] = %c\n", (unsigned long) i,
      arr[i], (unsigned long) i, arr3[i]);
      printf("arr3=\" %s\"\n", arr3);
      putchar('\n');

      if (!(arr4 = malloc(1 + strlen(arr3)))) {
      fprintf(stderr, "malloc for arr4 failed. Quitting.\n");
      exit(EXIT_FAILU RE);
      }
      strcpy(arr4, arr3);
      printf
      ("I have allocated space for arr4, and copied the string "
      "arr3 to it.\n" "Let's check:\n");
      printf("arr3=\" %s\", arr4 = \"%s\"\n", arr3, arr4);
      putchar('\n');
      free(arr3);
      free(arr4);
      return 0;
      }


      I have set arr1=arr. Let's check:
      arr[0] = a, arr1[0] = a
      arr[1] = b, arr1[1] = b
      arr[2] = c, arr1[2] = c

      I have allocated space for arr2, and copied arr to it.
      Let's check:
      arr[0] = a, arr2[0] = a
      arr[1] = b, arr2[1] = b
      arr[2] = c, arr2[2] = c

      In neither of the above do we have a string.
      I have allocated space for arr3, and copied arr and a terminating 0 to it.
      Let's check:
      arr[0] = a, arr3[0] = a
      arr[1] = b, arr3[1] = b
      arr[2] = c, arr3[2] = c
      arr3="abc"

      I have allocated space for arr4, and copied the string arr3 to it.
      Let's check:
      arr3="abc", arr4 = "abc"



      Comment

      • RoSsIaCrIiLoIA

        #4
        Re: strtoint

        On Sun, 25 Jul 2004 15:56:54 -0400, Martin Ambuhl
        <mambuhl@earthl ink.net> wrote:[color=blue]
        >Profetas wrote:[color=green]
        >> does this function exist in c? on any equivalent? that convert string to
        >> integer?
        >>
        >> how do I convert a array to a "string"
        >> example
        >> char arr['a','b','c'];
        >> char *arr2;[/color][/color]

        char arr[3]={ 'a', 'b', 'c'}, *arr2;
        [color=blue][color=green]
        >> how can I make arr2 = arr ?[/color][/color]

        you can make it
        [color=blue]
        >Here we have an example of someone who has not bothered to check even
        >the most elementary textbook.
        >
        >#include <stdio.h>
        >#include <stdlib.h>
        >#include <string.h>
        >
        >int main(void)
        >{
        > char arr[3] = "abc";[/color]
        Is this right?
        or it is better this?
        char arr[] = "abc";
        or this
        char arr[4]= "abc"; /* arr[4]=={a, b, c, 0}*/
        ?
        If I write "abc" there will be somewhere in the executable
        'a''b''c'0?

        Comment

        • Emmanuel Delahaye

          #5
          Re: strtoint

          RoSsIaCrIiLoIA wrote on 25/07/04 :[color=blue][color=green]
          >> int main(void)
          >> {
          >> char arr[3] = "abc";[/color]
          > Is this right?[/color]

          Yes. But it's not a string. It's an initialized array of 3 char.
          [color=blue]
          > or it is better this?
          > char arr[] = "abc";[/color]

          It's not 'better', it's different. It's a string.
          [color=blue]
          > or this
          > char arr[4]= "abc"; /* arr[4]=={a, b, c, 0}*/[/color]

          This is a redundent version of the above one.
          [color=blue]
          > ?
          > If I write "abc" there will be somewhere in the executable
          > 'a''b''c'0?[/color]

          Yes, except in this case:

          char arr[3] = "abc";

          --
          Emmanuel
          The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

          "C is a sharp tool"

          Comment

          • RoSsIaCrIiLoIA

            #6
            Re: strtoint

            On Sun, 25 Jul 2004 21:01:35 GMT, RoSsIaCrIiLoIA <n@esiste.ee> wrote:
            [color=blue]
            >On Sun, 25 Jul 2004 15:56:54 -0400, Martin Ambuhl
            ><mambuhl@earth link.net> wrote:[color=green]
            >>Profetas wrote:[color=darkred]
            >>> does this function exist in c? on any equivalent? that convert string to
            >>> integer?
            >>>
            >>> how do I convert a array to a "string"
            >>> example
            >>> char arr['a','b','c'];
            >>> char *arr2;[/color][/color]
            >
            >char arr[3]={ 'a', 'b', 'c'}, *arr2;
            >[color=green][color=darkred]
            >>> how can I make arr2 = arr ?[/color][/color]
            >
            >you can make it[/color]

            but arr2 not point to a string

            Comment

            • RoSsIaCrIiLoIA

              #7
              Re: strtoint

              On Sun, 25 Jul 2004 15:21:09 -0400, "Profetas" <xuxu_18@yahoo. com>
              wrote:
              [color=blue]
              >does this function exist in c? on any equivalent? that convert string to
              >integer?
              >
              >how do I convert a array to a "string"
              >example
              >char arr['a','b','c'];
              >char *arr2;
              >
              >how can I make arr2 = arr ?[/color]

              the string end with 0 [= \0]

              char arr[3]={'a', 'b', 'c'}, *arr2;
              unsigned i;

              arr2 = malloc(1 + sizeof arr);
              if(arr2 == 0) return EXIT_FAILURE;
              for(i = 0; i< sizeof arr; ++i)
              arr2[i]=arr[i];
              arr2[i] = 0;
              use (arr2);
              free(arr2);
              return EXIT_SUCCESS

              Comment

              • Profetas

                #8
                Re: strtoint

                Do I have to explain every single detail so you can understand?
                I didn't ask if c had string I didn't ask if my array was an string did
                I?
                I didn't ask you how to allocate memory did I?, I ask if there is ano
                function that convert a string to integer, ok now you are gonna write 20
                pages saying that c has no string assuming that I don't know, man you
                don't know what people know so don't judge people if you didn't understand
                their question just ignore the question if you did understand the question
                and you want to answer do so, but only answer the question asked.

                Comment

                • Emmanuel Delahaye

                  #9
                  Re: strtoint

                  Profetas wrote on 26/07/04 :[color=blue]
                  > Do I have to explain every single detail so you can understand?
                  > I didn't ask if c had string I didn't ask if my array was an string did
                  > I?
                  > I didn't ask you how to allocate memory did I?, I ask if there is ano
                  > function that convert a string to integer, ok now you are gonna write 20
                  > pages saying that c has no string assuming that I don't know, man you
                  > don't know what people know so don't judge people if you didn't understand
                  > their question just ignore the question if you did understand the question
                  > and you want to answer do so, but only answer the question asked.[/color]

                  Excuse me sir, but here is a quote of your original post:

                  how do I convert a array to a "string"
                  Followed by a weird way of defining an array of char that displayed
                  some lack of knowledge about what an array or a string are. Hence, we
                  tried to make this point clear.

                  That said, we probably missed this one:

                  does this function exist in c? on any equivalent? that convert string
                  to
                  integer?
                  that was unclear because it was referring to the subject line, which is
                  obviously not the best way to establish a clear communication stream
                  between you and the readers. Better to ask a full and complete question
                  in the body of the message.

                  To answer to your initial question, yes, there are strtol(), strtoul()
                  and eventually strtod() the day you are interested in floating points.

                  --
                  Emmanuel
                  The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

                  "C is a sharp tool"

                  Comment

                  • Profetas

                    #10
                    Re: strtoint

                    >>Excuse me sir, but here is a quote of your original post
                    Sorry but it was meant to the people who act the way I described, when I
                    read the posts I don't read the names and I don mark people, I have posted
                    very few questions in the c programming language, as I spend most of my
                    time in the
                    Assembly x86 where the people is far more “mature”.

                    I am not saying I wasn't wrong, I know I am lazy while explaining what I
                    have in mind but people who think that they are the masters and everyone
                    else is newbie irritates me.

                    My goal was to generate a random string, I knew how to generate random
                    numbers, any way here is my solution.

                    for(i=0; i<16; i++)
                    {
                    var_ptr[i]=65 + rand()%25;
                    }

                    Comment

                    • jacob navia

                      #11
                      Re: strtoint


                      "Martin Ambuhl" <mambuhl@earthl ink.net> a écrit dans le message de
                      news:2mihkeFn2g 6pU1@uni-berlin.de...[color=blue][color=green]
                      > >[/color]
                      > Here we have an example of someone who has not bothered to check even
                      > the most elementary textbook.[/color]


                      Yes. And that is why I didn't bother. Why should we
                      bother if he doesn't?

                      Besides, he could have friends...

                      "Hey man, do not bother to work/learn anything. Just ask in c.l.c and
                      they will fix it for you..."





                      Comment

                      • Al Bowers

                        #12
                        Re: strtoint



                        Profetas wrote:
                        [color=blue]
                        > My goal was to generate a random string, I knew how to generate random
                        > numbers, any way here is my solution.
                        >
                        > for(i=0; i<16; i++)
                        > {
                        > var_ptr[i]=65 + rand()%25;
                        > }
                        >[/color]

                        Well that is not a bad approach. However the magic numbers 65 and 25
                        trouble me. Why not make an array of the set of characters you want
                        to be in the random string and then randomly select from the set of
                        characters.

                        Example:

                        #include <stdlib.h>
                        #include <stdio.h>
                        #include <time.h>
                        #include <string.h>

                        #define SZ_STRING 16

                        int main( void )
                        {
                        char *source = "AaBbCcDdEeFfGg HhIiJjKkLlMmNn"
                        "OoPpQqRrSsTtUu VvWwXxYyZz01234 56789";
                        char randomstr[SZ_STRING+1];
                        int i,slen;

                        /* Seed the random-number generator with current time so that
                        * the numbers will be different every time we run.
                        */
                        srand( (unsigned)time( NULL ) );
                        for(i = 0,slen = strlen(source); i < SZ_STRING;i++)
                        randomstr[i] = *(source + rand()%slen);
                        randomstr[SZ_STRING] = '\0'; /* Nul terminate to make a string */
                        printf("The random string generated is \"%s\"\n",rando mstr);
                        return 0;
                        }


                        --
                        Al Bowers
                        Tampa, Fl USA
                        mailto: xabowers@myrapi dsys.com (remove the x to send email)
                        Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!


                        Comment

                        • Profetas

                          #13
                          Re: strtoint

                          >Well that is not a bad approach. However the magic numbers 65 and 25[color=blue]
                          >trouble me. Why not make an array of the set of characters you want
                          >to be in the random string and then randomly select from the set of[/color]

                          Well they aren't so magic 65 is equal to A and 25 is the range
                          of the Alphabet :-) so the generated string will be between
                          (A-Z)

                          Comment

                          • Peter Nilsson

                            #14
                            Re: strtoint

                            "Emmanuel Delahaye" <emdel@YOURBRAn oos.fr> wrote in message
                            news:mn.cd0f7d4 719c8c72e.15512 @YOURBRAnoos.fr ...[color=blue]
                            >
                            > Bare in mind that in C, '=' is the affectation operator,[/color]

                            [On first reading I thought you said affection operator, which I could
                            really do with. ;-]

                            I think 'affectation' must be a French/English faux ami.

                            http://www.m-w.com/cgi-bin/dictionary?affectation
                            [color=blue]
                            > and that '==' is the equal operator (works with numerical values)[/color]

                            It's the 'equal to' equality operator, not to be confused with the simple
                            assignment operator which consists of a single 'equal' character. Sorry for
                            the pedantism, but I've heard the assignment operator naively called the
                            'equal operator' by many newbies, so I think it's worth mentioning what the
                            true terms and differences are.

                            Note that equality operators also work operands with with pointer types, as
                            well as arithmetic types.

                            --
                            Peter


                            Comment

                            • Giorgos Keramidas

                              #15
                              Re: strtoint

                              "Profetas" <xuxu_18@yahoo. com> writes:[color=blue][color=green]
                              > >Well that is not a bad approach. However the magic numbers 65 and 25
                              > >trouble me. Why not make an array of the set of characters you want
                              > >to be in the random string and then randomly select from the set of[/color]
                              >
                              > Well they aren't so magic 65 is equal to A[/color]

                              You shouldn't depend on that.

                              Comment

                              Working...