Pointer to 2D arrays in C

后端 未结 4 2074
南笙
南笙 2020-11-28 04:01

I know there is several questions about that which gives good (and working) solutions, but none IMHO which says clearly what is the best way to achieve this. So, suppose we

4条回答
  •  执笔经年
    2020-11-28 04:09

    int *pointer[280]; //Creates 280 pointers of type int.

    In 32 bit os, 4 bytes for each pointer. so 4 * 280 = 1120 bytes.

    int (*pointer)[100][280]; // Creates only one pointer which is used to point an array of [100][280] ints.

    Here only 4 bytes.

    Coming to your question, int (*pointer)[280]; and int (*pointer)[100][280]; are different though it points to same 2D array of [100][280].

    Because if int (*pointer)[280]; is incremented, then it will points to next 1D array, but where as int (*pointer)[100][280]; crosses the whole 2D array and points to next byte. Accessing that byte may cause problem if that memory doen't belongs to your process.

提交回复
热议问题