2

For example:

char mem[100000];

int reg[8];

mem[36] = 'p';         // add char p to our 36th index of our char array

reg[3] = (int)mem[36]; // store value of mem[36] into reg[3]
  • Now I want to print the char value at index 3 of that int array.

  • So far my thought process has lead me to code such as this:

    char *c = (char*)reg[3];

    cout << *c << endl;

But I am still getting weird values and characters when trying to print it out.

From my understanding, an integer is equal to 4 characters. Since a character is technically a byte and an integer is 4 bytes.

So I am storing a character into my integer array as 4 bytes, but when I pull it out, there is garbage data since the character I inserted is only one byte compared to the index being 4 bytes in size.

2
  • Have you tried simply doing cout << reg[3] << endl;? Commented Oct 7, 2017 at 2:11
  • Yes, and instead of the char 'p' being printed, a long integer is printed. This is due to garbage data in my index, since a character is technically only 1 byte, and an integer is 4 bytes. Commented Oct 7, 2017 at 2:14

3 Answers 3

1

Have you tried this:

char mem[100000];
int reg[8];
mem[36] = 'p';         // add char p to our 36th index of our char array
reg[3] = (int)mem[36]; // store value of mem[36] into reg[3]
char txt[16];
sprintf(txt, "%c", reg[3]);  // assigns the value as a char to txt array
cout<<txt<<endl;

This prints out the value 'p'

Sign up to request clarification or add additional context in comments.

Comments

0

You shouldn't be using pointers here; it's sufficient to work with chars:

char c = reg[3];
cout << c << endl;

Note, however, that you could lose information when trying to stuff an int into a char variable.

5 Comments

Your solution is correct. But my problem is that I am not directly adding a character to my array. I was trying to show a simplified example. In reality I am adding a character from a separate char array into this int array. So I am getting an error that says "char* cannot be assigned to int." So the only way to make it work, is to cast it to an integer, which gives it a different representation in the array. And because of this casting, I am having troubles grabbing the character back out of the array.
Show us how you're adding the character from that other char array. You might be trying to assign the whole array to a single index in the int array.
Your "char* cannot be assigned to int" issue is solved if you use chars instead of char *s as given in the solution. Even with your additional code, using a char seems to solve your issue. Is there something I might be misunderstanding here?
The "char*" in my error is referring to my char array. I am taking an element from my char array (char*) and inserting it into my int array. That is why I am getting the "char* cannot be assigned to an int" error. So the only way I can insert something from my character array into my integer array is to cast the value into an integer.
(continued...) Now if I want to print out what I just inserted into my integer array, it gives me a weird representation. And that is because I am storing a 1-byte character into an index that is 4-bytes long (since it is an integer array). So I just can not figure out how to print out the character representation of what I just inserted.
0

I do not see what is your problem. You store the char into int var. You want to print it back - just cast the value to char and print it

#include <iostream>

int main()
{
    char mem[100];

    int reg[8];

    mem[36] = 'p';         // add char p to our 36th index of our char array

    // store value of mem[36] into reg[3]
    reg[3] = mem[36]; 

    // store value of mem[36] into reg[4] with cast
    reg[4] = static_cast<int>(mem[36]); 

    std::cout << static_cast<char>(reg[3]) << '\n';
    std::cout << static_cast<char>(reg[4]) << '\n';

}

/****************
 * Output
$ ./test
p
p
*/

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.