通过char *指针访问void * buffer / array [英] Accessing void * buffer/array through char * pointer

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

问题描述

此代码


#include< stdio.h>


int main(无效)

{

int hello [] = {''h'','e'',''l'','l'',''o''};

char * p =(void *)hello;


for(size_t i = 0; i< sizeof(hello); ++ i){

printf(" byte%2zu:<%c>",i,p [i]);

if(!p [i])

printf("(null char)");

printf(" \ n");

}


返回0;

}


产生此输出


字节0:< h>

字节1:<(null char)

字节2:<(null char)

字节3:<(null char)

字节4:< e>

字节5:<(null char)

字节6:<(null char)

字节7:<(null char)

字节8:< l>

字节9:<(null char)

字节10:<(null char)

字节11:<(null char)

字节12:< l>

字节13:<(null char)

字节14:<(null char)

字节15:<(null char)

字节16:< o>

字节17:<(null char)

byte 18:<(null char)

byte 19:<(null char)


我对int * -to-void感到困惑* - char *转换。

输出显示我的机器上的int是四个字节,而初始值为'h'','e'','l'的值为
','''',''o''的值分别为104,101,108,

108和111,因此它们可以表示为字符

(呃...)。但是,如果我将初始化程序更改为类似


int hello [] = {1000,436676,362,6364,2575};


我会得到这个输出


字节0:< ???>

字节1:<>

byte 2:<(null char)

byte 3:<(null char)

byte 4:< ???>

字节5:< ???>

字节6:<(null char)

字节7:<(null char )

字节8:< j>

字节9:<>

字节10:<(null char)

字节11:<(null char)

字节12:< ???>

字节13:< a ??> ;

字节14:<(null char)

字节15:<(null char)

字节16:<>

字节17:<

This code

#include <stdio.h>

int main(void)
{
int hello[] = {''h'', ''e'', ''l'', ''l'', ''o''};
char *p = (void *) hello;

for (size_t i = 0; i < sizeof(hello); ++i) {
printf("byte %2zu: <%c>", i, p[i]);
if (!p[i])
printf(" (null char)");
printf("\n");
}

return 0;
}

produces this output

byte 0: <h>
byte 1: <(null char)
byte 2: <(null char)
byte 3: <(null char)
byte 4: <e>
byte 5: <(null char)
byte 6: <(null char)
byte 7: <(null char)
byte 8: <l>
byte 9: <(null char)
byte 10: <(null char)
byte 11: <(null char)
byte 12: <l>
byte 13: <(null char)
byte 14: <(null char)
byte 15: <(null char)
byte 16: <o>
byte 17: <(null char)
byte 18: <(null char)
byte 19: <(null char)

I''m confused about the int *-to-void *-to char * conversion. The
output shows that ints are four bytes on my machine, and the values in
the initializer ''h'', ''e'', ''l'', ''l'', ''o'' have the values 104, 101, 108,
108, and 111, respectively, so they''re able to be represented as chars
(duh...). But if I''d change the initializer to something like

int hello[] = {1000, 43676, 362, 6364, 2575};

I''d get this output

byte 0: <???>
byte 1: <>
byte 2: <(null char)
byte 3: <(null char)
byte 4: <???>
byte 5: <???>
byte 6: <(null char)
byte 7: <(null char)
byte 8: <j>
byte 9: <>
byte 10: <(null char)
byte 11: <(null char)
byte 12: <???>
byte 13: <a??>
byte 14: <(null char)
byte 15: <(null char)
byte 16: <>
byte 17: <


>
>



字节18:<(null char)

字节19:<(null char)


(换句话说,不可打印的字符。)


当我下标char指针时会发生某种溢出吗?

或者是我只是因为访问了一个char
指针指向一个不是char对象的东西而变得毫无意义的值?


Sebastian

byte 18: <(null char)
byte 19: <(null char)

(In other words, non-printable characters.)

Is some kind of overflow happening when I subscript the char pointer?
Or am I simply getting meaningless values because of accessing a char
pointer that points to something that wasn''t a char object?

Sebastian

推荐答案

s0****@gmail.com 写道:
s0****@gmail.com writes:

#include< stdio.h>


int main(无效)

{

int hello [] = {''h'','e'',''l'','l'',''o''};

char * p =(void *)hello;


for(size_t i = 0; i< sizeof(hello); ++ i){

printf(" byte%2zu:<%c>",i,p [i]);

if(!p [i])

printf("(null char)");

printf(" \ n");

}


返回0;

}
#include <stdio.h>

int main(void)
{
int hello[] = {''h'', ''e'', ''l'', ''l'', ''o''};
char *p = (void *) hello;

for (size_t i = 0; i < sizeof(hello); ++i) {
printf("byte %2zu: <%c>", i, p[i]);
if (!p[i])
printf(" (null char)");
printf("\n");
}

return 0;
}



< snip>

<snip>


我对int * -to-void * -to char *转换感到困惑。

输出显示我的机器上的int是四个字节,而初始值为'h'','e'','l'的值为
','''',''o''的值分别为104,101,108,

108和111,因此它们可以表示为字符

(呃...)。但是,如果我将初始化程序更改为类似


int hello [] = {1000,436676,362,6364,2575};


我会得到这个输出


字节0:< ???>

字节1:<>

byte 2:<(null char)

byte 3:<(null char)

byte 4:< ???>

字节5:< ???>

字节6:<(null char)

字节7:<(null char )
I''m confused about the int *-to-void *-to char * conversion. The
output shows that ints are four bytes on my machine, and the values in
the initializer ''h'', ''e'', ''l'', ''l'', ''o'' have the values 104, 101, 108,
108, and 111, respectively, so they''re able to be represented as chars
(duh...). But if I''d change the initializer to something like

int hello[] = {1000, 43676, 362, 6364, 2575};

I''d get this output

byte 0: <???>
byte 1: <>
byte 2: <(null char)
byte 3: <(null char)
byte 4: <???>
byte 5: <???>
byte 6: <(null char)
byte 7: <(null char)



< snip>

<snip>


(换句话说,不可打印的字符。)


当我下标char时会发生某种溢出

指针?
(In other words, non-printable characters.)

Is some kind of overflow happening when I subscript the char
pointer?



不,访问时没有溢出。

No, no overflow is happening on access.


或者我只是因为访问一个char

指针指向不是char对象的东西?
Or am I simply getting meaningless values because of accessing a char
pointer that points to something that wasn''t a char object?



首先,它们并非毫无意义。某些过程完全控制着你看到的是什么,但是因为它可能涉及终端设置这样的事情

可能是一个非常复杂的过程。


其次,char指针始终指向char对象。 C不会对
强制赋值,但可以访问任何类型的任何对象,就好像它是一个char对象序列。在你的情况下,前两个字符

几乎肯定是1000%256和1000/256,即最小和

二进制表示的第二个最低有效字节1000。 br />

我知道这不是一个真正的答案,但你的任何一个/或者问题

不给我很大的空间!

-

Ben。

First, they are not meaningless. Some process governs exactly what
you see but since it may involve things like the terminal setting it
can be a very complex one.

Secondly, a char pointer always points at a char object. C does not
mandate the value but any object of any type can be accessed as if it
is a sequence of char objects. In your case the first two characters
are almost certainly 1000 % 256 and 1000 / 256, i.e. the least and
second least significant bytes of the binary representation of 1000.

I know this is not an actual answer, but your either/or questions
don''t give me much room!

--
Ben.


9月6日下午4:26,s0s ... @ gmail.com写道:
On Sep 6, 4:26 pm, s0s...@gmail.com wrote:

此代码


#include< stdio.h>


int main(void)

{

int hello [] = {''h'',''e'','l'','l '',''o''};

char * p =(void *)hello;


for(size_t i = 0; i< sizeof(你好); ++ i){

printf(" byte%2zu:<%c>",i,p [i]);

if(!p [i])

printf("(null char)");

printf(" \\\
" ;);

}


返回0;


}


