关于sprintf函数拼接失败问题

今天在工作中遇到一个关于sprintf的问题,代码如下:

char content_line[2048];

memset(content_line, 0, sizeof(content_line));

sprintf(content_line, "%-*.*s%-*.*s%-*.*s%-*.*s%s%-*.*s%-*.*s%-*.*s",

                                    L_LEN1, L_LEN1, (char *)s[0],

                                    L_LEN2, L_LEN2, getString1((char *s[1])),

                                    L_LEN3, L_LEN3, (char *)s[2],

                                    L_LEN4, L_LEN4, (char *)s[3],

                                    atoi(s[8]) == 1 ? " " : "-",

                                    L_LEN5, L_LEN5, (char *)s[4],

                                    L_LEN6, L_LEN6, (char *)s[5],

                                    L_LEN7, L_LEN7, (char *)s[6]);

首先content_line是足够大的,那些定长的宏均不大于20,s是char *s[10]指针数组,函数getString1的返回值是char *类型,但是就是失败了,最终在网上看到一句话说是有些编译器不知道整合的数是什么类型导致,于是我就将getString1的返回值强转为char *,同时将(char*)(atoi(s[8]) == 1 ? " " : "-"),告诉编译器需要整合的类型,最终成功了

猜你喜欢

转载自blog.csdn.net/weixin_42377147/article/details/81009489