이것은 처음으로 여기에 게시됩니다. 희망을 갖고 나 자신을 바보로 만들지 않을 것입니다.C : 포인터에 대한 포인터를 사용하여 메모리를 할당하기위한 적절한 구문
메모리에 포인터를 할당하고 버퍼에 텍스트를 복사 한 다음 문자를 변경하는 함수를 사용하려고합니다. 나는 계속 segfault를 얻고 답을 찾으려고 노력했다. 나의 구문은 틀렸고, 나는 깨달음을 사용할 수 있었다.
/* My objective is to pass a buffer to my Copy function, allocate room, and copy text to it. Then I want to modify the text and print it.*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int Copy(char **Buffer, char *Text);
int main()
{
char *Text = malloc(sizeof(char) * 100);
char *Buffer;
strncpy(Text, "1234567890\n", 100);
Copy(&Buffer, Text);
}
int Copy(char **Buffer, char *Text)
{
int count;
count = strlen(Text)+1;
*Buffer = malloc(sizeof(char) * count);
strncpy(*Buffer, Text, 5);
*Buffer[2] = 'A'; /* This results in a segfault. "*Buffer[1] = 'A';" results in no differece in the output. */
printf("%s\n", *Buffer);
}
고맙습니다. 다른 모든 사람들에게 동일합니다. :) – user359531