Guys I am new to programming and was just playing around with some functions, I wanted to test the
char *psz = "Hello world!";
memcpy(psz, dst, sizeof(psz));
Your first two parameter values of
memcpy() are backwards. The first parameter of
memcpy() is the destination, the second parameter is the source. You are trying to copy a local (uninitialized) buffer into the memory of a string literal. A string literal is stored in read-only memory, which is why you are getting a runtime error.
Also, your use a
sizeof() is wrong, too.
psz is declared as a pointer. The size of a pointer is 4 bytes on a 32bit system, and 8 bytes on a 64bit system. If you reverse your buffer parameters, on a 32bit system you would copy
Hell, and on a 64bit system you would copy
I suspect you intended to do the following instead:
memcpy(dst, psz, strlen(psz)+1);
char psz = "Hello world!"; char dst; memcpy(dst, psz, sizeof(psz));