产生此输出


字节0:< h>

字节1:<(null char)

byte 2:<(null char)

byte 3:<(null char)

byte 4:< e>

byte 5 :<(null char)

byte 6:<(null char)

byte 7:<(null char)

byte 8:< l>

字节9:<(null char)

字节10:<(null char)

byte 11 :<(null char)

byte 12:< l>

byte 13:<(null char)

byte 14: <(null char)

byte 15:<(null char)

byte 16:< o>

byte 17:< ;(null char)

字节18:<(null char)

字节19:<(null char)


我对int * -to-void * -to char *转换感到困惑。

输出显示我的机器上的int是四个字节,而初始值为'h'','e'','l'的值为
','''',''o''的值分别为104,101,108,

108和111,因此它们可以表示为字符

(呃...)。但是,如果我将初始化程序更改为类似


int hello [] = {1000,436676,362,6364,2575};


我会得到这个输出


字节0:< >

字节1:<>

字节2:<(null char)

字节3:<(null char )

字节4:< >

字节5:< >

字节6:<(null char)

字节7:<(null char)

字节8:< j> ;

字节9:<>

字节10:<(null char)

字节11:<(null char)

字节12:< >

字节13:<?>

字节14:<(null char)

字节15:<(null char)

字节16:<>

字节17:<


字节18:<(null char )

字节19:<(null char)


(换句话说,不可打印的字符。)


当我下标char指针时会发生某种溢出吗?

或者我只是因为访问一个字母而得到无意义的值

指针指向某个东西不是char对象吗?


Sebastian
This code

#include <stdio.h>

int main(void)
{
int hello[] = {''h'', ''e'', ''l'', ''l'', ''o''};
char *p = (void *) hello;

for (size_t i = 0; i < sizeof(hello); ++i) {
printf("byte %2zu: <%c>", i, p[i]);
if (!p[i])
printf(" (null char)");
printf("\n");
}

return 0;

}

produces this output

byte 0: <h>
byte 1: <(null char)
byte 2: <(null char)
byte 3: <(null char)
byte 4: <e>
byte 5: <(null char)
byte 6: <(null char)
byte 7: <(null char)
byte 8: <l>
byte 9: <(null char)
byte 10: <(null char)
byte 11: <(null char)
byte 12: <l>
byte 13: <(null char)
byte 14: <(null char)
byte 15: <(null char)
byte 16: <o>
byte 17: <(null char)
byte 18: <(null char)
byte 19: <(null char)

I''m confused about the int *-to-void *-to char * conversion. The
output shows that ints are four bytes on my machine, and the values in
the initializer ''h'', ''e'', ''l'', ''l'', ''o'' have the values 104, 101, 108,
108, and 111, respectively, so they''re able to be represented as chars
(duh...). But if I''d change the initializer to something like

int hello[] = {1000, 43676, 362, 6364, 2575};

I''d get this output

byte 0: < >
byte 1: <>
byte 2: <(null char)
byte 3: <(null char)
byte 4: < >
byte 5: < >
byte 6: <(null char)
byte 7: <(null char)
byte 8: <j>
byte 9: <>
byte 10: <(null char)
byte 11: <(null char)
byte 12: < >
byte 13: <?>
byte 14: <(null char)
byte 15: <(null char)
byte 16: <>
byte 17: <

byte 18: <(null char)
byte 19: <(null char)

(In other words, non-printable characters.)

Is some kind of overflow happening when I subscript the char pointer?
Or am I simply getting meaningless values because of accessing a char
pointer that points to something that wasn''t a char object?

Sebastian



我们的问题与我在此之前的问题相同br />
有chars或int数组,链接器分配已初始化的字节

值加上4 BYTES MORE,其中还包括一个NuLL char''\ 0''

your question is same as i asked here before acutally acutally when we
have array of chars or int the linker allocates bytes of initialized
values plus 4 BYTES MORE which also include a NuLL char ''\0''


9月6日下午4:26,s0s ... @ gmail.com写道:
On Sep 6, 4:26 pm, s0s...@gmail.com wrote:

此代码


#inclu de< stdio.h>


int main(无效)

{

int hello [] = {''h '','''','''','''',''o''};

