c指针理解问题
请看下面的代码并告诉我 ***ptr
位于哪里? 即我有一种感觉 ***ptr
实际上位于 ptr[0][0][0]
我错了吗?以下是指针的 3d 表示。我试图分配一些字符,后来我想测试 ***ptr
的索引是什么?将会等待
#include<stdio.h>
#include<conio.h>
#define row 5
#define rw 3
#define col 10
char ***ptr;
int i,j,k;
void main()
{
clrscr();
ptr=(char *)malloc(row*sizeof(char *));
for(i=0;i<row;i++)
{
*(ptr+row)=(char *)malloc(rw*sizeof(char *));
printf("\t:\n");
for(j=0;j<rw;j++)
{
*(*(ptr+row)+rw)=(char *)malloc(col*sizeof(char *));
if(i==0 && j==0)
{ // *(*(ptr+row)+rw)="kabul";
**ptr="zzz";
}
else
*(*(ptr+row)+rw)="abul";
printf("\taddress=%d %d%d = %s\n",((ptr+row)+rw),i,j,*(*(ptr+row)+rw));
}
printf("\n");
}
printf("%c %d",***ptr,ptr);
getch();
}
Please have a look at the following code and tell me where does ***ptr
locates ?
i.e. i have a feelings that ***ptr
actually locates at ptr[0][0][0]
Am I wrong ? The following is a 3d representation of pointer. where I am trying to assign some characters and later i wanted to test what is the index of ***ptr
? will be waiting
#include<stdio.h>
#include<conio.h>
#define row 5
#define rw 3
#define col 10
char ***ptr;
int i,j,k;
void main()
{
clrscr();
ptr=(char *)malloc(row*sizeof(char *));
for(i=0;i<row;i++)
{
*(ptr+row)=(char *)malloc(rw*sizeof(char *));
printf("\t:\n");
for(j=0;j<rw;j++)
{
*(*(ptr+row)+rw)=(char *)malloc(col*sizeof(char *));
if(i==0 && j==0)
{ // *(*(ptr+row)+rw)="kabul";
**ptr="zzz";
}
else
*(*(ptr+row)+rw)="abul";
printf("\taddress=%d %d%d = %s\n",((ptr+row)+rw),i,j,*(*(ptr+row)+rw));
}
printf("\n");
}
printf("%c %d",***ptr,ptr);
getch();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我发现你的编码风格非常难以阅读。
回答你的问题,是的,
ptr[0][0][0]
是***ptr
的同义词。那是因为a[b]
根据定义等于*(a+b)
,因此ptr[0]
等于*ptr
等。说的是,这是我的代码版本:
请注意以下更改:
void main
。并扔掉任何印刷它的书。malloc
的返回值通常被转换为指向元素类型的指针类型。i
和j
,而不是row
和rw
。*(ptr + x)
东西?这就是为什么我们有ptr[x]
语法。strcpy
来填充字符串,但在不解释问题的情况下很难说。printf
指针时,请使用%p
。malloc
,请包含
。i
、j
)而不是全局变量,尤其是 for 循环。PS。
?真的吗?你还在用 Turbo-C 还是什么?First of all, I find your coding style extremely hard to read.
Answering your question, yes,
ptr[0][0][0]
is a synonym of***ptr
. Thats becausea[b]
is by definition equal to*(a+b)
, soptr[0]
is equal to*ptr
, etc.Said that, here is my version of your code:
Note the following changes:
void main
in C or C++. And throw away any book that prints it.malloc
is usually the number of elements times the size of the element. Place special attention to the real type that you intend to use.malloc
is usually cast to the type pointer-to-the-type-of-the-element.i
andj
, notrow
andrw
.*(ptr + x)
stuff? That's why we have theptr[x]
syntax.strcpy
to fill your strings, but difficult to say without explaining the problem.printf
a pointer, use%p
.malloc
, include<stdlib.h>
.i
,j
) to global ones, particularly for loops.PS.
<conio.h>
? Really? Are you still using Turbo-C or what?