C : Access violation reading location 0x00000044

-1

I have a file with heading [what],[where],[who] and I put it into an array list. I am trying to concatenate the bracket and the array of words together. like [ what ] but error will pop up.

" Exception thrown at 0x0F20EDD0 (ucrtbased.dll) in project.exe: 0xC0000005: Access violation reading location 0x00000044 "

char intent[255];
char inv[3][10] = { "what","where","who" };

//char stringCat = strncat(intent, inv[0],10);
snprintf(intent, sizeof(intent), "[%s]", inv[0]);

can I know why does this happen?

c
arrays
string
concat
asked on Stack Overflow Nov 21, 2019 by kelly • edited Nov 21, 2019 by kelly

1 Answer

0

intent[0] is a char, not a char *. strcat will append to the end of the char * pointed to in the first argument, so you are getting an error. Changing to strcat(intent, inv[0]) will fix this immediate problem, but the code will still be wrong.

You need the string to be nil-terminated, so you need intent[1] = '\0' prior to the strcat().

strcat also simply writes to the end of intent, so stringCat and totalCat are pointless variables--they simply point to intent.

You would also do better to use strncat() than strcat() to prevent buffer overruns.

answered on Stack Overflow Nov 21, 2019 by Jabberwock

User contributions licensed under CC BY-SA 3.0