| | |
Problems from string to int array
Thread Solved |
•
•
Join Date: Sep 2007
Posts: 38
Reputation:
Solved Threads: 0
Hi guys, I've got troubles with a conversion.
My function:
void stringFormat (char *input, int nums)
receives a string like this:
"name;3,45,5,6,77;"
and a number indicating how many numbers in the String (in this case 5)
I've got to tokenize the string in order to obtain:
1) a string named data: "name"
2) an array of int made of the numbers received: numbers[0]=3; numbers[1]=45; and so on 'til 77
Here's my wrong solution:
...any suggestion will be highly appreciated !!
My function:
void stringFormat (char *input, int nums)
receives a string like this:
"name;3,45,5,6,77;"
and a number indicating how many numbers in the String (in this case 5)
I've got to tokenize the string in order to obtain:
1) a string named data: "name"
2) an array of int made of the numbers received: numbers[0]=3; numbers[1]=45; and so on 'til 77
Here's my wrong solution:
C Syntax (Toggle Plain Text)
void stringFormat(char *input, int nums){ char *data; int *numbers[nums]; int i=0; char *p; p = strtok (input,";"); if(p != NULL) data=p; p = strtok (NULL, ",;"); while (p != NULL) { numbers[i] = p; i++; p = strtok (NULL, ",;"); } i=0; /* I check array content */ while (i<(nums)){ printf("%d\n", *(numbers[i]) ); i++; } }
c Syntax (Toggle Plain Text)
int *numbers[nums]; /* this is an array of pointers of type int */ char *p; /* this is a pointer to a string */ numbers[i] = p; /* You are trying to assign a string to an element of the array of pointers type int */
Maybe use sscanf() to read the value from the string.
•
•
•
•
>int *numbers[nums];
Doesn't that have to be like... a static value? It may be ok I dunno, If you wanna do that dynamically maybe you need malloc or something? Or you could just initialise it to be a really big value, but that's not cast iron...
#include <stdio.h>
#include <string.h>
void stringFormat ( int nums );
int main ( void )
{
stringFormat ( 143 ); /*test*/
getchar();
return 0;
}
void stringFormat ( int nums )
{
int numbers[nums];
int i;
for ( i = 0; i < nums; i++ )
{
numbers[i] = 1;
printf ( "%d", numbers[i] );
}
}Anyway, have a look at some daniweb tutorials which you should find useful.
http://www.daniweb.com/code/coder5020-timestamp-2.html
http://www.daniweb.com/tutorials/tutorial45806.html
Last edited by iamthwee; Sep 8th, 2007 at 4:42 am.
•
•
Join Date: Sep 2007
Posts: 38
Reputation:
Solved Threads: 0
•
•
•
•
Um actually that may be wrong...I could have sworn I remembered something like that not working...lolz
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> void stringFormat ( int nums ); (...)
Anyway, even if I iniitialise it with a big value the problem persists.
•
•
Join Date: Sep 2007
Posts: 38
Reputation:
Solved Threads: 0
•
•
•
•
That doesn't work. You need to convert the number value store in the string pointed by `p' into integer before storing in numbers[i].c Syntax (Toggle Plain Text)
int *numbers[nums]; /* this is an array of pointers of type int */ char *p; /* this is a pointer to a string */ numbers[i] = p; /* You are trying to assign a string to an element of the array of pointers type int */
Maybe use sscanf() to read the value from the string.
I've seen some sscanf() tutorials but I've not understood the difference with strtok().
In my case, there's no way to perform a "on the fly" conversion using i.e. atoi()
something like:
numbers[i] = atoi(p);
dunno why it doesn't work!
ps: my variable numbers it's a pointer to an array of int, maybe I missed a couple of parenteses so it looked like an array of pointers of type int..
Last edited by BigFormat; Sep 8th, 2007 at 5:47 am.
No idea, I don't have a compiler to test so this is from my head.
It doesn't put it into an array but that's should be trivial. Maybe it will give you hints?
It doesn't put it into an array but that's should be trivial. Maybe it will give you hints?
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> int main () { char str[] ="retard;3,4510,-5,6,77;"; char * pch; printf ("Splitting: %s",str); printf("\n"); pch = strtok (str,",;"); int count = 0; char data[BUFSIZ]; while (pch != NULL) { char buf[BUFSIZ]; char *p; printf ("%s ",pch); int i = strtol(pch, &p, 10); if (pch[0] != '\n' && (*p == '\n' || *p == '\0')) { printf (" = Valid integer %d entered\n", i); /*Add this to your integer array*/ } else { printf(" = This is string not an integer\n"); strcpy(data,pch); } pch = strtok (NULL, ",;"); count++; } printf("The string named Data is \"%s\" ",data); getchar(); return 0; }
Last edited by iamthwee; Sep 8th, 2007 at 7:12 am.
•
•
•
•
You guessed the core of the problem.
I've seen some sscanf() tutorials but I've not understood the difference with strtok().
In my case, there's no way to perform a "on the fly" conversion using i.e. atoi()
something like:
numbers[i] = atoi(p);
dunno why it doesn't work!
ps: my variable numbers it's a pointer to an array of int, maybe I missed a couple of parenteses so it looked like an array of pointers of type int..
C Syntax (Toggle Plain Text)
int numbers[nums]; /* this needs to be an array of integers */ . . . while (p != NULL) { sscanf(p, "%d", &numbers[i]); /* scan string for an integer */ ++i; p = strtok (NULL, ",;"); }
You need to make numbers[i] an array of integers and not an array of pointers of type int. Right now you are trying to assign whatever atoi(p) gives you to a pointer memory which it can only hold the address of another memory and not an integer value.
Last edited by Aia; Sep 8th, 2007 at 9:57 pm.
![]() |
Similar Threads
- String to Int (C++)
- Coverting a string to a byte array (Visual Basic 4 / 5 / 6)
- fstream to char and int array (C++)
- Problem with string search of an array (C++)
- Conver int Array into a String (Java)
Other Threads in the C Forum
- Previous Thread: graphic problem in c
- Next Thread: Return string
| Thread Tools | Search this Thread |
* adobe ansi api array arrays binarysearch calculate centimeter char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking hardware highest homework i/o inches incrementoperators intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pattern pdf performance pointer posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string suggestions test unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h






