640printf()ing string in C++

Silly, little C++ Beginner’s mistake. How do you print strings in C++?
string test = "tssst";
printf(test);
Of course not, that’s C++!!! That’s how you do it:
printf(test.c_str());
And if you want to stop the compiler throwing warnings:
printf("%s", test.c_str());
So close, yet so far out.

632Inline Functions in C

The keyword inline, when applied to functions, tells the compiler to optimize calls to the functions. Usually used for short functions and for the sake of clarity.
inline int max(int a, int b) {
	return a + b;
}

int main(void) {
	int x = 2;
	int y = 6;
	printf("result: %d", add(x,y));
}

// ok, the example is stupid, but i hope it illustrates the idea
Looks exactly the same as calling a function, but actually the function is not called, no function-calling overheads are created. Instead the inline function is expanded in place, where it is called. Obviously this only makes sense with rather short functions.

452Padding with Zeros (or other characters)

int x = 11;

[NSString stringWithFormat:@"%03i", x];
// @"011"

[NSString stringWithFormat:@"%+5i", x];
// @"+++11"

[NSString stringWithFormat:@"%+05i", x];
// @"+0011"
And not really like that. 381. I’ll still have to learn a lot of C.

420Getting current Time with Microseconds

#import <sys/time.h>

struct timeval tv;
gettimeofday(&tv, NULL);
int sec = tv.tv_sec;
int usec = tv.tv_usec;
The system’s notion of the current Greenwich time and the current time zone is obtained with the gettimeofday() call, and set with the ettimeofday() call. The time is expressed in seconds and microseconds since midnight (0 hour), January 1, 1970. The resolution of the system clock is hardware dependent, and the time may be updated continuously or in “ticks.” If tp is NULL and tzp is non-NULL, gettimeofday() will opulate the timezone struct in tzp. If tp is non-NULL and tzp is NULL, then only the timeval struct in tp is populated. If both tp and tzp are NULL, nothing is returned.
Check out the man page for more info.

313Swimming Nearly drowning in C++

OK, I’ve decided to do it. To leave the elegance of Objective-C and have a closer look at C++. After all, isn’t everyone doing oF these days? Here a recapitulation of usesful C and some quick notes on C++ peculiarities – sorry – features: (That might be a good place to start.) Initialisation int a = 1; // standard c way int b(1); // same Defined constants #define PI 3.1415927 // faster than var Declared constants const int c = 100 // immutable Assignment = can also be used as rvalue a = 1 + (b = 2); same as: b = 2; a = 1 + b; Comma operator a = (b=3, b+2); // a = 5
When the set of expressions has to be evaluated for a value, only the rightmost expression is considered
Type casting int i, j; float f = 1.234; i = (int) f; j = int (f); sizeof() One parameter, either a type or a variable and returns the size in bytes. a = sizeof (char); Will become important with arrays.

305Converting float to string %4.2f

printf ("floats: %4.2f %+.0e %E \n", 3.1416, 3.1416, 3.1416); floats: 3.14 +3e+000 3.141600E+000

107Typedef, Struct, Enum

struct a collection of types, composite data type, composition, “user-definded data structure” struct Test { char* name; int nr; float f; }; Name of type is “struct Test”, and not “Test” alone. struct Test myNewTest Semicolon after closing parethesis is essentional. Access composed types with dot-syntax: myNewTest.name = "hjkjnk"; myNewTest.nr = 32; myNewTest.f = 2.2424; typedef a shorthand, an alias for a type, for ease of readability. typedef int MyType “MyType n;” would be equal to “int n;” enum “introduces a symbolic name for an integer constant” enum direction_enum {LEFT, RIGHT}; /* LEFT -> 0, RIGHT -> 1; */ enum direction_enum x = LEFT; typedef enum typedef enum { LEFT; RIGHT; } direction; declatation: -(void)myFunction:(direction)LEFTorRIGHT; call: -(void)myFunction:LEFT; typedef struct typedef struct objectInfo_ { CGPoint origin; NSString* name; NSColor* color; } objectInfo Shortcut. Instead of writing ‘struct objectInfo_’, write ‘objectInfo’. The keyword ‘objectInfo_’ seems to be optional.