Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I reinterpret cast from double to char* (I need it to store the data of double in file in bytes). Below is the code and I don't know why it doesn't work:
#include <iostream>
int main(int argc, char **argv)
{
const double tmpDouble = 1234.;
char *tmpChar = reinterpret_cast<char*>(tmpDouble);
return 0;
}
If what you had there worked, it probably wouldn't be what you want - the pointer's value would just be 1234 - effectively pointing to that address which might contain anything (not that it's accessible).
If you just want to have the double in binary format, you could do
const byte* pDouble = reinterpret_cast<const byte*>(&tmpDouble);
// |
// note the address here
But first check whatever it is you're using to write to file for a function prototype that takes a double directly.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
When I write a function like below and use it in main(), it doesn't work.
Why does this happen? (I am a beginner).
void addBst(char *name, char *num);
int main(void)
{
addBst("a", "b");
return 0;
}
In C++, a string literal is a const char[N] array, where N is the length of the string, including the null terminator.
Since C++11, it is illegal to assign a string literal to a non-const char* pointer, as your code is doing. You need to use a const char* pointer instead, eg:
void addBst(const char *name, const char *num);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to find a way to convert an int to char of the corresponding value of the int (assuming int is one digit). (example 1='1' 5='5' 9='9') I've tried
int a=5;
char b=char(a+48);
whenever I try to run this the program crashes. How can I set up a system that works correctly?
It can be done using the following code:
char c = (char)(48 + a);
You can also use the '0' char value, instead of 48. It will improve code readability and let you not remember the value 48:
int a = 5;
char c = (char)((int)'0' + a);
As mentioned in comments, you can do this without explicit casts:
char c = '0' + a;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am running the below code. I am getting run time error.
#include <iostream>
using namespace std;
int main() {
char *p="hello";
//p="Hi";
*p='G';
cout<<*p;
return 0;
}
if this is giving error then what is use of const char *p="hello";In this case my string should be constant not in char *p="hello"
char *p="hello";
*p='G';
You make p point to a constant, "hello". But then you try to modify what p points to. By definition, constants cannot be modified.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'd like to write a function that will return 4 members of a class. They are 3 ints and char, and I'd like to store them all in one vector and return it from a function call. Can I do that?
You either need an std::tuple if you want to preserve the types and if the result length is constant, or just cast all members to some common supertype and store them in a container.
You need a class:
struct S
{
int a, b, c;
char letter;
};
int main()
{
S s;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For example:
string str[3];
void foo(char** str)
{
//do something to str...
}
How to pass str[] to function foo in a convenient way?
The function expects an array of pointers, so you'll have to make one from your array of strings:
std::vector<char*> pointers;
for (auto & s : str) {
pointers.push_back(&s[0]);
}
foo(&pointers[0]);
Beware that this may not be valid if the function modifies the pointers, or the strings they point to. A better option would be to avoid mixing C and C++ style string handling, if possible.