I'm just an amateur programmer ... And, reading, for the second time and more than two years, heading out "Programming in Objective-C", now the sixth ed., Having reached the head of the index, I tried to revive the old days when I started programming with C .., So, I tried to program the inverse C-line function using char pointers ... In the end I got the desired result, but ... got also a very strange behavior, I can not explain with my little programming experience ...
First code:
This is a .m file,
#import <Foundation/Foundation.h> #import "*pathToFolder*/NSPrint.m" int main(int argc, char const *argv[]) { @autoreleasepool { char * reverseString(char * str); char *ch; if (argc < 2) { NSPrint(@"No word typed in the command line!"); return 1; } NSPrint(@"Reversing arguments:"); for (int i = 1; argv[i]; i++) { ch = reverseString(argv[i]); printf("%s\n", ch); //NSPrint(@"%s - %s", argv[i], ch); } } return 0; } char * reverseString(char * str) { int size = 0; for ( ; *(str + size) != '\0'; size++) ; //printf("Size: %i\n", size); char result[size + 1]; int i = 0; for (size-- ; size >= 0; size--, i++) { result[i] = *(str + size); //printf("%c, %c\n", result[i], *(str + size)); } result[i] = '\0'; //printf("result location: %lu\n", result); //printf("%s\n", result); return result; }
Second notes:
This code compiles on a MacBook Pro with MAC OS X Maverick with CLANG (clang -fobjc-arc $ file_name -o $ file_name_base)
That NSPrint is just a wrapper for printf to print an NSString built with stringWithFormat: arguments:
And the third strange behavior:
If I uncomment all commented printf declarations, everything works fine, i.e. all printf functions print what they need to print, including the last printf inside the main function.
If I uncomment one and only one random selection of those printf comments, everything works fine, and I got the correct printf results, including the last printf inside the main function.
If I leave all the commented printf functions as they are, I have only BLANK LINES with the last printf inside the main block and one black line for each argument passed ...
Worse, if I use this NSPrint function inside main , instead of printf , I get the desired result :! p>
Can someone bring some light here please :)