char activeip[11]="0123456789";
char buffer[1001];
int MAX_SIZE = 1000;
printf("MAX_SIZE %d\n", MAX_SIZE);
strncpy(buffer, "string here....... ",MAX_SIZE+1);
printf("MAX_SIZE %d\n", MAX_SIZE);
strncpy(&buffer[strlen(buffer)],activeip,MAX_SIZE+1 );
printf("MAX_SIZE %d\n", MAX_SIZE);
strncpy(&buffer[strlen(buffer)],"Long string here.....................................", MAX_SIZE+1);
printf("MAX_SIZE %d\n", MAX_SIZE);
puts(buffer);
如您所见,我初始化 MAX_SIZE 为 1000。当 MAX_SIZE 不大于缓冲区时,MAX_SIZE 变为零。代码的输出如下:
MAX_SIZE 1000
MAX_SIZE 0
MAX_SIZE 0
string here....... 0123456789L
Process finished with exit code 0
如何将函数(strncpy 更改为我的局部变量(MAX_SIZE)?我的编译器是在 CLion 上运行的 minGW 谢谢您的回答