关于c语言的探究
逻辑短路
原来的代码是
if (temp == 3 && (a % b != 0 || b == 0 )){ printf("go"); }
dev-c 报错:
program received signal sigsegv arithmetic **
改正后
c语言&&和||的短路性质分析 - 痞子晶的博客 - CSDN博客
https://blog.csdn.net/yhj110911119/article/details/52436085
C语言的短路现象 - blue_apolo的博客 - CSDN博客
https://blog.csdn.net/blue_apolo/article/details/51831320
结构体分析
typedef struct{ int a; int b; int c; int d; } TD;
完整代码如下
#include <stdio.h> typedef struct{ int a; int b; int c; int d; } TD; //typedef int[4] intx4; int LLsize = sizeof(long long); int INTsize = sizeof(int); int PINTsize = sizeof(int *); int main(void) { TD data; TD *dataPtr; dataPtr = &data; dataPtr->a=0xA00AA11A; dataPtr->b=0xB00BB22B; dataPtr->c=0xC00CC33C; dataPtr->d=0xD00DD44D; printf("Sizeof int is %d Bytes\n", INTsize); printf("Sizeof pint is %d Bytes\n", PINTsize); printf("Sizeof Long Long is %d Bytes\n", LLsize); printf("[start]: \n"); printf("%llx \n", *(TD *)dataPtr);
printf("ALL DATA is {\n"); // 下面通过移动指针来打印数据(移动距离以 int 类型大小为一个单位。) printf("%llX \n", *(((int *)(dataPtr)) + 0)); //等效于 printf("%llX \n", *(int *)dataPtr); printf("%llX \n", *(((int *)(dataPtr)) + 1)); printf("%llX \n", *(((int *)(dataPtr)) + 2)); printf("%llX \n", *(((int *)(dataPtr)) + 3)); printf("} \n"); printf("\n\nABCD is: \n"); printf("%X \n", (*dataPtr).a); printf("%X \n", (*dataPtr).b); printf("%X \n", (*dataPtr).c); printf("%X \n", (*dataPtr).d); printf("\n\n\nABCD's pointer is: \n"); printf("%p \n", &(*dataPtr).a); printf("%p \n", &(*dataPtr).b); printf("%p \n", &(*dataPtr).c); printf("%p \n", &(*dataPtr).d); printf("[End]"); return 0; }
输出结果
Sizeof int is 4 Bytes Sizeof pint is 8 Bytes Sizeof Long Long is 8 Bytes [start]: b00bb22ba00aa11a ALL DATA is { A00AA11A B00BB22B C00CC33C D00DD44D } ABCD is: A00AA11A B00BB22B C00CC33C D00DD44D ABCD's pointer is: 0x7ffe7b2715b0 0x7ffe7b2715b4 0x7ffe7b2715b8 0x7ffe7b2715bc [End]
在线测试:https://c.runoob.com/compile/11
结论:结构体是一段线性空间,里面仅包含了所有变量,不包含本结构体的大小等信息。结构体本身所在首地址和结构体里第一变量的首地址是相同的。
另外,结构体还有内存空间对齐的知识,见 《C Primer Plus》
--------蓝天上的云_转载请注明出处.