Is typecast required in malloc? [duplicate]

I assume you mean something like this: int *iptr = (int*)malloc(/* something */); And in C, you do not have to (and should not) cast the return pointer from malloc. It’s a void * and in C, it is implicitly converted to another pointer type. int *iptr = malloc(/* something */); Is the preferred form. … Read more

C++ When should we prefer to use a two chained static_cast over reinterpret_cast

reinterpret_cast should be a huge flashing symbol that says THIS LOOKS CRAZY BUT I KNOW WHAT I’M DOING. Don’t use it just out of laziness. reinterpret_cast means “treat these bits as …” Chained static casts are not the same because they may modify their targets according to the inheritence lattice. struct A { int x; … Read more

Safety of casting between pointers of two identical classes?

C++11 added a concept called layout-compatible which applies here. Two standard-layout struct (Clause 9) types are layout-compatible if they have the same number of non-static data members and corresponding non-static data members (in declaration order) have layout-compatible types (3.9). where A standard-layout class is a class that: has no non-static data members of type non-standard-layout … Read more

Iphone UITextField only integer

Implementing shouldChangeCharactersInRange method as below does not allow the user input non-numeric characters. – (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSCharacterSet *nonNumberSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet]; return ([string stringByTrimmingCharactersInSet:nonNumberSet].length > 0) || [string isEqualToString:@””]; } This returns YES if the string is numeric, NO otherwise. the [string isEqualToString@””] is to support the backspace key to … Read more