473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
  1. struct integer* read_integer(char* stringInt){
  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 2283
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
  1. struct integer* read_integer(char* stringInt){
  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
  1. struct integer* read_integer(char* stringInt){
  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

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Johannes | last post by:
Hi, I tried to pack eight integer values and one string into one binary string, which I would like to store in a mysql db. I encountered two problems doing this: 1) $this->packed =...
4
by: David Lawson | last post by:
I know how to conver a string to an array of strings, but I need to convert an ascii string to an array of integers (really unsigned chars). Eg, $str ="ABC"; needs to convert to something...
6
by: B J H | last post by:
I want to do the following int list; String list; i int; ..... for (i=0; i<int.length, i++_ { list.concat(list); list.concat(" ");
1
by: Graeme Downes | last post by:
I have a string of integer, such as the following: "12 24 12 9 2" and I want to convert this into an array or arraylist which will seperate the string into an array of integers. How do I do...
4
by: Pokerkook | last post by:
Hello, If anybody could help me with this I would greatly appreciate it. Or at least tell me why I get the output of this garbage: 49 49 10 49 52
4
by: Dennis Myrén | last post by:
Hi. Is there a way to utilize the great primitive data type formatting routines available in .NET without working with strings? I want a byte directly rather than a string. I think it is...
6
by: comp.lang.php | last post by:
I'm involved in a rather nasty debate involving a strange issue (whereby the exasperated tell me to RTFM even after my having done so), where this is insanely possible: print_r(is_int('1'));...
5
by: cdg | last post by:
Could anyone explain how to write this sample program correctly. I need to convert an integer to a string. However, I would prefer to convert the integer to char array. But I didn`t want to use...
3
by: Csaba Gabor | last post by:
I'm comparing the text of (snippets of) web pages which I expect to be quite different or quite similar. In the case where they are similar, I would like to display the more recent one and say...
12
by: Pascal | last post by:
hello and soory for my english here is the query :"how to split a string in a random way" I try my first shot in vb 2005 express and would like to split a number in several pieces in a random way...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.