casting

Should I worry about the alignment during pointer casting?

倖福魔咒の 提交于 2019-12-27 17:11:44
问题 In my project we have a piece of code like this: // raw data consists of 4 ints unsigned char data[16]; int i1, i2, i3, i4; i1 = *((int*)data); i2 = *((int*)(data + 4)); i3 = *((int*)(data + 8)); i4 = *((int*)(data + 12)); I talked to my tech lead that this code may not be portable since it's trying to cast a unsigned char* to a int* which usually has a more strict alignment requirement. But tech lead says that's all right, most compilers remains the same pointer value after casting, and I

In C#, is it possible to cast a List<Child> to List<Parent>?

江枫思渺然 提交于 2019-12-27 17:06:13
问题 I want to do something like this: List<Child> childList = new List<Child>(); ... List<Parent> parentList = childList; However, because parentList is a List of Child's ancestor, rather than a direct ancestor, I am unable to do this. Is there a workaround (other than adding each element individually)? 回答1: Casting directly is not allowed because there's no way to make it typesafe. If you have a list of giraffes, and you cast it to a list of animals, you could then put a tiger into a list of

In C#, is it possible to cast a List<Child> to List<Parent>?

时光总嘲笑我的痴心妄想 提交于 2019-12-27 17:05:52
问题 I want to do something like this: List<Child> childList = new List<Child>(); ... List<Parent> parentList = childList; However, because parentList is a List of Child's ancestor, rather than a direct ancestor, I am unable to do this. Is there a workaround (other than adding each element individually)? 回答1: Casting directly is not allowed because there's no way to make it typesafe. If you have a list of giraffes, and you cast it to a list of animals, you could then put a tiger into a list of

In C#, is it possible to cast a List<Child> to List<Parent>?

好久不见. 提交于 2019-12-27 17:04:10
问题 I want to do something like this: List<Child> childList = new List<Child>(); ... List<Parent> parentList = childList; However, because parentList is a List of Child's ancestor, rather than a direct ancestor, I am unable to do this. Is there a workaround (other than adding each element individually)? 回答1: Casting directly is not allowed because there's no way to make it typesafe. If you have a list of giraffes, and you cast it to a list of animals, you could then put a tiger into a list of

Converting a pointer into an integer

笑着哭i 提交于 2019-12-27 11:02:51
问题 I am trying to adapt an existing code to a 64 bit machine. The main problem is that in one function, the previous coder uses a void* argument that is converted into suitable type in the function itself. A short example: void function(MESSAGE_ID id, void* param) { if(id == FOO) { int real_param = (int)param; // ... } } Of course, on a 64 bit machine, I get the error: error: cast from 'void*' to 'int' loses precision I would like to correct this so that it still works on a 32 bit machine and as

java: how can i do dynamic casting of a variable from one type to another?

本小妞迷上赌 提交于 2019-12-27 10:31:13
问题 i would like to do dynamic casting for a java variable, the casting type is stored in a different variable. this is regular casting: String a = (String) 5; this is what i want: String theType = 'String'; String a = (theType) 5; is it possible? and if so how? thanks! update I'm trying to populate a class with a hashMap that i received. this is the constructor: public ConnectParams(HashMap<String,Object> obj) { for (Map.Entry<String, Object> entry : obj.entrySet()) { try { Field f = this

How to cast set in list into list in python?

爷,独闯天下 提交于 2019-12-26 07:20:03
问题 I want to cast set in list to list like below. before: [(1, 1, 1), (1, 1, 0), (1, 0, 1)] after: [[1, 1, 1], [1, 1, 0], [1, 0, 1]] I need the as simple code as possible. 回答1: >>> x = [(1, 1, 1), (1, 1, 0), (1, 0, 1)] >>> list(map(list, x)) [[1, 1, 1], [1, 1, 0], [1, 0, 1]] Explanation map(list, x) takes an iterable x and applies function list to each element of this iterable. Thus the tuple (1, 1, 1) becomes the list [1, 1, 1] , (1, 1, 0) becomes [1, 1, 0] and (1, 0, 1) becomes [1, 0, 1] .

How to cast set in list into list in python?

时光怂恿深爱的人放手 提交于 2019-12-26 07:19:46
问题 I want to cast set in list to list like below. before: [(1, 1, 1), (1, 1, 0), (1, 0, 1)] after: [[1, 1, 1], [1, 1, 0], [1, 0, 1]] I need the as simple code as possible. 回答1: >>> x = [(1, 1, 1), (1, 1, 0), (1, 0, 1)] >>> list(map(list, x)) [[1, 1, 1], [1, 1, 0], [1, 0, 1]] Explanation map(list, x) takes an iterable x and applies function list to each element of this iterable. Thus the tuple (1, 1, 1) becomes the list [1, 1, 1] , (1, 1, 0) becomes [1, 1, 0] and (1, 0, 1) becomes [1, 0, 1] .

C cast literal char '0' to int 0 (zero), how?

回眸只為那壹抹淺笑 提交于 2019-12-25 19:33:11
问题 How would I go to cast/convert literal char '0' to int 0 ? i.e.: char string[] = "0101"; char ch = string[0]; x = magic(ch); if (x) printf("int zero") 回答1: ch = 0; Now, if you want to convert any digit-character to its numeric equivalent, you'd need something like: ch = ch - '0'; 来源: https://stackoverflow.com/questions/41365518/c-cast-literal-char-0-to-int-0-zero-how

C cast literal char '0' to int 0 (zero), how?

霸气de小男生 提交于 2019-12-25 19:32:08
问题 How would I go to cast/convert literal char '0' to int 0 ? i.e.: char string[] = "0101"; char ch = string[0]; x = magic(ch); if (x) printf("int zero") 回答1: ch = 0; Now, if you want to convert any digit-character to its numeric equivalent, you'd need something like: ch = ch - '0'; 来源: https://stackoverflow.com/questions/41365518/c-cast-literal-char-0-to-int-0-zero-how