943,934 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 5895
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 7th, 2007
0

Problems from string to int array

Expand Post »
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 !!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
BigFormat is offline Offline
45 posts
since Sep 2007
Sep 7th, 2007
0

Re: Problems from string to int array

>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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 7th, 2007
0

Re: Problems from string to int array

Click to Expand / Collapse  Quote originally posted by BigFormat ...
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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Sep 8th, 2007
0

Re: Problems from string to int array

Click to Expand / Collapse  Quote originally posted by iamthwee ...
>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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 8th, 2007
0

Re: Problems from string to int array

Click to Expand / Collapse  Quote originally posted by iamthwee ...
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.
Reputation Points: 10
Solved Threads: 0
Light Poster
BigFormat is offline Offline
45 posts
since Sep 2007
Sep 8th, 2007
0

Re: Problems from string to int array

Click to Expand / Collapse  Quote originally posted by Aia ...
  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.
Reputation Points: 10
Solved Threads: 0
Light Poster
BigFormat is offline Offline
45 posts
since Sep 2007
Sep 8th, 2007
0

Re: Problems from string to int array

Did you look at those links?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 8th, 2007
0

Re: Problems from string to int array

Click to Expand / Collapse  Quote originally posted by iamthwee ...
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..)
Reputation Points: 10
Solved Threads: 0
Light Poster
BigFormat is offline Offline
45 posts
since Sep 2007
Sep 8th, 2007
0

Re: Problems from string to int array

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 8th, 2007
0

Re: Problems from string to int array

Click to Expand / Collapse  Quote originally posted by BigFormat ...
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. }

Click to Expand / Collapse  Quote originally posted by BigFormat ...
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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: graphic problem in c
Next Thread in C Forum Timeline: Return string





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC