int a[5]={}, &a+1与(int*)a+1的区别

#include <iostream>
#include <typeinfo>
using namespace std;

int  main()
{
    int b, *pb;
    char *pb2;
    char *pb3;
    //&b = 0x001af74
    pb = &b + 1;  //0x001af78   int*
    pb2 = (char*)&b + sizeof(b);  //0x001af78  char*
    pb3 = (char*)&b + 1; //0x001af75   char*
    printf("&b=%#x, pb=%#x, pb2=%#x,pb3=%#x\n", &b, pb, pb2, pb3);  

    int a[5]={1,2,3,4,5}, *pa;
    char *pa2;
    //&a = 0x002cfc00    int[5]*
    pa = (int*)(&a + 1);  //pa = 0x002cfc14  int*
    pa2 = (char*)&a + sizeof(a);  //pa2 = 0x002cfc14   char*
    //(int*)&a + 1 = 0x002cfc04          &a + 1 = 0x002cfc14
    printf("\nDiff: &a=%#x, %#x, %#x\n", &a, (int*)&a + 1, &a + 1); // 两者是有区别的  

    printf("&a=%#x, pa=%#x, pa2=%#x\n", &a, pa, pa2);
    printf("Equal2=%d\n", (void*)pa == (void*)pa2 );  

    puts("结论: &a + 1 == (char*)&a + sizeof(a)\n");  

    return 0;
}
时间: 2024-08-03 07:16:05

int a[5]={}, &a+1与(int*)a+1的区别的相关文章

void f(int(&amp;p)[3]){} 和void f(int(*p)[3]){}的区别

#include<iostream> using namespace std; void f(int(&p)[3]){ cout<<p[0]<<endl; cout<<p[2]<<endl; } int main(){ int a1[3]={1,2,3}; cout<<a1<<endl; cout<<&a1<<endl; f(a1); } 编译后输出: 0xbfbb8eb4 0xbf

解决warning: format ‘%x’ expects type ‘unsigned int’, but argument 2 has type ‘int *’

[[email protected] c]# gcc MemTest.c -o MemTest1 -WallMemTest.c: In function 'main':MemTest.c:24: warning: format '%x' expects type 'unsigned int', but argument 2 has type 'int *'MemTest.c:39: warning: format '%x' expects type 'unsigned int', but arg

C++中int转string与string转int

#include "stdafx.h" #include "string" #include "iostream" #include "vector" #include "sstream" using namespace std; int _tmain(int argc, _TCHAR* argv[]) { //string 转 int stringstream ss; string str; int i;

&quot;int?&quot; 是什么类型?和&quot;int&quot;有何区别

int?:表示可空类型,就是一种特殊的值类型,它的值可以为null用于给变量设初值得时候,给变量(int类型)赋值为null,而不是0int??:用于判断并赋值,先判断当前变量是否为null,如果是就可以赋役个新值,否则跳过public int? a=null:public int b(){return this.a ?? 0;} 值类型后面加问号表示可为空null(Nullable 结构) Nullable是.NET 2.0中新提供的一种用于标明一个值类型是否可以为空的技术. 对于一个类型,如

C#中(int)、Conver.Toint32()、int.Parse()三种类型转换方式的区别与联系--C#基础知识

自己也是刚学习C#程序设计语言,总结了一点知识点,想分享给大家.毕竟刚学习这门语言,学得不深,哪里如果有错误,请帮个忙指出一下哈,谢谢! 1.(int)可用于单精度.双精度等其他数值类型的转换(到整型int),不能用于转换string类型,例如: 这里用(int)转换string是不可以的,系统会报错,程序是不能运行. using System.Collections.Generic; using System.Linq; using System.Text; using System.Thre

详解Integer.toString(int i)方法和String.valueOf(int i)方法

通过查看String类的源码: public static String valueOf(int i) { return Integer.toString(i); } 我们可以看到,String.valueOf(int i)其实是调用了Integer.toString(int i)方法的. 再次通过查看Integer类的源码我们可以看到: public static String toString(int i) { if (i == Integer.MIN_VALUE) return "-214

编写函数int count_number_string(char str[])和函数int maxnum_string(char str[])

题目如图: 这里不再赘述 代码: //字符串中统计与查询 //杨鑫 #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXN 1000 char Str[MAXN]; /* *寻找字符串中最大的整数 * */ int maxnum_string(char str[]) { int i = 0, n = 0, maxNum = 0; while(str[i] != '\0') { if(

Service stopSelf(int statId)和onStartcommand(Intent intent,int flags,int startId)

Stopping a service A started service must manage its own lifecycle. That is, the system does not stop or destroy the service unless it must recover system memory and the service continues to run after onStartCommand() returns. So, the service must st

【c#基础】int 转换 string,string 转换 int

1.int 转换 string方法:toString() 或者 Convert.toString()举例: [code]phpcode://toString() int a =1; string b = a.toString(); //Convert.ToString() int a =1; string b = Convert.ToString(a); 2.string 转换 int(1)如果确定字符串中是可以转成数字的字符,可以用int.Parse(string s),该语句返回的是转换得到