#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE_OF_RECORD 100
void main()
{
char lineBuffer[SIZE_OF_RECORD];
memset(lineBuffer, ' ', (SIZE_OF_RECORD - 1) * sizeof(char));
strcpy(lineBuffer, "testing"); strcat(lineBuffer, ','); //exception thrown here Exception thrown at 0x00007FFCB7B5D1CB (ucrtbased.dll) in PA_2.exe: 0xC0000005: Access violation reading location 0x000000000000002C.
printf("line buffer after assemble line n%s", lineBuffer);
}
我不明白。声明的字符串不应该是只读的,对吗?为什么我不能更改它?
函数strcat
需要两个char*类型的参数,但调用它时传递的是int 类型的对象
strcat(lineBuffer, ',');
函数strcat
的声明类似
char *strcat(char * restrict s1, const char * restrict s2);
至少写入
strcat(lineBuffer, ",");
使用字符串文字CCD_ 3而不是整数字符常量。
此外,还不清楚为什么在这次通话中
memset(lineBuffer, ' ', (SIZE_OF_RECORD - 1) * sizeof(char));
您使用的是表达式(SIZE_OF_RECORD-1(而不是SIZE_OF-RECORD。你可以写
memset(lineBuffer, ' ', sizeof(lineBuffer ));