0

hi,
this is something i keep running into.
for example, in the following example of strtok, i'm splitting a given string on a space(" ") delimiter:

#include<stdio.h>
#include<string.h>
int main()
{
char str[]="1234563 34 7898";
char delim[]=" ";
char* result=NULL;
char new[15][3];
int i=0;
int j=0;
bzero(new, sizeof new);
result=strtok(str,delim);
i=0;
while( result != NULL ) {
   printf( "result is \"%s\"\n", result );
      strcpy(new[i],result);
i++;
printf("new[%d]=%s\n",i,new[i]);
    result = strtok( NULL, delim );
}
return 0;
}

while result prints the output correctly, the contents of new[] are printed truncated.
happens a lot of times and is probably a very important concept i'm missing out on. whats the way out?
Thanks!

3
Contributors
3
Replies
5
Views
7 Years
Discussion Span
Last Post by xenanovich
0
char new[15][3];

Make it:

char new[3][15];

[15][3] Means 15 rows of 3 cols
[3][15] Means 3 rows of 15 cols

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.