C strcpy = Unhandled exception: Access violation writing location 0x00000000

前端 未结 2 1359
没有蜡笔的小新
没有蜡笔的小新 2021-01-21 14:51

I have a problem with strcpy function. Using C. Main point of this simple code (below) is copying a string from a array to the array of pointers.

char string[20]         


        
2条回答
  •  失恋的感觉
    2021-01-21 15:26

    The target buffer has not been initialized. array_of_pointers[0] is just a pointer that (in this case based on the error information from the access violation) points to address 0. You need to initialize it. Possibly:

    array_of_pointers[0] = malloc( strlen( string ) + 1 );
    

    array_of_pointers is an array of 20 pointers. Defined like that, each entry in that array must be initialized before it can be used. Remember too that if you do use malloc (or possibly strdup) to allocate the memory, use free to release the memory.

提交回复
热议问题