通过指针访问数组 [英] Accessing array by a pointer

查看:75
本文介绍了通过指针访问数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想知道我的小例子是不是

危险:


#define SIZE 10

char global [SIZE];

char * globalPtr = global;


int main( )

{

char i;


for(i = 0; i< SIZE; i = i + 1) {

(* globalPtr)= i;

globalPtr ++;

}

}


我正在使用

整数值初始化char数组''global''(由''i''表示)。但由于

整数数据类型比char数据类型大4倍(我的机器上是4个字节),我初始化char元素但是

同时我用剩余的3个整数字节覆盖3个后续char数组元素

。此外,在初始化最后一个数组元素时,我写了超出数组范围的3个字节

这可能会破坏存储在内存中的其他一些值。

这是对的吗?


克里斯

解决方案



Christian Christmann写道:



我想知道我的小例子是不是危险:

> #define SIZE 10
char global [SIZE];
char * globalPtr = global;

int main()


法术它出来了:


int main(无效)

{
char i;

for(i = 0; i< SIZE; i = i + 1){
(* globalPtr)= i;
globalPtr ++;
}


作为`main`返回一个`int`所以你应该:


返回0;

}

我正在初始化char数组'带有
整数值的'global''(由''i''表示)。但是因为


不,你不是。你的`i`是`char`类型。

整数数据类型比char数据类型大4倍(我的机器上是4个字节),我初始化char元素但是在
同时我用剩余的3个整数字节覆盖3个后续的char数组元素。而且,在初始化最后一个数组元素时,我写了超出数组范围的3个字节,这可能会破坏存储在内存中的其他一些值。
这是对的吗?



编号无论你怎样看待它。


Vladimir S. Oka:

Christian Christmann:


{
char i;
[...]我正在使用
整数值初始化char数组''global''(由''i''表示)。但自从



不,你不是。你的`i`是`char`类型。




这是一个整数,见6.2.5类型 - 4,6和7.

整数数据类型比char数据类型大4倍(我机器上4个字节),我初始化char元素但是
相同时间我用剩余的3个整数字节覆盖3个后续的char数组元素。而且,在初始化最后一个数组元素时,我写了超出数组范围的3个字节,这可能会破坏存储在内存中的其他一些值。
这是对的吗?


不管你看它的方式。




对,即使我是整数类型int也不行。 :-)


Jirka


Christian Christmann写道:


<我想知道我的小例子是不是危险:

#define SIZE 10
char global [SIZE];
char * globalPtr = global;

int main()
{
char i;

for(i = 0; i< SIZE; i = i + 1){
(* globalPtr)= i;
globalPtr ++;
}

我正在初始化char数组''全局''与
整数值(由''i''表示)。但由于整数数据类型比char数据类型大4倍(我的机器上是4个字节),我初始化char元素但是同时我覆盖了3个后续char数组元素由剩余的3个整数字节组成。而且,在初始化最后一个数组元素时,我写了超出数组范围的3个字节,这可能会破坏存储在内存中的其他一些值。
这是对的吗?



假设您更改了代码 - 我已经将我从char更改为long int - 所以

,在循环中,分配给* globalPtr的值是/更多/显然

大于char。

#define SIZE 10

char global [SIZE];


char * globalPtr = global;


int main(无效)

{

long int i;


for(i = 100000; i< 100009; ++ i)

{

* globalPtr = i;


globalPtr ++;

}


返回0;

}


在第一次迭代中,二进制中的i的值是11000011010100000,并且

将其分配给char将导致* not *在某些内存覆盖中,但是a / b
数据丢失,例如地址为globalPtr的char可能会有

值10100000,即它只有CHAR_BITs值得长。

-

==============

不是学生

==============


Hi,

I''m wondering if my small example is not
"dangerous":

#define SIZE 10
char global[SIZE];
char* globalPtr = global;

int main()
{
char i;

for ( i = 0; i < SIZE; i = i + 1 ) {
(*globalPtr) = i;
globalPtr++;
}
}

I''m initializing the char array ''global'' with
integer values (represented by ''i''). But since the
integer data type is 4x larger (4 byte on my machine) than
the char data type, I initialize the char element but at
the same time I overwrite the 3 subsequent char array elements
by the remaining 3 integer bytes. Moreover, when initializing
the last array element, I write 3 bytes beyond the array ranges
which might corrupt some other values stored in memory.
Is this right?

Chris

解决方案


Christian Christmann wrote:

Hi,

I''m wondering if my small example is not
"dangerous":

#define SIZE 10
char global[SIZE];
char* globalPtr = global;

int main()
Spell it out:

int main(void)
{
char i;

for ( i = 0; i < SIZE; i = i + 1 ) {
(*globalPtr) = i;
globalPtr++;
}
As `main` returns an `int` so should you:

return 0;
}

I''m initializing the char array ''global'' with
integer values (represented by ''i''). But since the
No, you''re not. Your `i` is of type `char`.
integer data type is 4x larger (4 byte on my machine) than
the char data type, I initialize the char element but at
the same time I overwrite the 3 subsequent char array elements
by the remaining 3 integer bytes. Moreover, when initializing
the last array element, I write 3 bytes beyond the array ranges
which might corrupt some other values stored in memory.
Is this right?



No. Whichever way you look at it.


Vladimir S. Oka:

Christian Christmann:


{
char i; [...] I''m initializing the char array ''global'' with
integer values (represented by ''i''). But since the



No, you''re not. Your `i` is of type `char`.



Which is an integer, see 6.2.5 Types - 4, 6 and 7.

integer data type is 4x larger (4 byte on my machine) than
the char data type, I initialize the char element but at
the same time I overwrite the 3 subsequent char array elements
by the remaining 3 integer bytes. Moreover, when initializing
the last array element, I write 3 bytes beyond the array ranges
which might corrupt some other values stored in memory.
Is this right?



No. Whichever way you look at it.



Right, not even if i were of the integer type int. :-)

Jirka


Christian Christmann wrote:

Hi,

I''m wondering if my small example is not
"dangerous":

#define SIZE 10
char global[SIZE];
char* globalPtr = global;

int main()
{
char i;

for ( i = 0; i < SIZE; i = i + 1 ) {
(*globalPtr) = i;
globalPtr++;
}
}

I''m initializing the char array ''global'' with
integer values (represented by ''i''). But since the
integer data type is 4x larger (4 byte on my machine) than
the char data type, I initialize the char element but at
the same time I overwrite the 3 subsequent char array elements
by the remaining 3 integer bytes. Moreover, when initializing
the last array element, I write 3 bytes beyond the array ranges
which might corrupt some other values stored in memory.
Is this right?



Say you altered your code - I''ve changed i from char to a long int - so
that, in the loop, the value assigned to *globalPtr is /more/ obviously
larger than a char.
#define SIZE 10

char global[SIZE];

char * globalPtr = global;

int main(void)
{
long int i;

for(i = 100000; i < 100009; ++i)
{
*globalPtr = i;

globalPtr++;
}

return 0;
}

On the first iteration, the value of i in binary is 11000011010100000, and
assigning that to a char will result *not* in some memory overwrite, but a
loss of data, e.g. the char at address globalPtr would be perhaps have the
value 10100000, i.e., it got just CHAR_BITs worth of the long.
--
==============
Not a pedant
==============


这篇关于通过指针访问数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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