0

I have an int8 array essentially declared as a BYTE array at the top of my program.

I want to copy the contents of this array into char array for processing. The content in my byte array is as follows:

byte_array[0] = "A";
byte_array[1] = "Q";
byte_array[2] = "W";
byte_array[3] = "E";
byte_array[4] = "R";
byte_array[5] = "T";
byte_array[6] = "Y";
byte_array[0] = "Z";
byte_array[1] = ".";
byte_array[2] = ".";
byte_array[3] = ".";

And my code is as follows:

char char_array[];

for (j = 0; j < byte_array_size; j++) {
    char_array = &byte_array[j];

    printf("char_array[j]: %c - j: %u\n\r", char_array[j], j);
            }

Note: j and byte_array_size are declared at the top of my program and are intialised with 0 and 10 respectively. byte_array has been populated as mentioned at the start of the post.

The printf above is essentially printing out spurious characters rather than:

char_array[0] = "A";
char_array[1] = "Q";
char_array[2] = "W";
char_array[3] = "E";
char_array[4] = "R";
char_array[5] = "T";
char_array[6] = "Y";
char_array[0] = "Z";
char_array[1] = ".";
char_array[2] = ".";
char_array[3] = ".";
2
  • 2
    char literal should be 'A' instead of "A". ditto. Commented Feb 25, 2014 at 11:09
  • check if you have initiated your chars correctly or you just store the ls byte of the pointer to random strings Commented Feb 25, 2014 at 12:33

2 Answers 2

3

Here it is:

char char_array[char_array_size];

for (j = 0; j < byte_array_size && j < char_array_size; j++) {
    char_array[j] = byte_array[j];
}
Sign up to request clarification or add additional context in comments.

Comments

0

char_array = &byte_array[j]; you are copying the addresses , to copy the values u should char_array[j] = byte_array[j];

also you should allocate the char_array if you don't know it's size use malloc if you do know it use char char_array[YOUR_SIZE];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.