如何判断一个变量是二维数组、指针数组还是char的双指针? [英] How can you tell whether a variable is a 2D array, array of pointers or double pointers of char?

查看:41
本文介绍了如何判断一个变量是二维数组、指针数组还是char的双指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有 3 个变量:

Lets say we have 3 variables:

 char a[4][4];
 char *b[4];
 char **c;

假设以上所有变量都正确分配了值.没有代码错误.

Lets say all of the above variables have correctly assigned values. There is no code errors.

所有这些变量都可以使用 [] 运算符打印它们的值,如下所示:

All of these variables can print their values using [] operator like below:

for( i=0; i<4; i++){
    printf( "%s\n", a[i] );
}
for( i=0; i<4; i++){
    printf( "%s\n", b[i] );
}
for( i=0; i<4; i++){
    printf( "%s\n", c[i] );

仅查看这些打印语句,无法识别其真正的数据类型.如何识别其变量数据类型?

Just looking at these print statements there is no way to identify its true datatypes. How to identify its variable datatypes?

我的一个想法是打印出每个索引的内存地址.对于二维数组,内存地址应该以相等的距离分开.但是对于数组指针,我希望内存地址在空间上彼此不均匀.有没有更好的方法来找出这些变量的数据类型?

One idea I had was to print out the memory addresses of each index. With 2-D array, the memory address should be separated with equal distance from each other. But with pointer of array, I expected the memory addresses to be not uniformly spatially distant from each other. Is there a better way to find out datatypes of these variables?

推荐答案

如何判断一个变量是二维数组、指针数组还是char的双指针?

How can you tell whether a variable is a 2D array, array of pointers or double pointers of char?

一种区分类型的方法.
请注意,二维数组"不是类型 - 更像是类型的分类.
char 的双指针"可以认为是 char **

将对象的地址传递给_Generic().

#define xtype(X) _Generic((&X), \
    char (*)[4][4]: "char [4][4]", \
    char *(*)[4]  : "char *[4]", \
    char ***      : "char **", \
    char (*)[4]   : "char [4]", \
    char **       : "char *", \
    char *        : "char", \
    default       : "?" \
)

int main(void) {
  char a[4][4];
  char *b[4];
  char **c;
  puts(xtype(a));
  puts(xtype(b));
  puts(xtype(c));
  puts(xtype(a[0]));
  puts(xtype(b[0]));
  puts(xtype(c[0]));
  puts(xtype(a[0][0]));
  puts(xtype(b[0][0]));
  puts(xtype(c[0][0]));
}

输出

char [4][4]
char *[4]
char **
char [4]
char *
char *
char
char
char

<小时>

_Generic() 是对 C 的有用补充,它的一些细节和正确应用仍然具有挑战性.我希望以上至少能让 OP 有部分区分物体的能力.


_Generic() is a useful addition to C and some of its details and proper application are still challenging. I hope the above allows at least a partial ability for OP to distinguish objects.

有趣的是,我能够使用更通用的 _Generic 如下,在 a,b,c 之间具有相同的区别.我对 _Generic 的某些方面持谨慎态度,因为我怀疑实现定义的行为.

Interestingly, I was able to use a more generic _Generic as below with equal distinguishably amongst a,b,c. I am wary of some aspects of _Generic as I suspect implementation defined behavior.

#define xtype(X) _Generic((&X), \
    char (*)[][4] : "char [][4]", \
    char *(*)[]   : "char *[]", \
    char ***      : "char **", \
    char (*)[]    : "char []", \
    char **       : "char *", \
    char *        : "char", \
    default       : "?" \
)

这篇关于如何判断一个变量是二维数组、指针数组还是char的双指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