Objective-C global array of ints not working as expected?

The extern needs to be in the declaration in the header, not in the definition in the source file. Extern tells the compiler that the symbol exists somewhere else, it may or may not be in the same translation unit. It is the linker's job to make sure that all declared symbols were actually defined.

Up vote 2 down vote favorite share g+ share fb share tw.

In my MyConstants. H file... I have: int abc3; In my matching MyConstants. M file... I have: extern int abc3 = {11, 22, 33}; In each of my other *.

M files... I have #import "MyConstants. H" Inside 1 of my viewDidLoad{} methods, I have: extern int abc; NSLog(@"abc = (%d) (%d)", abc1, sizeof(abc)/sizeof(int)); Why does it display "abc = (0) (3)" instead of "abc = (22) (3)"? How do I make this work as expected?

C objective-c arrays global-variables extern link|improve this question asked May 12 '10 at 1:07Susanna144110 5% accept rate.

4 Shouldn't 'extern' go in the . H file instead of . M?

– squelart May 12 '10 at 1:21 @squelart: make that an answer. – Stephen Canon May 12 '10 at 1:27 @squelart: D'oh, sorry, didn't mean to steal your thunder. I will upboat your answer if you add one and remove mine.

– dreamlax May 12 '10 at 1:45 I didn't have a mac to test and little time to write a full answer... @dreamlax: No worries, you wrote a thorough answer, I couldn't do better, deserved +1 for you! – squelart May 12 '10 at 2:25.

The extern needs to be in the declaration in the header, not in the definition in the source file. Extern tells the compiler that the symbol exists somewhere else, it may or may not be in the same translation unit. It is the linker's job to make sure that all declared symbols were actually defined.

Constants Header (MyConstants. H): extern int abc3; Constants Source (MyConstants. M): int abc3 = {11, 22, 33}; Other Source (SomeFile.

M): #include "MyConstants. H" ... - (void) someMethod { NSLog (@"abc = (%d) (%d)", abc1, sizeof(abc)/sizeof(int)); } Also, note that when measuring the size of an array, it is less error-prone to divide by the size of the first element, so that if the type of abc changes (i.e. From int to double), the results are still valid.

- (void) someMethod { NSLog(@"abc = (%d) (%d)", abc1, sizeof(abc)/sizeof(abc0)); }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions