c中的数组名的指针类型解析
2009-11-28 19:52
316 查看
下面是用来输出变量信
息的宏:
在这个宏定义里面,
do
{} while(0);结
构是为了让
tmp这个变量不会和别的变量产生命名冲突。用
cerr
<< “type of “#typevar”
:”而
不用
cout,是为了关闭输出缓冲,一般情况下
cout会缓冲,而
cerr是不会缓冲的。
c++filt是
binutils工具包中的一个实用程
序,用来反向解析
c++的名字修饰,它也可以用来反向解析
java中的变量名修饰。使用
这个工具的原因是
typeinfo的输出没有标准,不同的编译器输出不一样,在
g++中,比如对于
int
a[4],
输出的是
A4i,不方便阅读,如果使用的
cl编译器,就可能不需要
这个工具。
下面是测试代码:
[/code]
输出结果:
prototype
of a:
int a[3][4]:
type
of a :int
[3][4]
type
of *a :int
[4]
type
of &a
:int (*) [3][4]
type
of a+1 :int
(*) [4]
type
of *(a+1)
:int [4]
type
of *(a+1)+1
:int*
type
of a[1] :int
[4]
type
of 1[a] :int
[4]
type
of a[1] + 1
:int*
type
of b :int
[4]
type
of &b
:int (*) [4]
type
of *(&b)
:int [4]
type
of b+1 :int*
type
of *(b+1)
:int
type
of &b + 1
:int (*) [4]
type
of *b :int
type
of c :int
[3][4][5]
type
of *c :int
[4][5]
type
of &c
:int (*) [3][4][5]
type
of c+1 :int
(*) [4][5]
type
of *(c+1)
:int [4][5]
息的宏:
#define typeof(typevar) / do { / string tmp; / cerr << "type of "#typevar" :"; / tmp = "c++filt -t "; / system((tmp + typeid(typevar).name()).c_str()); / } while(0);
在这个宏定义里面,
do
{} while(0);结
构是为了让
tmp这个变量不会和别的变量产生命名冲突。用
cerr
<< “type of “#typevar”
:”而
不用
cout,是为了关闭输出缓冲,一般情况下
cout会缓冲,而
cerr是不会缓冲的。
c++filt是
binutils工具包中的一个实用程
序,用来反向解析
c++的名字修饰,它也可以用来反向解析
java中的变量名修饰。使用
这个工具的原因是
typeinfo的输出没有标准,不同的编译器输出不一样,在
g++中,比如对于
int
a[4],
输出的是
A4i,不方便阅读,如果使用的
cl编译器,就可能不需要
这个工具。
下面是测试代码:
#include <iostream>
#include <string>
#include <typeinfo>
using namespace std;
#define typeof(typevar) / do { / string tmp; / cerr << "type of "#typevar" :"; / tmp = "c++filt -t "; / system((tmp + typeid(typevar).name()).c_str()); / } while(0);
int main() {
int a[3][4];
cout << "prototype of a: int a[3][4]:/n";
typeof(a);
typeof(*a);
typeof(&a);
typeof(a+1);
typeof(*(a+1));
typeof(*(a+1)+1);
typeof(a[1]);
typeof(1[a]);
typeof(a[1] + 1);
int b[4];
cout << "/n/n";
typeof(b);
typeof(&b);
//typeof(&(&b)); // error, &b is lvalue
typeof(*(&b));
typeof(b+1);
typeof(*(b+1));
typeof(&b + 1);
//typeof(&(b+1)); // error, invalid lvalue
typeof(*b);
int c[3][4][5];
cout << "/n/n";
typeof(c);
typeof(*c);
typeof(&c);
typeof(c+1);
typeof(*(c+1));
//typeof(&(c+1)); // error, invalid lvalue
int d[1][4];
cout << "/n/n";
typeof(d);
typeof(*d);
typeof(&d);
typeof(d+1);
typeof(*(d+1));
return 0;
}
[/code]
输出结果:
prototype
of a:
int a[3][4]:
type
of a :int
[3][4]
type
of *a :int
[4]
type
of &a
:int (*) [3][4]
type
of a+1 :int
(*) [4]
type
of *(a+1)
:int [4]
type
of *(a+1)+1
:int*
type
of a[1] :int
[4]
type
of 1[a] :int
[4]
type
of a[1] + 1
:int*
type
of b :int
[4]
type
of &b
:int (*) [4]
type
of *(&b)
:int [4]
type
of b+1 :int*
type
of *(b+1)
:int
type
of &b + 1
:int (*) [4]
type
of *b :int
type
of c :int
[3][4][5]
type
of *c :int
[4][5]
type
of &c
:int (*) [3][4][5]
type
of c+1 :int
(*) [4][5]
type
of *(c+1)
:int [4][5]
相关文章推荐
- iOS 最详细的解析(数组与指针)笔试题
- 关于C中字符数组,字符指针以及C++中string类型的两两转换及排序
- C语言回顾(六、基类型,数组指针,字符串指针,函数指针,文字常量区)——iOS开发基础
- C语言 内存分配 地址 指针 数组 参数 实例解析
- 解析C++普通局部变量与指针类型的对象变量的应用区别
- 解析PHP将对象转换成数组的方法(兼容多维数组类型)
- 指针数组和字符类型的二维数组在处理字符串时的区别
- 数组与指针类型的关系
- C为什么要把数组类型的函数参数认为是指向数组第一个元素的指针
- 解析C++普通局部变量与指针类型的对象变量的应用区别
- C 语言中的数组类型和数组指针类型.
- 关于数据类型与指针的小笔记--初始化问题&&数组
- sizeof关于数组、指针、基本数据类型的使用
- 关于结构指针类型数组做函数参数的一个问题#include<stdio.h>typede
- 6.数组类型和数组指针类型
- 指针和数组实例解析(二)
- 【嵌入式开发】C语言 内存分配 地址 指针 数组 参数 实例解析
- 指针数组 看过的最经典的解析
- 黑马程序员————C语言基本语法(数组、字符串、指针、其他数据类型)
- C语言的指针类型详细解析