Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Access violation with strcpy in c

#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);
    
}

I do not understand. The string as it is declared should not be read only right? Why cant I change it?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The function strcat expects two arguments of the type char * but you are calling it passing an object of the type int

strcat(lineBuffer, ',');

The function strcat is declared like

char *strcat(char * restrict s1, const char * restrict s2);

At least write

strcat(lineBuffer, ",");

using the string literal "," instead of the integer character constant.

Also it is unclear why in this call

memset(lineBuffer, '\0', (SIZE_OF_RECORD - 1) * sizeof(char));

you are using the expression (SIZE_OF_RECORD – 1) instead of SIZE_OF_RECORD. You could write

memset(lineBuffer, '\0', sizeof(lineBuffer ));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading