469,904 Members | 2,261 Online

# String to array of integers

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

Expand|Select|Wrap|Line Numbers
2.        struct integer* intTemp;
3.        int i, j, test;
4.        intTemp = malloc(sizeof(struct integer));
5.        //printf("%s\n", stringInt);
6.        intTemp[0].digits = malloc(strlen(stringInt) * sizeof(int));
7.        intTemp[0].size = strlen(stringInt);
8.        j = 1;
9.        for(i=0;i<intTemp[0].size;i++){
10.           intTemp[0].digits[i] = atoi(stringInt[intTemp[0].size - (i+1)]);
11.        printf("%d.", intTemp[0].digits[i]);
12.        }
13.        printf("\n");
14.        return intTemp;
15. }
16.
17. //this is the definition of the struct integer
18.
19. struct integer {
20.     int* digits;
21.     int size;
22. };
23.
24.
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!
May 28 '08 #1
11 1969
Sick0Fant
121 100+
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...

Expand|Select|Wrap|Line Numbers
2.        struct integer* intTemp;
3.        int i, j, test;
4.        intTemp = malloc(sizeof(struct integer));
5.        //printf("%s\n", stringInt);
6.        intTemp[0].digits = malloc(strlen(stringInt) * sizeof(int));
7.        intTemp[0].size = strlen(stringInt);
8.        j = 1;
9.        for(i=0;i<intTemp[0].size;i++){
10.           intTemp[0].digits[i] = atoi(stringInt[intTemp[0].size - (i+1)]);
11.        printf("%d.", intTemp[0].digits[i]);
12.        }
13.        printf("\n");
14.        return intTemp;
15. }
16.
17. //this is the definition of the struct integer
18.
19. struct integer {
20.     int* digits;
21.     int size;
22. };
23.
24.
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.
May 28 '08 #2
Ganon11
3,652 Expert 2GB
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.
May 28 '08 #3
Shisou
52
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?
May 28 '08 #4
Shisou
52
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 :)
May 28 '08 #5
Sick0Fant
121 100+
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.
May 28 '08 #6
Shisou
52
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;

???
May 28 '08 #7
Sick0Fant
121 100+
so that would look like

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

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

Expand|Select|Wrap|Line Numbers
1. for(int i = 0; i < MyString.Size; i++)
2.     MyIntArray[i] = MyString[i] - 48;
3.
May 28 '08 #8
Shisou
52
If you had the string MyString then, you'd do something like

Expand|Select|Wrap|Line Numbers
1. for(int i = 0; i < MyString.Size; i++)
2.     MyIntArray[i] = MyString[i] - 48;
3.
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
Expand|Select|Wrap|Line Numbers
1. strDigits[lastindex] = '\0';
2. //and
3. pointertostringindex = '\0';
4.
but neither of these worked, any ideas?
May 28 '08 #9
Sick0Fant
121 100+
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
Expand|Select|Wrap|Line Numbers
1. strDigits[lastindex] = '\0';
2. //and
3. pointertostringindex = '\0';
4.
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.
May 28 '08 #10
oler1s
671 Expert 512MB
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.
May 28 '08 #11
Shisou
52
Hey guys,

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

Expand|Select|Wrap|Line Numbers
2.        struct integer* intTemp;
3.        int i;
4.        char* pointTo;
5.        intTemp = malloc(sizeof(struct integer));
6.        //printf("%s\n", stringInt);
7.        intTemp[0].digits = malloc(strlen(stringInt) * sizeof(int));
8.        intTemp[0].size = strlen(stringInt);
9.        for(i=0;i<intTemp[0].size;i++){
10.            //sets pointer to index in string
11.            pointTo = &stringInt[intTemp[0].size - (i+1)];
12.            //reads string from index into digits index i
13.            intTemp[0].digits[i] = atoi(pointTo);
14.            //sets index of string to '\0' so that the next index of digits doesn't pick it up
15.            *pointTo = '\0';
16.        }
17.        printf("\n");
18.        return intTemp;
19. }
20.
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 :)
May 28 '08 #12