Ç - 指数适用于指针的指针 [英] C - index applied to pointer to pointer
本文介绍了Ç - 指数适用于指针的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要问,当它看到一个指向指针的指数是什么c执行;例如:
结构X {
int类型的;
INT B:
};结构点¯x** PTR;
如果一个语句包含会发生什么:
PTR [I] // i是一个unsigned int
解决方案
任何指针可以用数组索引使用,所以 PTR [I]
将是一个指针结构X
。
但是,你必须要分配给 PTR
内存首先当然,否则你将间接引用未初始化的指针导致不确定的行为。如果你解引用 PTR [I]
不初始化该指针,然后你又具有不确定的行为。
I want to ask what does C do when it sees an index on a pointer to pointer; for example:
struct X {
int a;
int b;
};
struct X ** ptr;
What will happen if a statement contains :
ptr[i] // where i is an unsigned int
解决方案
Any pointer can be used with array indexing, so ptr[i]
will be a pointer to struct X
.
However, you have to allocate memory for ptr
first of course, otherwise you will dereference an uninitialized pointer leading to undefined behavior. And if you dereference ptr[i]
without initializing that pointer, then you're again have undefined behavior.
这篇关于Ç - 指数适用于指针的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文