如何使指针指向2D数组的任何数组元素? [英] How to make a pointer point to any array element of a 2D array?
本文介绍了如何使指针指向2D数组的任何数组元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
直指重点,
我希望字符指针p
指向唯一包含字符'T
'的数组元素.
I want the character pointer p
to point to the only array element that contains the character 'T
'.
char a[100][100];
char *p;
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
if(a[i][j] == 'T')
p = a[i][j];
P.S.我尝试使用*
,**
等的各种组合,但是似乎没有任何效果.
P.S. I tried with various combinations of *
, **
, etc but nothing seems to work.
推荐答案
使用其地址:
char a[100][100];
char *p;
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
if(a[i][j] == 'T')
p = &a[i][j];
a[i][j]
的类型为char
,p
的类型为char *
,其中保存一个地址.要获取变量的地址,请在其前面加上&
.
a[i][j]
is of type char
and p
is of type char *
, which holds an address. To get the address of a variable, prepend it with &
.
指针上的*
运算符以相反的方式工作.如果您想找回'T'
,请使用:
The *
operator on a pointer works the other way round. If you would want to get the 'T'
back, you'd use:
char theT = *p;
这篇关于如何使指针指向2D数组的任何数组元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文