Anwar Mohamed
Anwar Mohamed

Reputation: 653

C++ change value in const char

I have a const char

const char example[] = "\x4D\x5A\xE8\x00\x00\x00\x00\x5B\x52\x45\x55\x89\xE5\x81\xC3";

and

DWORD* example2 = "\xAA\xBB\xCC\xDD";

and i want to change the last 4 bytes of example1 with those on example2 what can I do in C++?

i have tried memcpy , strcpy and strcpy_s with no luck

Upvotes: 1

Views: 3458

Answers (5)

MOHAMED
MOHAMED

Reputation: 43518

your example[] char array is defined as const so you can not modify it.

1) You should get an eror in the compilation if you change your const char array in this way

example[2] ='R';

2) You should get a warning if you modify your const char array via memcpy or via strcpy

Change it to

char example[] = "\x4D\x5A\xE8\x00\x00\x00\x00\x5B\x52\x45\x55\x89\xE5\x81\xC3";

And you can not use strcpy because your character array contains x00 in the middle so this will affect the strcpy function. Because strcpy stop when it find x00 in the char array

example[] char array contains x00 in the middle, so to find the length of example[] with strlen will not work properly. For this case I suggest to use sizeof(example) instead.

Here after how you can make your copy:

char example[] = "\x4D\x5A\xE8\x00\x00\x00\x00\x5B\x52\x45\x55\x89\xE5\x81\xC3";
DWORD* example2 = "\xAA\xBB\xCC\xDD";

if (sizeof(example)>=sizeof(example2))
     memcpy(example+sizeof(example)-sizeof(example2), example2, sizeof(example2));

Upvotes: 2

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70929

You should never modify a constant including a constant array. If you want to change what you have above, create a copy of it and change the copy. As pointed out by RasmusKaj strcpy will not help you here as the source strings contains zero chars so maybe use memcpy for the creation of the copy.

Upvotes: 0

jmh
jmh

Reputation: 9336

Const variables can't be changed. This is by design. In the case of a c string, you can have the contents of the string const or the pointer to the string const.

Since you are defining it as a const character array, the pointer is implicitely const and the contents are explicitly const.

const char * const mystring = "hello"

In this case the first "const" tries to apply left (there is nothing), so it applies right (to the char type). So the string content may not change. The second const tries to apply left, so it makes the pointer itself const. That means that the mystring pointer must always point to where the "h" from "hello" in memory is.

So afterwards if I try:

mystring[0] = "y"

or

mystring = "gooodbye!"

They would not work. If you removed the first or second const respectively, they could be made to work.

The purpose of const allows you to say ahead of time "this variable cannot be modified". That means that if it is modified then there is a problem. Generally you should always use const with any variable that you do not want to be modified after instantiation.

Upvotes: 0

Aniket Inge
Aniket Inge

Reputation: 25705

  1. Donot modify a constant string.
  2. const char example[] = "\x4D\x5A\xE8\x00\x00\x00\x00\x5B\x52\x45\x55\x89\xE5\x81\xC3"; here, your string has a few NULL string terminator. This will NOT work with functions in <string.h> (such as strlen() and others)
  3. Instead use memcpy, memset functions to append ONLY after knowing the length of the binary string.
  4. Store your result in a character array, but don't assume it will work as a regular string because of your data.

Upvotes: 3

Alok Save
Alok Save

Reputation: 206536

You should not modify a constant array!
Modifying a inherently constant object/variable leads to Undefined Behavior.
Just don't do it. Make a copy of it and modify that copy or if you want to modify the same array simply don't declare it as const.

Upvotes: 9

Related Questions