Problems from string to int array

Thread Solved

Join Date: Sep 2007
Posts: 38
Reputation: BigFormat is an unknown quantity at this point 
Solved Threads: 0
BigFormat BigFormat is offline Offline
Light Poster

Problems from string to int array

 
0
  #1
Sep 7th, 2007
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:

  1. void stringFormat(char *input, int nums){
  2.  
  3. char *data;
  4. int *numbers[nums];
  5. int i=0;
  6. char *p;
  7.  
  8. p = strtok (input,";");
  9. if(p != NULL) data=p;
  10.  
  11. p = strtok (NULL, ",;");
  12. while (p != NULL)
  13. {
  14. numbers[i] = p;
  15. i++;
  16. p = strtok (NULL, ",;");
  17. }
  18.  
  19. i=0;
  20.  
  21. /* I check array content */
  22. while (i<(nums)){
  23. printf("%d\n", *(numbers[i]) );
  24. i++;
  25. }
  26. }
...any suggestion will be highly appreciated !!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Problems from string to int array

 
0
  #2
Sep 7th, 2007
>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...
Last edited by iamthwee; Sep 7th, 2007 at 3:52 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,030
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Problems from string to int array

 
0
  #3
Sep 7th, 2007
Originally Posted by BigFormat View Post
Hi guys, I've got troubles with a conversion.
  1. int *numbers[nums]; /* this is an array of pointers of type int */
  2.  
  3. char *p; /* this is a pointer to a string */
  4.  
  5. numbers[i] = p; /* You are trying to assign a string to an element of the array of pointers type int */
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].
Maybe use sscanf() to read the value from the string.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Problems from string to int array

 
0
  #4
Sep 8th, 2007
Originally Posted by iamthwee View Post
>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...
Um actually that may be wrong...I could have sworn I remembered something like that not working...lolz

#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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 38
Reputation: BigFormat is an unknown quantity at this point 
Solved Threads: 0
BigFormat BigFormat is offline Offline
Light Poster

Re: Problems from string to int array

 
0
  #5
Sep 8th, 2007
Originally Posted by iamthwee View Post
Um actually that may be wrong...I could have sworn I remembered something like that not working...lolz

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void stringFormat ( int nums );
  5. (...)
Your code works correctly on my PC, maybe assigning a variable instead of a static value it's only bad style since I get "only" a gcc warning.
Anyway, even if I iniitialise it with a big value the problem persists.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 38
Reputation: BigFormat is an unknown quantity at this point 
Solved Threads: 0
BigFormat BigFormat is offline Offline
Light Poster

Re: Problems from string to int array

 
0
  #6
Sep 8th, 2007
Originally Posted by Aia View Post
  1. int *numbers[nums]; /* this is an array of pointers of type int */
  2.  
  3. char *p; /* this is a pointer to a string */
  4.  
  5. numbers[i] = p; /* You are trying to assign a string to an element of the array of pointers type int */
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].
Maybe use sscanf() to read the value from the string.
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..
Last edited by BigFormat; Sep 8th, 2007 at 5:47 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Problems from string to int array

 
0
  #7
Sep 8th, 2007
Did you look at those links?
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 38
Reputation: BigFormat is an unknown quantity at this point 
Solved Threads: 0
BigFormat BigFormat is offline Offline
Light Poster

Re: Problems from string to int array

 
0
  #8
Sep 8th, 2007
Originally Posted by iamthwee View Post
Did you look at those links?
Yes I read something and put the rest in bookmarks, but I've not found my answer.
I'd like to know why my code doesn't work with strtok() and atoi(), before changing everything and using sscanf() (which I found a little tricky..)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Problems from string to int array

 
0
  #9
Sep 8th, 2007
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?

  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4. int main ()
  5. {
  6. char str[] ="retard;3,4510,-5,6,77;";
  7. char * pch;
  8. printf ("Splitting: %s",str);
  9. printf("\n");
  10. pch = strtok (str,",;");
  11.  
  12. int count = 0;
  13. char data[BUFSIZ];
  14. while (pch != NULL)
  15. {
  16. char buf[BUFSIZ];
  17. char *p;
  18. printf ("%s ",pch);
  19.  
  20. int i = strtol(pch, &p, 10);
  21.  
  22. if (pch[0] != '\n' && (*p == '\n' || *p == '\0'))
  23. {
  24. printf (" = Valid integer %d entered\n", i);
  25. /*Add this to your integer array*/
  26. }
  27. else
  28. {
  29. printf(" = This is string not an integer\n");
  30. strcpy(data,pch);
  31. }
  32.  
  33. pch = strtok (NULL, ",;");
  34. count++;
  35. }
  36.  
  37. printf("The string named Data is \"%s\" ",data);
  38.  
  39. getchar();
  40. return 0;
  41. }
Last edited by iamthwee; Sep 8th, 2007 at 7:12 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,030
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Problems from string to int array

 
0
  #10
Sep 8th, 2007
Originally Posted by BigFormat View Post
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..
Here's the changes for your code to work using sscanf() to read the integer.

  1. int numbers[nums]; /* this needs to be an array of integers */
  2. .
  3. .
  4. .
  5. while (p != NULL)
  6. {
  7. sscanf(p, "%d", &numbers[i]); /* scan string for an integer */
  8. ++i;
  9. p = strtok (NULL, ",;");
  10. }

Originally Posted by BigFormat View Post
numbers[i] = atoi(p);
dunno why it doesn't work!
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC