简体   繁体   English

使用size_t指定C的printf中字符串的精度

[英]Using size_t for specifying the precision of a string in C's printf

I have a structure to represent strings in memory looking like this: 我有一个结构来表示内存中的字符串,如下所示:

typedef struct {
    size_t l;
    char   *s;
} str_t;

I believe using size_t makes sense for specifying the length of a char string. 我相信使用size_t对于指定char字符串的长度是有意义的。 I'd also like to print this string using printf("%.*s\\n", str.l, str.s) . 我还想用printf("%.*s\\n", str.l, str.s)打印这个字符串。 However, the * precision expects an int argument, not size_t . 但是, * precision需要一个int参数,而不是size_t I haven't been able to find anything relevant about this. 我一直无法找到与此有关的任何内容。 Is there someway to use this structure correctly, without a cast to int in the printf() call? 有没有正确使用这种结构,没有在printf()调用中转换为int

printf("%.*s\n", (int)str.l, str.s)
//               ^^^^^ use a type cast

Edit 编辑

OK, I didn't read the question properly. 好的,我没有正确地阅读这个问题。 You don't want to use a type cast, but I think, in this case: tough. 你不想使用类型转换,但我想,在这种情况下:艰难。

Either that or simply use fwrite 或者只是使用fwrite

fwrite(str.s, str.l, 1, stdout);
printf("\n");

You could do a macro 你可以做一个宏

#define STR2(STR) (int const){ (STR).l }, (char const*const){ (STR).s }

and then use this as printf("%.*s\\n", STR2(str)) . 然后将其用作printf("%.*s\\n", STR2(str))

Beware that this evaluates STR twice, so be carefull with side effects, but you probably knew that already. 请注意,这会对STR两次评估,因此请注意副作用,但您可能已经知道了。

Edit: 编辑:

I am using compound initializers such that these are implicit conversions. 我正在使用复合初始值设定项,这些是隐式转换。 If things go wrong there are more chances that the compiler will warn you than with an explicit cast. 如果出现问题,编译器会向您发出警告的可能性大于使用显式强制转换的情况。

Eg if STR has a field .l that is a pointer and you'd only put a cast to int , all compilers would happily convert that pointer to int . 例如,如果STR有一个字段.l是一个指针而你只是将一个强制转换为int ,那么所有编译器都会很乐意将该指针转换为int Similar for the .s field this really has to correspond to a char* or something compatible, otherwise you'd see a warning or error. 类似于.s字段,这实际上必须对应于char*或兼容的东西,否则你会看到警告或错误。

There is no guarantee that the size_t is an int, or that it can be represented within an int. 不能保证size_t是int,或者它可以在int中表示。 It's just part of C's legacy in not defining the exact size of an int, coupled with concerns that size_t's implementation might need to be leveraged to address large memory areas (ones that have more than MAX_INT values in them). 这只是C未能定义int的确切大小的遗留问题的一部分,再加上可能需要利用size_t的实现来解决大内存区域(其中包含超过MAX_INT值的内存区域)的问题。

The most common error concerning size_t is to assume that it is equivalent to unsigned int. 关于size_t的最常见错误是假设它等同于unsigned int。 Such old bugs were common, and from personal experience it makes porting from a 32 bit to a 64 bit architecture a pain, as you need to undo this assumption. 这些旧的bug很常见,从个人经验来看,它使得从32位架构移植到64位架构变得很麻烦,因为你需要撤消这个假设。

At best, you can use a cast. 充其量,你可以使用演员表。 If you really want to get rid of the cast, you could alternatively discard the use of size_t. 如果你真的想要摆脱演员表,你可以放弃使用size_t。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM