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!

Recommended Answers

All 3 Replies

You're incrementing i in the wrong place.

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

Thank you aspire and nisheeth!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.