Does pointer arithmetic have uses outside of arrays?

后端 未结 7 1978
青春惊慌失措
青春惊慌失措 2020-12-17 15:00

I think I understand the semantics of pointer arithmetic fairly well, but I only ever see examples when dealing with arrays. Does it have any other uses that can\'t be achie

相关标签:
7条回答
  • 2020-12-17 15:25

    Pointer arithmetic is only defined on arrays. Adding an integer to a pointer that does not point to an array element produces undefined behavior.

    0 讨论(0)
  • 2020-12-17 15:27

    From the top of my head I know it's used in XOR linked-lists (very nifty) and I've seen it used in very hacky recursions.

    On the other hand, it's very hard to find uses since according to the standard pointer arithmic is only defined if within the bounds of an array.

    0 讨论(0)
  • 2020-12-17 15:28

    Pointer arithmetic by definition in C happens only on arrays. However, as every object has a representation consisting of an overlaid unsigned char [sizeof object] array, it's also valid to perform pointer arithmetic on this representation. For example:

    struct foo {
        int a, b, c;
    } bar;
    
    /* Equivalent to: bar.c = 1; */
    *(int *)((unsigned char *)&bar + offsetof(struct foo, c)) = 1;
    

    Actually char * would work just as well.

    0 讨论(0)
  • 2020-12-17 15:35

    Here's a case for pointer arithmetic outside of (strictly defined) arrays:

    double d = 0.5;
    unsigned char *bytes = (void *)&d;
    for(size_t i = 0; i < sizeof d; i++)
        printf("Byte %zu of d is %hhu\n", i, bytes[i]);
    

    Why would you do this? I don't know. But if you want to look at the bitwise representation of an object (useful for things like memcpy and memcmp), you'll need to cast their addresses to unsigned char *s (or signed char *s if you like) and work with them byte-by-byte. (If your task isn't too difficult you can even write the code to work word-by-word, which most memcpy implementations will do. It's the same principle, though, just replace char with int32_t.)

    Note that, in the standard, the exact values (or the number of values) that are printed are implementation-defined, but that this will always work as a way to access an object's internal bytewise representation. (It is not required to work for larger integer types, but almost always will - no processor I know of has had trap representations for integers in quite some time).

    0 讨论(0)
  • 2020-12-17 15:36

    a[n] is "just" syntactic sugar for *(a + n). For lulz, try the following

    int a[2];
    0[a] = 10;
    1[a] = 20;
    

    So one could argue that indexing and pointer arithmetic are merely interchangeable syntax.

    0 讨论(0)
  • 2020-12-17 15:37

    In embedded systems, pointers are used to represent addresses or locations. There may not be an array defined. (Although one could say that all of memory is one huge array.)

    For example, a stack (holding variables and addresses) is manipulated by adding or subtracting values from the stack pointer. (In this case, the stack could be said to be an array based stack.)

    0 讨论(0)
提交回复
热议问题