c 迷惑1

1.
#include <iostream>
using namespace std;


void fun(int b[100])
{
cout << sizeof(b); //4

}


int main()
{
int *p = NULL;
sizeof(p); //4
sizeof(*p);//4
sizeof(int) *p; //error 4不能乘以指针

int a[100];
cout << sizeof(a) << endl; //400
cout << sizeof(a[100]) << endl; //4
cout << sizeof(&a) << endl; //4
cout << sizeof(&a[0]); //4

int b[100];

cout << sizeof(b) << endl; //400
fun(b);
}


2.
#include <iostream>
using namespace std;






int main()
{
int a[100];
cout << strlen(a) << endl; //不确定
int i;
for(i=0; i<1000; i++)
{
a[i] = -1-i; //vs2012出错,用int赋值char数组
}
cout << strlen(a) << endl; //255
return 0;
}
//strlen计算字符串长度,不包含字符串的“\0”
3.
#include <iostream>
using namespace std;


int main()
{
    int i = -20;
unsigned int j = 10;
cout << i+j; //4294967286
//无符号数和有符号数相加,都变成无符号数

    return 0;
}
4.
#include <iostream>
using namespace std;




int main()
{
    int* print;
cout << print << endl;//00000001
print++;
cout << print << endl;//00000005
print += 1;
cout << print << endl;//00000009


void* pvoid;
cout << pvoid << endl;//有地址
//pvoid++; //void* vs2012不能进行加法
(int*)pvoid++;//vs2012强转依然不能进行加法
cout << pvoid << endl;//强转int*,地址不会变
    return 0;
}




5.
#include<stdio.h>


int main()
{
const int s = 1;
    switch(1)
{
case(s): printf("yes"); //vs2012出错,case表达式不是常量。如果是c++,可以正确执行
break;
default:
printf("default");
}
return 0;

}

6.


#include <stdio.h>


int main()
{
const volatile int i = 10; //没问题


//i = 12; //error 左值指定const对象
int f = i;
f = 88;


return 0;


}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值