如果我们增加任何指针变量,则poiter指向下一个元素位置,那个元素是什么? [英] If we increment any pointer variable then the poiter points to the next element location , What is that element?

查看:128
本文介绍了如果我们增加任何指针变量,则poiter指向下一个元素位置,那个元素是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们增加任何指针变量,则poiter指向下一个元素位置,该元素是什么?

If we increment any pointer variable then the poiter points to the next element location , What is that element?

推荐答案

取决于指针在指针中要寻址的内容.第一名!
如果您有整数数组:
Depends on what the pointer was addressing in the first place!
If you had an array of integers:
int ar[10];
int* p = &(ar[7]);


然后

p++;

p = &(ar[8]);

是等效的.


但是在数组中,我们只有10个位置,如果它指向一个变量,那么下一个指向的位置是什么,如何确定?"


如果您的意思是:

are equivalent.


"but in array we have only 10 location & in case of a variable to which it points, then what will be the next location of pointing and how will it be decided?"


If you mean:

int ar[10];
int* p = &(ar[7]);
p++; //OK, p == &(ar[8])
p++; //OK, p == &(ar[9])
p++; //OK, but p == &(ar[10])

指针现在无效.只要您不尝试阅读它

The pointer is now invalid. Provided you do not try to read from it

int i = *p;

或写给它

*p = 6;

,那么您将不会收到错误消息.但是它指向的位置无关紧要:它超出了数组的有效地址范围.使用它会导致问题,或者在运行时发现问题,或者损坏其他数据并导致奇怪的错误.



那么,您是说它是指向未知位置的指针,但是我们仍然可以增加它吗?"


是的.指针只是一个数字-在使用之前没有任何意义.将其视为变量中的数组索引:

then you will not get an error. But it is irrelevant where it is pointing: it is outside the range of valid addresses for the array. Using it will cause problems, either spotted at run time, or corrupting other data and resulting in strange errors.



"so Do you mean that is it a pointer pointing to unknown location but still we can increment it?"


Yes. A pointer is just a number - it has no relevance until it is used. Think about it as an array index in a variable:

int ar[10];
int index = 7;
int i = ar[index];

很好,没问题.

is fine, no problems.

index = 10;

也可以.只有在尝试使用索引时,您才会遇到问题:

Is also fine. It''s only when you try to use the index that you get problems:

index = 10; // Fine, no hassle.
ar[index] = 6; // And Boom! there is no element 10, so you get problems.

如果在分配指针时检查了指针,则无法有效使用它们:将指针替换为一个int值数组是否无效?另一个整数数组中的地址?

If your pointers were checked when you assigned them, you could not use them effectively: is it invalid to replace a pointer into one array of int values with an address in another array of integers?


递增指针将元素大小添加到该地址. (从指针类型可以知道元素的大小.)编译器将不会(无法)检查在新地址处是否存在预期类型的​​数据.这就是为什么在C/C ++中指针如此方便且如此不安全的原因.
Incrementing a pointer adds the element size to the address. (The element size is known from the pointer type.) The compiler will not (can not) check if there is data of the expected type at the new address. This is why pointers are so handy and so unsafe in C/C++.

short* p; // p is a pointer to a short (p is not initialized so far)
p++; // now p point two bytes further (wherever that is)


指向元素的指针实际上称为该局部元素的地址.
The pointer that points the element is actually called the address of that partcular element.


这篇关于如果我们增加任何指针变量,则poiter指向下一个元素位置,那个元素是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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