String to array of integers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shisou
    New Member
    • Jul 2007
    • 52

    String to array of integers

    Hello again,

    This is still related to my previous post but a different question about the same program...

    I'm having some trouble converting a string to an array of integers... such as

    string = 12345
    integer[0] = 5
    integer[1] = 4
    integer[2] = 3
    integer[3] = 2
    integer[4] = 1

    This is the function i have currently...

    Code:
    struct integer* read_integer(char* stringInt){
           struct integer* intTemp;
           int i, j, test;
           intTemp = malloc(sizeof(struct integer));
           //printf("%s\n", stringInt);
           intTemp[0].digits = malloc(strlen(stringInt) * sizeof(int));
           intTemp[0].size = strlen(stringInt);
           j = 1;
           for(i=0;i<intTemp[0].size;i++){
              intTemp[0].digits[i] = atoi(stringInt[intTemp[0].size - (i+1)]); 
           printf("%d.", intTemp[0].digits[i]);         
           }
           printf("\n");
           return intTemp;
    }
    
    //this is the definition of the struct integer
    
    struct integer {
    	int* digits;
    	int size;
    };
    I'm getting a warning from the atoi() function as follows

    [Warning] cast to pointer from integer of different size

    this has been driving me mad for the past week so any help will be greatly appreciated!
  • Sick0Fant
    New Member
    • Feb 2008
    • 121

    #2
    Originally posted by Shisou
    Hello again,

    This is still related to my previous post but a different question about the same program...

    I'm having some trouble converting a string to an array of integers... such as

    string = 12345
    integer[0] = 5
    integer[1] = 4
    integer[2] = 3
    integer[3] = 2
    integer[4] = 1

    This is the function i have currently...

    Code:
    struct integer* read_integer(char* stringInt){
           struct integer* intTemp;
           int i, j, test;
           intTemp = malloc(sizeof(struct integer));
           //printf("%s\n", stringInt);
           intTemp[0].digits = malloc(strlen(stringInt) * sizeof(int));
           intTemp[0].size = strlen(stringInt);
           j = 1;
           for(i=0;i<intTemp[0].size;i++){
              intTemp[0].digits[i] = atoi(stringInt[intTemp[0].size - (i+1)]); 
           printf("%d.", intTemp[0].digits[i]);         
           }
           printf("\n");
           return intTemp;
    }
    
    //this is the definition of the struct integer
    
    struct integer {
    	int* digits;
    	int size;
    };
    I'm getting a warning from the atoi() function as follows

    [Warning] cast to pointer from integer of different size

    this has been driving me mad for the past week so any help will be greatly appreciated!
    The subscript you're using in the atoi call is derefferencing the pointer. You should use pointer arithmetic in order to pass the actual pointer.

    e.g.

    ar[i] is a value while (ar + i) is a pointer.

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      You also do not cast the result of your call to malloc to a (struct integer*). Not sure if this is causing your problems, but it can't hurt.

      Comment

      • Shisou
        New Member
        • Jul 2007
        • 52

        #4
        Originally posted by Sick0Fant
        The subscript you're using in the atoi call is derefferencing the pointer. You should use pointer arithmetic in order to pass the actual pointer.

        e.g.

        ar[i] is a value while (ar + i) is a pointer.

        AHHHHH I see! that works alot better than the way i was doing it :P Thank you sooooo much!

        one more quick question though, I need my digits to be stored backwards as I showed in the first post. with using a pointer to a specific index of the string and atoi() it is turning the whole string into integers... such as...

        string = "12345"
        integer[0] = 5
        integer[1] = 45
        integer[2] = 345
        integer[3] = 2345
        integer[4] = 12345

        is there a better way of doing this converstion so that each index only holds 1 digit?

        Comment

        • Shisou
          New Member
          • Jul 2007
          • 52

          #5
          Originally posted by Ganon11
          You also do not cast the result of your call to malloc to a (struct integer*). Not sure if this is causing your problems, but it can't hurt.
          I was taught to do this in my class but then I was told on this forum that casting the malloc can cause problems and doesn't really help at all... So I'm not sure which way is best :\

          Thanks :)

          Comment

          • Sick0Fant
            New Member
            • Feb 2008
            • 121

            #6
            Originally posted by Shisou
            AHHHHH I see! that works alot better than the way i was doing it :P Thank you sooooo much!

            one more quick question though, I need my digits to be stored backwards as I showed in the first post. with using a pointer to a specific index of the string and atoi() it is turning the whole string into integers... such as...

            string = "12345"
            integer[0] = 5
            integer[1] = 45
            integer[2] = 345
            integer[3] = 2345
            integer[4] = 12345

            is there a better way of doing this converstion so that each index only holds 1 digit?
            Are you allowed to assume that this will run on an ASCII system? If so, I would just use the fact that there is a direct relation between chars and ints. E.G. the char '0' is equivalent to the int 48 . Then you can just go element by element and fill the int array with the char array's equivalent.

            Comment

            • Shisou
              New Member
              • Jul 2007
              • 52

              #7
              Originally posted by Sick0Fant
              Are you allowed to assume that this will run on an ASCII system? If so, I would just use the fact that there is a direct relation between chars and ints. E.G. the char '0' is equivalent to the int 48 . Then you can just go element by element and fill the int array with the char array's equivalent.
              so that would look like

              intArray[0] = (int)*-pointerToIndex;

              ???

              Comment

              • Sick0Fant
                New Member
                • Feb 2008
                • 121

                #8
                Originally posted by Shisou
                so that would look like

                intArray[0] = (int)*-pointerToIndex;

                ???
                If you had the string MyString then, you'd do something like

                [Code=c]
                for(int i = 0; i < MyString.Size; i++)
                MyIntArray[i] = MyString[i] - 48;
                [/Code]

                Comment

                • Shisou
                  New Member
                  • Jul 2007
                  • 52

                  #9
                  Originally posted by Sick0Fant
                  If you had the string MyString then, you'd do something like

                  [Code=c]
                  for(int i = 0; i < MyString.Size; i++)
                  MyIntArray[i] = MyString[i] - 48;
                  [/Code]
                  hmm... that seems a bit messy... what I was going to try was to replace the last character in the string with a \0 after i read it and store it as an int, since i won't need that character anymore... however, this seemed to mess things up further

                  i tried
                  Code:
                  strDigits[lastindex] = '\0';
                  //and
                  pointertostringindex = '\0';
                  but neither of these worked, any ideas?

                  Comment

                  • Sick0Fant
                    New Member
                    • Feb 2008
                    • 121

                    #10
                    Originally posted by Shisou
                    hmm... that seems a bit messy... what I was going to try was to replace the last character in the string with a \0 after i read it and store it as an int, since i won't need that character anymore... however, this seemed to mess things up further

                    i tried
                    Code:
                    strDigits[lastindex] = '\0';
                    //and
                    pointertostringindex = '\0';
                    but neither of these worked, any ideas?
                    Just free the entire string at the end, if you don't need the data.

                    The process you describe seems to be re-reading substrings while the method I gave you reads one char at a time.

                    Comment

                    • oler1s
                      Recognized Expert Contributor
                      • Aug 2007
                      • 671

                      #11
                      I was taught to do this in my class but then I was told on this forum that casting the malloc can cause problems and doesn't really help at all... So I'm not sure which way is best :\
                      I told you about the “don’t cast” rule. It’s not something I made up. Ask the comp.lang.c or comp.std.c people for confirmation.

                      Comment

                      • Shisou
                        New Member
                        • Jul 2007
                        • 52

                        #12
                        Hey guys,

                        Thank you all for your help! I finally got it to work! and here's how in case anyone cares.

                        Code:
                        struct integer* read_integer(char* stringInt){
                               struct integer* intTemp;
                               int i;
                               char* pointTo;
                               intTemp = malloc(sizeof(struct integer));
                               //printf("%s\n", stringInt);
                               intTemp[0].digits = malloc(strlen(stringInt) * sizeof(int));
                               intTemp[0].size = strlen(stringInt); 
                               for(i=0;i<intTemp[0].size;i++){
                                   //sets pointer to index in string
                                   pointTo = &stringInt[intTemp[0].size - (i+1)]; 
                                   //reads string from index into digits index i
                                   intTemp[0].digits[i] = atoi(pointTo); 
                                   //sets index of string to '\0' so that the next index of digits doesn't pick it up
                                   *pointTo = '\0';
                               }         
                               printf("\n");
                               return intTemp;
                        }
                        thank you so much again!

                        p.s. I wasn't saying i don't believe you about the casting during malloc I just know it works either way now :)

                        Comment

                        Working...