Initializing array of integer pointer in C

前端 未结 3 1914
无人及你
无人及你 2020-12-16 01:13

I have some confusions/problems about the usage of pointers in C. I\'ve put the example code below to understand it easily. Please notice differences of these codes. If you

3条回答
  •  感动是毒
    2020-12-16 01:54

    When you say: ptr = {1,2,3,4,5}, you make ptr point to a memory in the data segment, where constant array {1,2,3,4,5} resides and thus you are leaking memory. If you want to initialize your memory, just after allocation, write: ptr[0]=1; ptr[1]=2; and so on. If you want to copy data, use memcpy.

提交回复
热议问题