casting

c++ Why can't I use static_cast to convert char* to int?

三世轮回 提交于 2019-12-13 11:29:24
问题 number = static_cast<int>(argv[1]); Error: Using static_cast to convert from char* to int not allowed. I've tried finding out why on google and I just can't seem to find it. Also, I don't want to get the ascii value, because argv[1] is a number. e.g. ./prog 15 cout << number; //want it to print 15. 回答1: You just try to convert char* to int. You code should be: int number = atoi(argv[1]) 回答2: You can use this: std::stoi function. It's totally C++ , not like one borrowed from c libraries.. atoi

Purpose of void*

六月ゝ 毕业季﹏ 提交于 2019-12-13 11:29:02
问题 I was trying to understand casting in C. I tried this code in IDEONE and got no errors at all: #include <stdio.h> int main(void) { int i=1; char c = 'c'; float f = 1.0; double* p = &i; printf("%d\n",*(int*)p); p = &c; printf("%c\n",*(char*)p); p = &f; printf("%f\n",*(float*)p); return 0; } But when compiled on C++ compiler here I got these errors: prog.cpp:9:15: error: cannot convert 'int*' to 'double*' in initialization double* p = &i; ^ prog.cpp:11:4: error: cannot convert 'char*' to

CAST hyphen (-) to Decimal

扶醉桌前 提交于 2019-12-13 11:13:57
问题 I have to CAST '-' (Hyphen Symbol) to Decimal(8,2) in sql server. I am creating a CTE and dumping some data into a table with some columns. For 1 Column I need to dump '-' AS DECIMAL(8,2). How I will achieve this... I am using SELECT CAST('-' AS DECIMAL(8,2)) Column1 Which is throwing Error: Arithmetic overflow error converting varchar to data type numeric. "I know the above query or casting cannot be done", but I need to achieve it somehow!! please help I have couple of queries using which I

Cast class A to class B without generics

牧云@^-^@ 提交于 2019-12-13 10:11:18
问题 I have two classes that have no connection to one another : public class A { public String Address {get;set} } public class B { public String Address {get;set} } List<A> addressList = DB.Addresses.GetAll(); When I do List<B> addressListOther = addressList.Cast<B>().ToList(); the output is : Additional information: Unable to cast object of type 'A' to type 'B'. Any idea how to fix that ? 回答1: You can use Select() instead of that way: List<B> addressListOther = addressList.Select(a => new B {

JAVA casting list of strings to string array [duplicate]

牧云@^-^@ 提交于 2019-12-13 10:00:34
问题 This question already has answers here : java: (String[])List.toArray() gives ClassCastException (4 answers) Closed 4 years ago . I am working on creating an array of strings from a list of strings. So far, I have the following code: ArrayList<String> layerChoices = new ArrayList<>(); for(IFeatureLayer layer : layerList){ layerChoices.add(layer.getName()); } String[] choices = (String[])layerChoices.toArray(); The issue being that toArray() returns an Object[] and not a String[] which is

C# - Get value from dictionary with key - return property of object?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 09:55:21
问题 I'm making a program which when a chemical symbol of an element is entered, it'll return information of that element onto the form. This is simple to do but it want to try and keep my code as efficient as possible. My dictionary: Dictionary<string, object> alkaliM = new Dictionary<string, object>(); In my code also: alkaliM.Add("Li", new AlkaliMetals.Lithium()); elementSymbol is my input string from the textbox I'm trying to set elementName.Text to the property " name " of my AlkaliMetals

Printing after typecasting with %d or %i gives unexpected outputs [closed]

雨燕双飞 提交于 2019-12-13 09:48:25
问题 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 4 years ago . I am rounding off some values and then printing them. When I use %f option, they are printed correctly, but using the %d or %i option (even after casting the rounded values to int) is giving a weird output, and I am not able to figure the why of it out. Any help is much

printing int using %f format specifier [duplicate]

不羁岁月 提交于 2019-12-13 09:39:59
问题 This question already has answers here : Unexpected output of printf (4 answers) Closed 4 years ago . I wanted to know why output is coming as 0.000000. I know printing int using float format specifier or using a type of value using b type gives unspecified behaviour as written in many place. I'm quoting it "If a conversion specification is invalid, the behaviour is undefined. If any argument is not the correct type for the corresponding conversion specification, the behaviour is undefined."

Using reinterpret_cast to check inheritance at compile time

﹥>﹥吖頭↗ 提交于 2019-12-13 09:33:53
问题 Regarding this question: When to use reinterpret_cast? I found sth. like this: template<typename T> bool addModuleFactoryToViewingFactory(ViewingPackage::ViewingFactory* pViewingFactory) { static_cast<ModuleFactory*>(reinterpret_cast<T*>(0)); // Inheritance compile time check ... } Is this a good way to check whether T can be casted to ModuleFactory at compile time? I mean, to check if the programmer put valid stuff into the <> of addModuleFactoryToViewingFactory<T>(...) Is this okay, good or

Why does a float when converted to an int be rounded off below in C?

末鹿安然 提交于 2019-12-13 08:47:55
问题 In an interview, I was asked what do I think about the following code: #include <stdio.h> int main() { float f = 10.7; int a; a = f; printf ("%d\n", a); } I answered: The compiler will emit a warning as you are changing a float to an int without a cast. The int will have garbage value as you are not using a cast. Then, they allowed me to run the program on a online compiler. I was overwhelmed. Both my assumptions were wrong. The compiler did not emit any warnings, and the int had the value 10