简体   繁体   English

包含指针的结构上的指针:数组命名法有效但不是 (ptr + 1)。 为什么?

[英]Pointer on a struct containing a pointer : The array nomenclature works but not (ptr + 1). Why?

I have a hard time formulating my question, so I used onlinegdb to create an example.我很难提出我的问题,所以我使用 onlinegdb 创建了一个示例。


#include <stdio.h>
#include <stdlib.h>
#include <string.h>


typedef struct
{
    unsigned int GlobalHeader;
    unsigned char * Data;
}GLOBAL_DEVICE;

GLOBAL_DEVICE * GlobalDevice;

int main()
{
    GLOBAL_DEVICE GlobalTest[2];
    
    GlobalDevice = GlobalTest;
    
    char Buffer[] = {"ABCDEFG"};
    char TestChar;
    
    (GlobalDevice + 1)->Data = (char *)malloc(8 * sizeof(char));
    
    memcpy((GlobalDevice + 1)->Data,Buffer,8);
    
    //TestChar = (GlobalDevice + 1)->(Data + 1); <- This doesn't work. Error : expected identifier before '('
    TestChar = (GlobalDevice + 1)->Data[1]; // This works
    
    printf(&TestChar);
    
    return 0;
}

As described in the example above, why can I use (ptr+1) nomenclature on GlobalDevice pointer, but not on the Data pointer contained in the GlobalDevice pointed structure.如上例所述,为什么我可以在 GlobalDevice 指针上使用 (ptr+1) 命名法,但不能在 GlobalDevice 指向的结构中包含的 Data 指针上使用。 I can use the array notation though for the Data pointer with no error.我可以将数组表示法用于 Data 指针而没有错误。

My understanding is that the nomenclature array[i] is equal to *(array + i) and that works with GlobalDevice pointer.我的理解是命名数组 [i] 等于 *(array + i) 并且适用于 GlobalDevice 指针。

So why can't I write那为什么我不能写

GlobalDevice[1]->Data[1]

using the (ptr + 1 nomenclature)使用 (ptr + 1 命名法)

(GlobalDevice + 1)->(Data + 1)

Thanks,谢谢,

Jean-Francois让-弗朗索瓦

edit: For other people reading this question, my misunderstanding came from the fact that I thought that array[i] was equal to (array+i).编辑:对于阅读这个问题的其他人,我的误解来自于我认为 array[i] 等于 (array+i) 的事实。 That is a mistake and array[i] is equal to *(array+i).这是一个错误,array[i] 等于 *(array+i)。

Use ((GlobalDevice + 1)->Data) + 1 , or even just (GlobalDevice + 1)->Data + 1 .使用((GlobalDevice + 1)->Data) + 1 ,甚至只是(GlobalDevice + 1)->Data + 1

Data isn't itself a pointer, it's a member name, and makes no sense as an expression by itself except on the right side of . Data本身不是一个指针,它是一个成员名,除了在. or -> .-> In particular it makes no sense as an operand to + .特别是它作为+的操作数毫无意义。

A correction: array[i] is equivalent, not to array+i , but to *(array+i) .更正: array[i]等效于,而不是array+i ,而是*(array+i) So the correct equivalent of (GlobalDevice + 1)->Data[1] is *((GlobalDevice + 1)->Data + 1) .所以(GlobalDevice + 1)->Data[1]的正确等价物是*((GlobalDevice + 1)->Data + 1)

I think the [] syntax is clearer here anyhow, in both places.无论如何,我认为[]语法在这两个地方都更清晰。 I'd write我会写

TestChar = GlobalDevice[1].Data[1];

(By the way, printf(&TestChar) at the end won't work, because TestChar isn't followed in memory by a null character, so &TestChar isn't a string. Instead write putchar(TestChar); or printf("%c", TestChar); .) (顺便说一下,最后的printf(&TestChar)不起作用,因为在TestChar中没有跟随 null 字符,所以&TestChar不是字符串。而是写putchar(TestChar);printf("%c", TestChar); .)

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

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