0562 赋值的右操作数指向更严格的类型
On the other hand, it is perfectly legitimate(合法的) to initialize an object of type "const TYPE *" or "volatile TYPE *" or even "const volatile TYPE *" with an expression of type "TYPE *".
const int * a; 指向的内容是常量,指针本身可以变
int *b;
*b = 6;
a = b ; 合法, 以后不能通过指针a 修改,a所指向的内容
b = a; 不合法,b所指向的值可以随便改,与a的定义相悖。
extern int *gpi; extern const int *gpci; extern volatile int *gpvi; extern const volatile int *gpcvi; void test(void) { int *xpi; const int *xpci; volatile int *xpvi; const volatile int *xpcvi; xpi = gpi; /* */ xpi = gpci; /* Message 0562 */ xpi = gpvi; /* Message 0562 */ xpi = gpcvi; /* Message 0562 */ xpci = gpi; /* */ xpci = gpci; /* */ xpci = gpvi; /* Message 0562 */ xpci = gpcvi; /* Message 0562 */ xpvi = gpi; /* */ xpvi = gpci; /* Message 0562 */ xpvi = gpvi; /* */ xpvi = gpcvi; /* Message 0562 */ xpcvi = gpi; /* */ xpcvi = gpci; /* */ xpcvi = gpvi; /* */ xpcvi = gpcvi; /* */ }
0588 位域宽度必须是整数常量表达式。
#define MAX 3.2f #define X_WIDTH (int)MAX #define Y_WIDTH (int)(1.9f * MAX) struct bits { unsigned int bx:X_WIDTH; /* OK */ unsigned int by:Y_WIDTH; /* Message 0588 浮点数 错误 */ };
0589 枚举常量必须是整数常量表达式。
#define MAX 3.2f #define X (int)MAX #define Y (int)(1.9f * MAX) enum colours { RED = X, YELLOW = Y, /* Message 0589 浮点数 错误*/ GREEN, BLUE };
0591 A 'case' label must be an integral constant expression(整数常量表达式)
Integral constant expressions:
... are used in case expressions, to specify array bounds, to specify the size of a bit-field and as enumerator constants.
They may contain:
- Integral constants 整型常数
- Enum constants 枚举常数
- Character constants 字符常数
- sizeof expressions sizeof 表达式
- Floating constants cast to an integer type 浮点型强转成整形
... but may not contain (except as an argument to the sizeof operator):
- Arrays
- * operator
- & operator
- Structure member operations
#define A 1 #define B (int)2. #define C 3.5 #define D (int)(2.2 * 2.5) void foo(int n) { switch(n) { case A: /* OK: integer constant */ break; case B: /* OK: float cast to int */ break; case C: /* Message 0591: float expression 浮点数不可以 */ break; case D: /* Message 0591: not an integral constant expression 不是整数常量表达式 */ break; default: break; } }
https://blog.csdn.net/ishellhub/article/details/86285957
switch
在C语言中的语法如下
switch(expression) { case constant-expression : statement(s); break; /* optional */ case constant-expression : statement(s); break; /* optional */ /* you can have any number of case statements */ default : /* Optional */ statement(s); }
switch语句中使用的表达式必须具是int或enum类型,否则如float等其他数据类型是无法通过的编译的,因为编译器需要switch后面的语句和case后面的值精确匹配,而计算机无法精确表达一个float数据类型
switch可以任意个case语句(包括没有), 值和语句之间使用:分隔
case后面的值必须是int常量值,或者返回结果为int类型的表达式,以下代码无法编译通过
当switch后面的变量值和case后面的常量值匹配相等后,case后面的代码将会被执行,直到break语句被执行后跳出switch代码块
break不是必须的,如果没有break,则执行完当前case的代码块后会继续执行后面case代码块的内容,直到执行break才可以退出
switch有一个默认的情况,我们用default关键词表示,当switch后面的变量和所有case后面的常量都不匹配的情况下,默认执行default后面的语句
/* example 1 */
#include <stdio.h> int main () { /* local variable definition */ char grade; scanf("%c", &grade); switch(grade) { case 'A' : /* char与int是一回事 */ printf("Excellent!\n" ); break; case 'B' : case 'C' : printf("Well done\n" ); break; case 'D' : printf("You passed\n" ); break; case 'F' : printf("Better try again\n" ); break; default : printf("Invalid grade\n" ); } printf("Your grade is %c\n", grade ); return 0; }
/* example 2 */
#include <stdio.h> int main() { printf("Please input your grade(1-100):"); int grade; scanf("%d", &grade); switch (grade / 10) { case 10: case 9: printf("A\n"); break; case 8: case 7: printf("B\n"); break; case 6: case 5: printf("C\n"); break; default: break; } return 0; }
类型说明符或存储类说明符的非法组合。
void foo(void) { unsigned int xa; unsigned unsigned int xb; /* Message 0616 */ long xc; long long xd; long long long xe; /* Message 0616 */ float xf; double xg; long double xh; long float xi; /* Message 0616 */ static int ya; static extern int yb; /* Message 0616 */ typedef extern int yc; /* Message 0616 */ static register int yd; /* Message 0616 */ }
0640 struct , union中的成员不能是 void 类型的
Members of a struct or union cannot be declared with type void, or indeed any incomplete type
incomplete type不完整类型: 因为缺少信息,导致无法判断一种类型的大小
A type which lacks information required to determine its size; for example an array type in which the array dimension is missing or a structure/union type for which members are not specified.
struct ST1 {int a; void b; }; /* Message 0640 无法判断b的大小 */ struct ST2 {int a; void *b; }; /* OK 指针类型默认占用4个字节 */ struct ST3 {int a; int buf[]; }; /* Message 0642 数组有多少个元素未知 */
0641 struct or union不能有函数类型,函数指针可以,但是要用对!
This struct or union member has been declared with "function" type; perhaps "pointer to function" was intended?
struct ST1 { int mode; int f(void);}; /* Message 0641 - f has function type */ struct ST2 { int mode; int (*f)(void);}; /* OK - f has type pointer to function */
0643 'struct'或'union'类型的'%s'可能不是内容未知的'struct'或'union'。
struct ST1 { int mode; struct STX sx; }; /* Message 0643 and also 3313 - if STX has not been declared */ struct ST2 { int mode; struct ST2 sx; }; /* Message 0643 */ struct ST3 { int mode; struct ST3 *ps; }; /* OK */
0644 位域的宽度必须不大于“ int”的宽度。
struct ST { unsigned int xbit: 48; /* Message 0644 - unless int contains at least as many bits */ };
0645 宽度为零的位字段无法命名。
零宽度位字段无法命名; 其目的是指示不再将其他位域打包到放置前一个位域的单元中。 换句话说,它仅用于填充,不应尝试访问填充位
struct record { unsigned int a : 5; unsigned int : 0; /* OK - unnamed zero-width bit-field */ unsigned int b :10; unsigned int c : 0; /* Message 0645 - zero-width bit-field is named */ unsigned int d : 3; };
0646 枚举常量必须具有可表示为“ int”的值。
enum list { /* int is assumed to be 32 bits */ HEAD = 2147483647, /* OK - maximum possible value */ TAIL /* Message 0646 - too large */ };
0649 在包含参数列表的函数头之后,参数的K&R样式声明不合法。
定义函数原型有新式和老式两种写法,不能混用两种形式
int f1(int x, int y) /* OK - "new-style" prototype declaration */ { return 1; } int f2(x, y) /* OK - "old-style" K&R declaration */ int x; int y; { return 1; } int f3(int x) int y; /* Message 0649 - neither old nor new */ { return 1; }
0650 命名函数参数上的非法存储类说明符。
除“寄存器”以外的存储类说明符已应用于命名的函数参数。 参数是局部对象,在每次调用函数时都会对其进行初始化。 它们是自动的,但不能使用“自动”存储类说明符。
void f1(typedef int ); /* Message 0655 */ void f2(extern int ); /* Message 0655 */ void f3(static int ); /* Message 0655 */ void f4(auto int ); /* Message 0655 */ void f5(register int ); /* OK */ void fa(typedef int x); /* Message 0650 */ void fb(extern int x); /* Message 0650 */ void fc(static int x); /* Message 0650 */ void fd(auto int x); /* Message 0650 */ void fe(register int x); /* OK */