C++ :: Can't Find A Cast To Work For Conversion
Apr 10, 2015
I have a library function with a prototype:
Code:
void func(unsigned char* in);
I would like to pass a std::string to it, so I tried:
Code:
std::string temp = "Hello!";
func((unsigned char*)temp.c_str());
This works, but I thought it was good practice for type safety to use C++ style casting, yet reinterpret_cast does not work in this instance as it fails:
ERROR: reinterpret_cast can not cast away const or other type qualifiers
and const_cast fails with:
ERROR: a const_cast can only adjust type qualifiers, it cannot change the underlying type
So what is the correct method of casting here assuming I can not change the library interface, and my data is kept in an std::string?
View 7 Replies
ADVERTISEMENT
Oct 2, 2014
Is it possible to get the string representation of an int. So I mean if i have: 5, I want to get '5'.
I was searching in the internet and I found this solution:
Code:
int number = 5;
char c = number + '0'; which works fine for small numbers, but if I do:
Code: int i;
char c;
for(i =0; i < 10000; i++) {
c = i + '0';
printf("%c
", c);
}
At some point some strange sings like %&/)/)$%&) are appearing, But i really need this huge numbers as well .. Is there any other way to do this??
View 8 Replies
View Related
Sep 29, 2013
How to cast an int to a string I came across this code:
#include <iostream>
#include <string>
int main(){
double f = 23.43;
std::string f_str = std::to_string(f);
std::cout << f_str << '
';
}
Unfortunately, using Dev C++ Version 5.4.2, I'm getting an error:
[Error] 'to_string is not a member of 'std'
View 2 Replies
View Related
Dec 7, 2013
In my refference book I have got a example with a part saying to access the a[4][0] element of the array (named a) with pointer this can be written:
*((int*)a+4)
I wonder if the cast is really required. The book says it is required so that the pointer arithmetic can be done properly. However I am not sure about it. When I work with pointers defined by myself I don't use casts similar to this one. Is there a difference between a self defined pointer and array name?
View 14 Replies
View Related
May 7, 2014
I have this program.
Code:
int test_variable;
int main() {
test_variable = (int)0x12;
}
Now my doubt is what is the advantage of type casting 0x12 to int. suppose if the test_variable data type is "char" then should i type cast to "char"?
View 6 Replies
View Related
Mar 14, 2012
Code:
namespace ConsoleApplication1 {
class ImplicitlyConvertibleFromFoo {
public ImplicitlyConvertibleFromFoo(Foo foo) {
this.foo = foo;
}
Foo foo;
[code]....
Any way to easily convert arrays of user-defined types?
View 7 Replies
View Related
Jun 20, 2013
Here is the code,
Code:
class B {
};
class D1:public B {
};
class D2:public D1 {
};
int main() {
B& b = dynamic_cast<B&>(*(new D2));
return 0;
}
D2 is actually grandchild of B, so D2 object reference can still be converted to B reference without any exception. Is it legal?
View 4 Replies
View Related
Mar 6, 2015
I am writing a program with FLTK and in event handling part wanted to send the address of the handler function to callback function. Type cast just does not work.
Code: btnOpenDB->callback(&FLUI::WndMain::DBOpenBtnClick_scb, reinterpret_cast<void*>(&WndMain::DBOpenBtnClick_cb));
I want to cast "a pointer to a class member function which accepts one argument" to a void pointer. After all it is just an address. So it is logically possible to cast it. Isn't it? Actually an static_cast should be enough!
View 9 Replies
View Related
Mar 18, 2013
The warning: :63:7: warning: passing argument 1 of fputc makes integer from pointer without a cast [enabled by default]
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
[Code]....
View 2 Replies
View Related
Dec 15, 2013
Code:
#include <stdio.h>
#include <stdlib.h>
#define CLASS 4
#define STUDENT 11
#define GRADE 5
[Code] ....
Giving me that error on 75:10
avesub+=grade[k];
and 90:17
donkeypunch+=grade[j][k];
not sure exactly why
View 8 Replies
View Related
Apr 18, 2014
I cannot get the following to compile. The problem is the printf on the last line. I understand that printf requires a char (or pointer to char). I understand that I can convert between datatypes by putting the target data type in parenthises in front of the variable. But how do I cast the integer into a character and then get it's pointer to pass into printf?
Following is my code. I compile with gcc temp.c -o temp.
Note that I have tried many attempts at that last line and this is just the one that I really, really think should work (or is at least the closest to the correct answer).
This code shown below, using printf("%s", &(char)nextChar); returns
temp.c:26: error: lvalue required as unary '&' operand
If I try to use printf("%s", *(char)nextChar); I get the error
temp.c:26: error: invalid type argument of 'unary *' (have 'int')
This line printf("%s", (char)nextChar); returns the obvious
format '%s' expects type 'char *', but argument 2 has type 'int'
Code:
#include <stdio.h>
int main() {
printf("hello, world
");
#if defined(SUNDIALS_EXTENDED_PRECISION)
[Code] ....
View 5 Replies
View Related
Oct 10, 2014
Is it possible to handle situations where static cast fails. I have a sample code written belo:
typedef struct test {
int a;
int b;
} tester;
void setevent(void *in) {
tester *recv = static_cast<tester*>(in);
[Code] ....
In above code when setevent is called with the tester object, there is no issue, but if we pass random value it leads to seg fault. This is because static_cast is not type_safe, but can it be handled in any other way?
View 1 Replies
View Related
Oct 10, 2013
Is there a difference between having static and dynamic cast in this scenario? The output is the same.
Code:
Base* pb = new Derived();
if(Derived* pd2 = static_cast<Derived*>(pb)) // true {
pd2->get_price(); // calls Base::get_price()
pd2->get_rate(); // calls Derived::get_rate()
[Code] ....
View 7 Replies
View Related
Jul 1, 2013
I've got two classes, which are both derived from the same two base classes. Here's a representation of the actual code:
Code: #include <vector>
class BaseClassA {
};
class BaseClassB {
};
class TestClassX
: public BaseClassA,
public BaseClassB
[code].....
Basically, I'd like to know if it is possible to cast directly from a BaseClassA pointer to a BaseClassB pointer, without casting to the child class first.
View 10 Replies
View Related
Jan 23, 2014
int hash = 0;
char *strings[100];
if((int)strings[i] != 0)
if((int) strings[hash] != 0)
while((int) strings[hash] != 0)
if((int)strings[hash] != 0)
if((int)strings[hash] != 0)
View 12 Replies
View Related
Oct 24, 2014
I programming currently something with OpenGL. Now I have written some wrapper classes, like Shader, Program .... All works fine, but I don't like to call Shader.GetHandle() every time I want to call a OpenGL function manually where I need the object handle/id. (GetHandle() returns the OpenGL ID of the object)
So now I wonder, is it possible to program it in C++ so, that I can put my objects to gl methods and c++ automatically pass the handle/id member to that function ? Is there maybe a operator that I can use for that?
For example:
The way I do it now:
Shader vertexShader();
glCompileShader(vertexShader.GetHandle());
The way I want to:
Shader vertexShader;
glCompileShader(vertexShader);
View 3 Replies
View Related
Aug 1, 2012
my array contains elements of integer type
how can i convert my array to long array
View 1 Replies
View Related
Mar 6, 2015
Code:
#include<stdio.h>
print_int_ptr(int *a){
printf(" a %i
" ,a);
printf(" &a %i
" ,&a);
[Code] .....
I get that warning : passing arg 1 of `print_int_ptr' makes pointer from integer without a cast|
View 3 Replies
View Related
Jan 18, 2015
Code:
#include <stdio.h>
int main(){
int a=15 ;
int b =20 ;
strcmp((a, "20") == 0) {
printf("Correct!");
[Code] .....
passing arg 1 of `strcmp' makes pointer from integer without a cast
View 11 Replies
View Related
Mar 26, 2014
I am having some errors with pointers and passing arguments.
Code:
#include <stdlib.h>
#include <stdio.h>
#define MAX_FILE_LENGTH 20
typedef struct node_{
int value;
struct node_* next;
[Code]....
View 3 Replies
View Related
Sep 21, 2013
I have a function that I want to exit gracefully when an "error" occurs in an input file. My function declaration is:
Code: BSTnode *buildTree(FILE *fp)
The few lines that are causing the problems are:
Code: if(regcomp(®ex, to_find, REG_EXTENDED | REG_NEWLINE) != 0) {
fprintf(stderr, "Failed to compile regex '%s'
", to_find);
return EXIT_FAILURE;
}
I know that if I just use "return" by itself the warning goes away but fails to exit when the error occurs. I also believe this may not be the correct use of stderr. But I need the program to exit when an error has occurred.
View 9 Replies
View Related
Apr 25, 2013
I having a problem which I'm not able to resovle. I try to dereference a void pointer but I always get a C2440 error. It says: 'static_cast':void* cannot be converted in wqueue<T>. I tried different cast ways but I always get the same error. As far as I found out I should get the error if I try to dereference without cast but in my case I cast before and still get that error.
void *srumbler (void *arg) {
wqueue<workclas*> m_queue= static_cast<wqueue<workclass*>>(arg);
return NULL;
}
The according type wqueue in the header file:
template <typename T> class wqueue {
list<T> m_queue;
pthread_mutex_t m_mutex;
pthread_cond_t m_condv;
[Code] .....
View 3 Replies
View Related
Feb 26, 2013
I have the next program: if I comment the assignation mNum=num; or mCh=ch the segmentation fault don't exist.
#include <vector>
class Base{
public:
[Code]....
View 2 Replies
View Related
Jul 4, 2013
The log file gives me: In function ‘memFileAlloc’ assignment makes pointer from integer without a cast..When compiling the drivers for the Matrox card in the DL580. The offending code is:
STACK_LINKAGE MEMHANDLE memFileAlloc(
UINT32 dwSize,
const char* pszFileName,
int iLine) {
void* pvChunk;
#if MEMORY_STATS
[code]...
I think the offending line is:
pvChunk = ClientMemAlloc(dwSize + sizeof(UINT32), NULL)
because that's what the log file tells me.
The system is a 16 core HP DL580 G4 with 8g RAM, RAID 0, Mandrivalinux 11.0 and the display is a Matrox Parhelia 256PCIx.
View 11 Replies
View Related
Dec 17, 2014
Following is my xml file called PropertyInfo.xml :-
<?xml version="1.0" encoding="utf-8" ?>
<PropertyInformation>
<locations>
<location name="Bombay">
[Code].....
In above LoadPropertyInfo() method, how to cast DataSet to List<Location> locations before returning "locations" ?
View 2 Replies
View Related
Apr 4, 2013
Code: Obj<TCHAR>* obj = dynamic_cast<Obj<TCHAR>* >(I->objects[0]);
Which is resulting in this error
Code: A dynamic cast is present, but the correct RTTI option is not specified.
View 3 Replies
View Related