char * p =(void *)你好;


for(size_t i = 0;我<的sizeof(你好); ++ i){

printf(" byte%2zu:<%c>",i,p [i]);

if(!p [ i])

printf("(null char)");

printf(" \ n");

}


返回0;


}


产生此输出

字节0:< h>

字节1:<(null char)

字节2:<(null char)

byte 3:<(null char)

byte 4:< e>

byte 5:<(null char)

字节6:<(null char)

字节7:<(null char)

字节8:< l>

byte 9:<(null char)

byte 10:<(null char)

byte 11:<(null char)

字节12:< l>

字节13:<(null char)

字节14:<(null char)

byte 15:<(null char)

byte 16:< o>

byte 17:<(null char)

字节18:<(null char)

字节19:&l t;(null char)


我对int * -to-void * -to char *转换感到困惑。

输出显示我的机器上的int是四个字节,而初始值为'h'','e'','l'的值为
','''',''o''的值分别为104,101,108,

108和111,因此它们可以表示为字符

(呃...)。但是,如果我将初始化程序更改为类似


int hello [] = {1000,436676,362,6364,2575};


我会得到这个输出


字节0:< >

字节1:<>

字节2:<(null char)

字节3:<(null char )

字节4:< >

字节5:< >

字节6:<(null char)

字节7:<(null char)

字节8:< j> ;

字节9:<>

字节10:<(null char)

字节11:<(null char)

字节12:< >

字节13:<?>

字节14:<(null char)

字节15:<(null char)

字节16:<>

字节17:<


字节18:<(null char )

字节19:<(null char)


(换句话说,不可打印的字符。)


当我下标char指针时会发生某种溢出吗?

或者我只是因为访问一个字母而得到无意义的值

指针指向某个东西不是char对象吗?


Sebastian
This code

#include <stdio.h>

int main(void)
{
int hello[] = {''h'', ''e'', ''l'', ''l'', ''o''};
char *p = (void *) hello;

for (size_t i = 0; i < sizeof(hello); ++i) {
printf("byte %2zu: <%c>", i, p[i]);
if (!p[i])
printf(" (null char)");
printf("\n");
}

return 0;

}

produces this output

byte 0: <h>
byte 1: <(null char)
byte 2: <(null char)
byte 3: <(null char)
byte 4: <e>
byte 5: <(null char)
byte 6: <(null char)
byte 7: <(null char)
byte 8: <l>
byte 9: <(null char)
byte 10: <(null char)
byte 11: <(null char)
byte 12: <l>
byte 13: <(null char)
byte 14: <(null char)
byte 15: <(null char)
byte 16: <o>
byte 17: <(null char)
byte 18: <(null char)
byte 19: <(null char)

I''m confused about the int *-to-void *-to char * conversion. The
output shows that ints are four bytes on my machine, and the values in
the initializer ''h'', ''e'', ''l'', ''l'', ''o'' have the values 104, 101, 108,
108, and 111, respectively, so they''re able to be represented as chars
(duh...). But if I''d change the initializer to something like

int hello[] = {1000, 43676, 362, 6364, 2575};

I''d get this output

byte 0: < >
byte 1: <>
byte 2: <(null char)
byte 3: <(null char)
byte 4: < >
byte 5: < >
byte 6: <(null char)
byte 7: <(null char)
byte 8: <j>
byte 9: <>
byte 10: <(null char)
byte 11: <(null char)
byte 12: < >
byte 13: <?>
byte 14: <(null char)
byte 15: <(null char)
byte 16: <>
byte 17: <

byte 18: <(null char)
byte 19: <(null char)

(In other words, non-printable characters.)

Is some kind of overflow happening when I subscript the char pointer?
Or am I simply getting meaningless values because of accessing a char
pointer that points to something that wasn''t a char object?

Sebastian



这里是帖子
http:// groups。 google.co.in/group/com...42714e7b3e6c17


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

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