#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE_OF_RECORD 100
void main()
{
char lineBuffer[SIZE_OF_RECORD];
memset(lineBuffer, '\0', (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);
}
我不明白。宣告的字串不應該是只讀的嗎?為什么我不能改變它?
uj5u.com熱心網友回復:
該函式strcat需要兩個 char * 型別的引數,但您呼叫它時傳遞了一個 int 型別的物件
strcat(lineBuffer, ',');
該函式strcat宣告為
char *strcat(char * restrict s1, const char * restrict s2);
至少寫
strcat(lineBuffer, ",");
使用字串文字","而不是整數字符常量。
也不清楚為什么在這個電話中
memset(lineBuffer, '\0', (SIZE_OF_RECORD - 1) * sizeof(char));
您正在使用運算式 (SIZE_OF_RECORD - 1) 而不是 SIZE_OF_RECORD。你可以寫
memset(lineBuffer, '\0', sizeof(lineBuffer ));
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/512979.html
標籤:C例外字符c字符串字符串
下一篇:laravel錯誤:InvalidArgumentException:index.php中的Request::capture()
