yanhua yanhua - 1 year ago 98
C Question

strtok() very weird behaviour

EDIT: new problem

char * symbolInList = listOfSymbolTokens[0];
char * addressOfSymbol = listOfSymbolTokens[1];

printf("symbolLookingFor: %s\n", symbolLookingFor);
printf("symbolInList: %s\n", symbolInList);
printf("addressOfSymbol: %s\n", addressOfSymbol);
if (strcmp(symbolInList, symbolLookingFor) == 0) {
strcpy(tokenAddress, addressOfSymbol);
printf("did not enter for loop\n");

For some reason, strcmp(symbolinList, symbolLookingFor) does not return 0 even when they are the same.

symbolLookingFor: abc
symbolInList: abc
addressOfSymbol: 3011

did not enter for loop

it does not enter the if statement

Answer Source

This is wrong

const char sdilem[1] = " ";

To store a " " string you need at least 2 characters in the buffer. (In C this is not an error.)

Firstly, to avoid such mistakes you don't need to specify the size explicitly

const char sdilem[] = " ";

Secondly, in this case you could just use

const char *sdilem = " ";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download