Please help me understand what is the difference using an initialized character array like char line[80]="1:2" ( which doesn't work !! ) and using char line[80] followed by strcpy(line,"1:2").
As per my understanding in first case I have a charachter array, it has been allocated memory, and i am copying a string literal to it. And the same is done in the second case. But obviously I am wrong. So what is wrong with my understanding.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void tokenize(char* line)
{
char* cmd = strtok(line,":");
while (cmd != NULL)
{
printf ("%s\n",cmd);
cmd = strtok(NULL, ":");
}
}
int main(){
char line[80]; //char line[80]="1:2" won't work
/*
char *line;
line = malloc(80*sizeof(char));
strcpy(line,"1:2");
*/
strcpy(line,"1:2");
tokenize(line);
return 0;
}
chararray, value should be like{'1',':','2'};during initialization. At one index only one character value will be plcaed.char line[80]="1:2"is string literal and you can't modified it intokenizefunction( usingstrtok) So it not work.char line[80]="1:2"-->char line[80]="1:2";