memcpy(), what should the value of the size parameter be?

前端 未结 12 2157
灰色年华
灰色年华 2020-12-09 09:36

I want to copy an int array to another int array. They use the same define for length so they\'ll always be of the same length.

What are the pros/cons of the followi

12条回答
  •  既然无缘
    2020-12-09 10:09

    It depends. Both arr and pointer are arrays, but sizeof() returns only the correct size for arr, which is declared at compile time.

    int main() {
            int arr[10];
            int * pointer;
            pointer = (int *) malloc(10 * sizeof(int));
            printf("%d\n", sizeof(arr)); // 40
            printf("%d\n", sizeof(pointer)); // 4 or 8
            free(pointer);
    }
    

提交回复
热议问题