What do the plus and minus signs mean in Objective-C next to a method?
+
is for a class method and -
is for an instance method.
E.g.
// Not actually Apple's code.
@interface NSArray : NSObject {
}
+ (NSArray *)array;
- (id)objectAtIndex:(NSUInteger)index;
@end
// somewhere else:
id myArray = [NSArray array]; // see how the message is sent to NSArray?
id obj = [myArray objectAtIndex:4]; // here the message is sent to myArray
// Btw, in production code one uses "NSArray *myArray" instead of only "id".
There's another question dealing with the difference between class and instance methods.
What is &&& operation in C
It's c = i && (&i);
, with the second part being redundant, since &i
will never evaluate to false
.
For a user-defined type, where you can actually overload unary operator &
, it might be different, but it's still a very bad idea.
If you turn on warnings, you'll get something like:
warning: the address of ‘i’ will always evaluate as ‘true’
What does the operation c=a+++b mean?
It's parsed as c = a++ + b
, and a++
means post-increment, i.e. increment after taking the value of a
to compute a + b == 2 + 5
.
Please, never write code like this.
What does output indicate when larger address is subtracted from smaller address in C/C++ programming?
The problem in your code is that you have undefined behaviour. Not because of the pointer arithmetic but because you are printing signed integers using the %u
format specifier. Change to the %td
format (the t
to specify a 'pointer difference' type) and you will see more meaningful results:
#include <stdio.h>
#include <stddef.h>
int main()
{
int a[5];
printf("%td\n", &a[3] - &a[0]); // Shows "3"
printf("%td", &a[0] - &a[3]); // Shows "-3"
return 0;
}
From this C11 Draft Standard (Section 6.5.6 Paragraph #9) (bolding mine):
When two pointers are subtracted, both shall point to elements of the
same array object, or one past the last element of the array object;
the result is the difference of the subscripts of the two array
elements. The size of the result is implementation-defined, and its
type (a signed integer type) isptrdiff_t
defined in the<stddef.h>
header ...
Related Topics
Do Stl Iterators Guarantee Validity After Collection Was Changed
Constructor Initialization VS Assignment
Who Defines C Operator Precedence and Associativity
Comma Operator in If Condition
Why Don't Std::Vector's Elements Need a Default Constructor
Do Static Members of a Class Occupy Memory If No Object of That Class Is Created
C++: How to Get Fprintf Results as a Std::String W/O Sprintf
How to Extend a Lexical Cast to Support Enumerated Types
Looking for 16-Bit X86 Compiler
C++Cli. Are Native Parts Written in Pure C++ But Compiled in Cli as Fast as Pure Native C++
Easiest Way to Make a Cyclic Iterator (Circulator)
Std::Back_Inserter for a Std::Set
Return Statement in Ternary Operator C++
Error: Class Has Not Been Declared Despite Header Inclusion, and the Code Compiling Fine Elsewhere
Lambda Capture and Parameter with Same Name - Who Shadows the Other? (Clang VS Gcc)