本文介绍了带条件(三元)表达式的运算符“ sizeof”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当给出三元表达式时,我很难理解 sizeof 的行为。

I have a hard time understanding sizeof's behaviour when given a ternary expression.

#define STRING "a string"

int main(int argc, char** argv)
{
  int a = sizeof(argc > 1 ? STRING : "");

  int b = sizeof(STRING);
  int c = sizeof("");

  printf("%d\n" "%d\n" "%d\n", a, b, c);

  return 0;
}

在此示例中(使用gcc 4.4.3和4.7.2测试,已编译对于 -std = c99 ),b为9(8个字符+隐式'\0'),c为1(隐式'\0')。由于某种原因,a是 4

In this example (tested with gcc 4.4.3 and 4.7.2, compiled with -std=c99), b is 9 (8 characters + implicit '\0'), c is 1 (implicit '\0'). a, for some reason, is 4.

根据argc是否大于1,我希望a为9或1。我以为字符串文字可能会在传递给 sizeof 之前转换为指针,从而导致 sizeof(char *)是4。

I would expect a to be either 9 or 1, based on whether argc is greater than 1. I thought maybe the string literals get converted to pointers before being passed to sizeof, causing sizeof(char*) to be 4.

我尝试替换 STRING 通过char数组...

I tried replacing STRING and "" by char arrays...

char x[] = "";
char y[] = "a string";
int a = sizeof(argc > 1 ? x : y);

...但是我得到了相同的结果(a = 4,b = 9,c = 1 )。

... but I got the same results (a=4, b=9, c=1).

然后,我尝试进入,但是我没有发现任何明显的解释。出于好奇,我还尝试将x和y更改为其他类型:

Then I tried to dive into the C99 spec, but I did not find any obvious explanation in it. Out of curiosity I also tried changing changing x and y to other types:


  • char long long int :a变为8

  • 两者都 short 或两者都 char :a变为4

  • char and long long int: a becomes 8
  • both short or both char: a becomes 4

因此肯定存在某种转换,但我很难找到任何官方解释。我可以想象一下,算术类型会发生这种情况(我模糊地意识到当涉及到这些类型时会进行大量的提升),但是我不明白为什么三元表达式返回的字符串文字会转换为大小为4的东西

So there's definitely some sort of conversion going on, but I struggle to find any official explanation. I can sort of imagine that this would happen with arithmetic types (I'm vaguely aware there's plenty of promotions going on when those are involved), but I don't see why a string literal returned by a ternary expression would be converted to something of size 4.

NB:在此计算机上 sizeof(int)== sizeof(foo *)== 4

NB: on this machine sizeof(int) == sizeof(foo*) == 4.

感谢指针专家。了解 sizeof ?:的工作方式实际上使我尝试了更多类型的混搭,并观察了编译器的反应。我是为了完整起见而在编辑它们:

Thanks for the pointers guys. Understanding how sizeof and ?: work actually led me to try a few more type mashups and see how the compiler reacted. I'm editing them in for completeness' sake:

foo* x = NULL; /* or foo x[] = {} */
int  y = 0;    /* or any integer type */

int a = sizeof(argc > 1 ? x : y);

收益警告:条件表达式中的指针/整数类型不匹配[默认启用] a == sizeof(foo *)

带有 foo x [],bar y [] foo * x,bar * y foo * x,y栏[] ,警告将变为指针类型不匹配。使用 void * 时没有警告。

With foo x[], bar y[], foo* x, bar* y or foo* x, bar y[], the warning becomes pointer type mismatch. No warning when using a void*.

float x = 0; /* or any floating-point type */
int   y = 0; /* or any integer type */

int a = sizeof(argc > 1 ? x : y);

没有警告,并且 a == sizeof(x)(即浮点类型)。

Yields no warning, and a == sizeof(x) (that is, the floating-point type).

float x = 0;    /* or any floating-point type */
foo*  y = NULL; /* or foo y[] = {} */

int a = sizeof(argc > 1 ? x : y);

收益率错误:条件表达式中的类型不匹配

如果我完全阅读过规范,将确保编辑此问题以指向相关部分。

If I ever read the spec completely I'll make sure to edit this question to point to the relevant parts.

推荐答案

您必须了解表达式,它们是语言的核心组成部分。

You have to understand expressions, which are the core component of the language.

每个表达式都有一个 type 。对于表达式 e sizeof e 是表达式 e 。

Every expression has a type. For an expression e, sizeof e is the size of the type of the value of the expression e.

表达式 a吗? b:c 具有类型。该类型是两个操作数表达式 b c 通用类型

The expression a ? b : c has a type. The type is the common type of the two operand expressions b and c.

在您的示例中,常见类型为 char [9] char [1] char * (两个数组值表达式都衰减到指向第一个元素的指针)。 (在C ++中,字符串文字的规则不同,并且到处都有 const 。)

In your example, the common type of char[9] and char[1] is char * (both array-valued expressions decay to a pointer to the first element). (In C++, the rules for string literals are different and there is a const everywhere.)

这篇关于带条件(三元)表达式的运算符“ sizeof”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:40