【发布时间】:2015-08-16 15:06:53
【问题描述】:
我有这段代码:
if(string_starts_with(line, "name: ") == 0){
//6th is the first char of the name
char name[30];
int count = 6;
while(line[count] != '\0'){
name[count-6] = line[count];
++count;
}
printf("custom string name: %s", name);
strncpy(p.name, name, 30);
}
else if(string_starts_with(line, "age: ") == 0){
//6th is the first char of the name
printf("age line: %s", line);
short age = 0;
sscanf(line, "%d", age);
printf("custom age: %d\n", age);
}
if 有效,但 else if 无效。
示例输出为:
person:
name: great
custom string name: great
age: 6000
age line: age: 6000
custom age: 0
我改变了很多,比如在sscanf函数中使用&age,但是没有任何效果。
【问题讨论】:
-
我很确定
else也可以。这是不起作用的条件。请提供minimal reproducible example! -
@GilianJoosen 他的意思是
line的类型。 -
line是如何声明的?我们在这里看到了你无法想象的东西。 -
行是:字符行[200];
-
请提供完整代码