加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

不在C中从struct打印字符串

发布时间:2020-12-16 10:22:10 所属栏目:百科 来源:网络整理
导读:从C语言中的结构打印字符串有问题… typedef struct box{ char *REF_OR_SYS; int num,x,y,w,h,o;}BOX;sscanf(str,"%s %d %d %d %d %d %d",label,refNum,x,y,w,h,o);BOX detect = {label,refNum,o};printf("nLABEL IS %sn",detect.REF_OR_SYS); //Prints ou
从C语言中的结构打印字符串有问题…

typedef struct box{
    char *REF_OR_SYS; int num,x,y,w,h,o;
}BOX;

sscanf(str,"%s %d %d %d %d %d %d",&label,&refNum,&x,&y,&w,&h,&o);
BOX detect = {label,refNum,o};
printf("nLABEL IS %sn",detect.REF_OR_SYS); //Prints out the String correctly
                                              //(Either the word REF or SYS)
return detect;

当这个结构传递给另一个结构时,所有内容都显示在字符串的EXCEPT之外.

void printBox(BOX detect){
printf("Type: %s    Ref: %d    X: %d    Y: %d    W: %d    H: %d    O:%dn",detect.REF_OR_SYS,detect.num,detect.x,detect.y,detect.w,detect.h,detect.o);

}

我错过了一些简单的事吗? REF_OR_SYS总是打印出?? _?

解决方法

使用strdup()(通常可用,如果不使用malloc())通过sscanf()将读取的字符串复制到标签中:

detect.REF_OR_SYS = strdup(label);

当该函数返回标签超出范围时,REF_OR_SYS将是一个悬空指针.记得在不再需要时释放()它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读