Copying Double to a void pointer in C++ [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm copying a double value to a Void*.How can I do it in C++.
Currently I've tried in C++10
strcpy(Trade->MtTr.MPData->MPTrXML.da,dCou);
Trade->MtTr.MPData->MPTrXML.da-->this is a Void*
dCou is double.
strcpy(Trade->MtTr.MPData->MPTrXML.da,dCou);
I expect the void* should contain the double value.
In actual I'm getting error as:
error C2665: 'strcpy' : none of the 2 overloads could convert all the argument types
while trying to match the argument list '(void *, double)'

If you have a valid reason to be attempting to copy a double to a void*, then one way is to use memcpy:
#include <cstring>
//..
// Assuming Trade->MtTr.MPData->MPTrXML.da is valid:
double dCou;
//...
memcpy(Trade->MtTr.MPData->MPTrXML.da, &dCou, sizeof(double));

Related

Cast void pointer to int [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
With a VC++ project I have a line:
FXint realIndex = (int)m_cmbDevice->getItemData(indx);
This reads a void pointer value from combo box to an int value. In VC++ it compiles and works well.
Now I have to port this to Linux and there I get a compile error
cpp:514:20: error:
cast from pointer to smaller type 'int' loses information
FXint realIndex = (int)m_cmbDevice->getItemData(indx);
Now I use
std::size_t x = reinterpret_cast<std::size_t>(m_cmbDevice->getItemData(indx));
FXint realIndex = x;
My question is now, is this the right way to go?
reinterpret_cast is dangerous and is typically used only to interpret a pointer as another kind of pointer. You should use static_cast instead
size_t is not necessarily big enough to store a pointer. There are already intptr_t and uintptr_t which are signed and unsigned integer types that are capable of holding a pointer
FXint realIndex = x; still raises a warning if x is wider than FXint, so another cast is necessary to turn off all related warnings
So you need to do this
auto x = reinterpret_cast<uintptr_t>(m_cmbDevice->getItemData(indx));
auto realIndex = (FXint)x;

Passing pointer on pointers into function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Couldn't find answer to this. When I have pointer on pointers
char **buffer;
and I want to pass it to a function
void some_func(char **buffer) {}
so after this function call buffer will contain data from this function how should I call this function please ?
in your example it is correct but the fact is that you are passing this pointer to pointer by value so pass it by pointer:
// initialize it
char** buffer;
void some_func(char ***buffer) {} // by reference
and in function call:
some_funct(&buffer);
some_func(&buffer);
That's the call you need.
And the function should be...
void some_func(char ***buffer) {}
To manipulate buffer...
*buffer = something;

I am creating a binary file to insert a pair of int type and a structure in it and seeing the below given error . Kindly suggest [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Error:
/opt/x11r6/lib/gcc-lib/sparc-sun-solaris2.5.1/2.95.2/../../../../include/g++-3/stl_pair.h: In method `pair::pair(const char (&)[10], const journey_plan::contact_details &)':
/opt/x11r6/lib/gcc-lib/sparc-sun-solaris2.5.1/2.95.2/../../../../include/g++-3/stl_pair.h:68: instantiated from `make_pair(const char (&)[10], const journey_plan::contact_details &)'
sonu.cpp:142: instantiated from here
/opt/x11r6/lib/gcc-lib/sparc-sun-solaris2.5.1/2.95.2/../../../../include/g++-3/stl_pair.h:44: incompatible types in assignment of `const char[10]' to `char[10]'
/opt/x11r6/lib/gcc-lib/sparc-sun-solaris2.5.1/2.95.2/../../../../include/g++-3/stl_pair.h: In method `pair::pair(const pair &)':
sonu.cpp:142: instantiated from here
/opt/x11r6/lib/gcc-lib/sparc-sun-solaris2.5.1/2.95.2/../../../../include/g++-3/stl_pair.h:48: assignment to `char *const' from `const char *' discards qualifiers
if(it == my_map.end())
{
log_file<<"updateFile()::updating new Customer with : "<<UID<<":"<<temp.c_name<<" : "<<temp.j_count<<endl;
my_map.insert(std::make_pair(UID,temp));
myfile.write(UID,sizeof(UID));//<<UID<<":"<<c_name<<endl;
myfile.write(reinterpret_cast<char *>(&temp),sizeof(temp));
}
It's hard to answer since you did not post any of the source code. But it seems that you tried to use a char array as key to a map. That will not work. Use std::string instead.
std::map<std::string,journey_plan::contact_details>
Edit: You can use char arrays as keys, following this question: Using char* as a key in std::map

What is the difference between pointers to array of pointers? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I came across these two terms
Int (*q)[3][4] and. int q[ ][3][4].
What's the difference these two terms?
And one more question .
Char a[ ]="abcd";
Char *p="abv";
a="ghj";
p="ajk";
Printf("℅s℅s",a,p);
Why this would not compile?
It won't compile because of the line:
a = "ghi"
This assigns a const char* to a char* directly. You can use strcpy to copy the string intead:
strcpy(a, "ghi")
You will still have warnings at this point, because you have not declared p as a const. You can fix this like so:
const char* p = "abv"

Why do I need to const_cast when releasing memory for a const data [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
While releasing memory of a const data, why do I need to const_cast it. What will be the result If I ignore it.
You don't. This is completely valid, standard conform, code:
int const * const a = new int(42);
delete a;
It sounds like you are using std::free from <cstdlib> to do this instead, which signature is
void free(void *);
In this case the implicit conversion from int const * const to void * fails because you can only implicit const-cast, not cast const away. You want the first version in C++ anyway.