casting

C job interview - casting and comparing

蓝咒 提交于 2019-12-20 09:32:12
问题 I was confronted with a tricky (IMO) question. I needed to compare two MAC addresses, in the most efficient manner. The only thought that crossed my mind in that moment was the trivial solution - a for loop, and comparing locations, and so I did, but the interviewer was aiming to casting. The MAC definition: typedef struct macA { char data[6]; } MAC; And the function is (the one I was asked to implement): int isEqual(MAC* addr1, MAC* addr2) { int i; for(i = 0; i<6; i++) { if(addr1->data[i] !=

Idiomatic way to do conversion/type assertion on multiple return values in Go

南楼画角 提交于 2019-12-20 08:23:58
问题 What is the idiomatic way to cast multiple return values in Go? Can you do it in a single line, or do you need to use temporary variables such as I've done in my example below? package main import "fmt" func oneRet() interface{} { return "Hello" } func twoRet() (interface{}, error) { return "Hejsan", nil } func main() { // With one return value, you can simply do this str1 := oneRet().(string) fmt.Println("String 1: " + str1) // It is not as easy with two return values //str2, err := twoRet()

as! vs as operator in Xcode 6.3 in Swift

孤街浪徒 提交于 2019-12-20 07:46:05
问题 Swift changed a lot with Xcode 6.3. I had to replace dozens of places in each of my app as -> as! . Why, what are the rules now? 回答1: Prior to Swift 1.2, the as operator could be used to carry out two different kinds of conversion, depending on the type of expression being converted and the type it was being converted to: Guaranteed conversion of a value of one type to another, whose success can be verified by the Swift compiler. For example, upcasting (i.e., converting from a class to one of

Trying to use AsyncTask do download some image files

别等时光非礼了梦想. 提交于 2019-12-20 06:38:56
问题 I have list of files stored inside arraylist that I need to download in background thread. My initial thought is that AsyncTask should be up for that task. But, I have a problem, I don't know how to supply my list to doInBackground method. My arraylist is defined as private ArrayList<String> FilesToDownload = new ArrayList<String>(); My DownloadFiles subclass (the way it is written now) should be called with: new DownloadFiles().execute(url1, url2, url3, etc.); This is not suitable for me

MySQL Unexpected Results: “IN”-clause (number, 'string') on a varchar column

送分小仙女□ 提交于 2019-12-20 06:26:29
问题 In MySQL, why does the following query return '----' , '0' , '000' , 'AK3462' , 'AL11111' , 'C131521' , 'TEST' , etc.? select varCharColumn from myTable where varCharColumn in (-1, ''); I get none of these results when I do: select varCharColumn from myTable where varCharColumn in (-1); select varCharColumn from myTable where varCharColumn in (''); Note: I'm using MySQL version 5.0.45-log ( show variables like "%version%"; ) Note 2: I tried this on a number column as well, but I do not get

Explicit casting doesn't work in default model binding

旧时模样 提交于 2019-12-20 05:40:46
问题 I am using ASP.NET MVC2 and Entity Framework. I am going to simplify the situation a little; hopefully it will make it clearer, not more confusing! I have a controller action to create address, and the country is a lookup table (in other words, there is a one-to-many relationship between Country and Address classes). Let's say for clarity that the field in the Address class is called Address.Land. And, for the purposes of the dropdown list, I am getting Country.CountryID and Country.Name. I

Convert String to Int with Stringstream

寵の児 提交于 2019-12-20 05:11:32
问题 have a little problem here: int IntegerTransformer::transformFrom(std::string string){ stream->clear(); std::cout<<string<<std::endl;; (*stream)<<string; int i; (*stream)>>i; std::cout<<i<<std::endl; return i; } I by calling this function with the string "67" (other values dont work too) i get this output: 67 6767 回答1: Did you notice there are two std::cout in the function itself? Beside that also add this: stream->str(""); //This ensures that the stream is empty before you use it. (*stream)<

Convert String to Int with Stringstream

拈花ヽ惹草 提交于 2019-12-20 05:11:06
问题 have a little problem here: int IntegerTransformer::transformFrom(std::string string){ stream->clear(); std::cout<<string<<std::endl;; (*stream)<<string; int i; (*stream)>>i; std::cout<<i<<std::endl; return i; } I by calling this function with the string "67" (other values dont work too) i get this output: 67 6767 回答1: Did you notice there are two std::cout in the function itself? Beside that also add this: stream->str(""); //This ensures that the stream is empty before you use it. (*stream)<

Casting to Superclass, and Calling Overriden Method

你说的曾经没有我的故事 提交于 2019-12-20 04:51:59
问题 I have my next question. I have extended a class, Parrent and overridden one of its method in the Child class. I tried to cast the type to the superclass type, but I get the child's overridden method every time. This also happens when I use polymorphism. Questions are in the comments inside code below... Thanks in advance. class Parrent{ public void test(){ System.out.println("parentTest"); } } class Child extends Parrent{ @Override public void test(){ System.out.println("childTest"); } }

Why is 32-bit pointer sign-extended when cast to uint64_t?

牧云@^-^@ 提交于 2019-12-20 04:21:47
问题 When compiled as a 32-bit process, the following code prints ffffffff82223333 instead of 82223333 , so it seems like a pointer is always sign-extended when converted to uint64_t . Why is that? #include <stdint.h> #include <stdio.h> int main() { void *p = (void*) 0x82223333; uint64_t x = (uint64_t) p; printf("%llx\n", x); } I thought an address can never be negative, so it should be treated as unsigned. My question is also related to this question (because Windows handles are just typedefs for