简体   繁体   English

C程序返回0,而指针指向值3

[英]C programme return 0 while the pointer point to value 3

I thought at the end of the programme, il will print 3 but in fact it is 0. 我以为程序结束时il将打印3,但实际上它是0。

Can anyone explain how it is happening ? 谁能解释这是怎么回事?

Here is the code : 这是代码:

int arr[3] = {2, 3, 4};
char *p;
p = arr;
p = (char*)((int*)(p)); // two conversion, means nothing, omit
printf("%d, ", *p);     // point to 2, 
p = (int*)(p+1);  // point to 3 ?
printf("%d", *p);
return 0;

You are treating an array of integers like an array of chars. 您正在将整数数组视为char数组。 The line that is marked as "means nothing" actually means a lot. 标记为“没有任何意义”的行实际上意味着很多。

When you increment char* p by one, you shift your pointer one byte, but in order to point to the arr[1] you need to increment it sizeof(int) bytes. 当将char* p递增1时,会将指针移动1个字节,但是要指向arr[1] ,则需要将其sizeof(int)个字节递增。 Declare p as int* p , then p+1 will do what you need. p声明为int* p ,然后p+1将完成您需要的操作。

Since p is of type char, p+1 points to one byte past the start of the first integer. 由于p为char类型,因此p+1指向第一个整数的开头之后的一个字节。 So when you read one byte by dereferencing p, you are reading a position in the integer array that contains a zero byte. 因此,当通过解引用p读取一个字节时,您正在读取包含零字节的整数数组中的位置。

If you increment a pointer, it is incremented by its type's size. 如果增加一个指针,则该指针将增加其类型的大小。 Here p is character pointer, and If you increment p it points to next character. 这里p是字符指针,如果增加p则指向下一个字符。 As character is of 1 byte, p+1 points to next byte to that pointed by p . 由于字符为1个字节,因此p+1指向由p指向的下一个字节。 If p had been an integer pointer, p+1 would point to next integer i,e 3 in your code. 如果p是一个整数指针,则p+1将指向代码中的下一个整数i,e 3。

What's going on? 这是怎么回事?

The following lines print char content (here *p ) in int format . 以下各行以int 格式打印char 内容 (此处为*p )。

char *p;
printf("%d, ", *p);

It prints 2 because the (current) machine is little-endian (eg x86). 因为(当前)机器是低端字节序(例如x86),所以它打印2 If this code is run on a big-endian machine, it would print 0 . 如果此代码在big-endian机器上运行,则将输出0 [For endian-ness, see http://en.wikipedia.org/wiki/Endianness ] [有关字节序,请参阅http://en.wikipedia.org/wiki/Endianness ]

Exercise: If the array initialization is changed to the following, what is the expected print output? 练习:如果将数组初始化更改为以下内容,则预期的打印输出是什么?

int arr[3] = {0x1a2b3c4d, 3, 4};

Pointer increments 指针增量

Let's say we have 假设我们有

char * pc = 0xA0;
int  * pi = 0xB0;

pc += 1;    // pc = ?
pi += 1:    // pi = ?

What are the new values of pc and pi ? pcpi的新值是多少?

Since pc is a pointer to char , the + 1 increments pc by sizeof(char) , which is 1. 由于pc是指向char的指针,因此+ 1 pc sizeof(char) ,即1。

However, since pi is a pointer to int , the + 1 increments pi by sizeof(int) . 但是,由于pi是指向int的指针,因此+ 1 pi增加sizeof(int) In 32 bit system, sizeof(int) is typically 4, whereas in a 64 bit system, sizeof(int) is typically 8. 在32位系统中, sizeof(int)通常为4,而在64位系统中, sizeof(int)通常为8。

pc += 1;    // pc = 0xA1
pi += 1:    // pi = perhaps 0xB4 or 0xB8

Word of Caution 警示语

It is okay to experiment for knowledge and curiosity sake, but in real code, accessing the bytes of an int by casting it to char * is not a good idea, it leads to all sorts of endian-portability issues. 出于知识和好奇心的考虑可以进行实验,但是在实际代码中,通过将int的字节转换为char *来访问int的字节不是一个好主意,它会导致各种字节序可移植性问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM