Array designation with pointer designation C

Is there any advantage to using array notation pointer notation notation? I understand that there may be some special cases where pointer notation is better, but it seems to me that array notation is clearer. My professor told us that he prefers the designation of the pointer "because it is C", but that is not what he will label. And I know that there are differences with declaring strings as arrays of characters and declaring a pointer as strings - I'm just talking about a general loop through an array.

+4
source share
2 answers

If you write a simple loop, the array and pointer forms are usually compiled into the same machine code.

There are differences in the especially non-constant loop exit conditions, but this only matters if you are trying to optimize the loop for a particular compiler and architecture.

So, what about an example of a real world that builds on both?

These types implement a dynamically determined double-precision floating-point matrix with a separate data store with counting:

struct owner {
    long          refcount;
    size_t        size;
    double        data[];    /* C99 flexible array member */
};

struct matrix {
    long          rows;
    long          cols;
    long          rowstep;
    long          colstep;
    double       *origin;
    struct owner *owner;
};

, , struct matrix. struct owner C99. , , "" . : , , ( ).

, , , , . , , . , refcount . , , "" , (), .

; .

struct matrix m, r, c, 0 <= r < m.rows 0 <= c < m.cols, m.origin[r*m.rowstep + c*m.colstep].

, m.rows m.cols m.rowstep m.colstep. , , - , ( ).

( , origin , 0, 0, rowstep colstep . "" , ..)

C99 - , , data . ( ). , data , , , , .

- , ( , ), - , , , , . , ,

#define MATRIXELEM(m, r, c)  ((m).origin[(r)*(m).rowstep + (c)*(m).colstep])

, , , m. ( , MATRIXELEM(m++,0,0) m .) m struct matrix, . , ,

struct matrix m1, m2;

/* Stuff that initializes m1 and m2, and makes sure they point
   to valid matrix data */

MATRIXELEM(m1, 0, 0) = MATRIXELEM(m2, 0, 0);

"" , , i + 4*j , ((i + 4*j)*m.rowstep, i + 4*j*m.rowstep). "". , , "" , , . ( , " " , , , "" , .)

, , : , , , . "Foo"[1] 'o', *("Foo"+1) . ( , 1["foo"], C .)

, , ; , , - , , , , , . , C, .

+4

, , , C, . , -, , -, ( ) . , , (, std::vector::begin() ++), , . . , , , .

, , . -, . . - , .

+2

Source: https://habr.com/ru/post/1668146/


All Articles