F
Franklin Li
Hi all,
I have one simple program:
#define __EXTENSIONS__
#include <stdio.h>
#include <string.h>
int main() {
char *buf="5/90/45";
char *token;
char *lasts;
printf("tokenizing \"%s\" with strtok():\n", buf);
if ((token = strtok(buf, "/")) != NULL) {
printf("token = "%s\"\n", token);
while ((token = strtok(NULL, "/")) != NULL) {
printf("token = \"%s\"\n", token);
}
}
}
When I compile it using CC and got one Warning: String literal converted to
char* in assignment.
When I run it and got Segmentation Fault(coredump).
Then I changed defination as below:
char str[] = "5/90/45";
char *buf = str;
Then this is no warning and I can get the correct result.
Could you give me the reason?
I think I can use char *point = "string" directly.
Thansk.
Franklin
I have one simple program:
#define __EXTENSIONS__
#include <stdio.h>
#include <string.h>
int main() {
char *buf="5/90/45";
char *token;
char *lasts;
printf("tokenizing \"%s\" with strtok():\n", buf);
if ((token = strtok(buf, "/")) != NULL) {
printf("token = "%s\"\n", token);
while ((token = strtok(NULL, "/")) != NULL) {
printf("token = \"%s\"\n", token);
}
}
}
When I compile it using CC and got one Warning: String literal converted to
char* in assignment.
When I run it and got Segmentation Fault(coredump).
Then I changed defination as below:
char str[] = "5/90/45";
char *buf = str;
Then this is no warning and I can get the correct result.
Could you give me the reason?
I think I can use char *point = "string" directly.
Thansk.
Franklin