Hi I'm trying to workout the difference in behaviour of the following to code snippets
This one Works:
Code:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
const char *words[4] = { "aardvark", "abacus",
"allude", "zygote" };
int wordCount = 4;
int i;
for (i = 0; i < wordCount; i++) {
NSLog (@"%s is %d characters long",
words[i], strlen(words[i]));
}
return (0);
} // main
This doesnt:
Code:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
const char words[4] = { "aardvark", "abacus",
"allude", "zygote" };
int wordCount = 4;
int i;
for (i = 0; i < wordCount; i++) {
NSLog (@"%s is %d characters long",
words[i], strlen(words[i]));
}
return (0);
} // main
The difference is const char words[4] decleration.
Am I right in thinking
Code:
const char *words[4]
works because it returns the address of the first array element and the list can then be assigned because it knows the starting adress from the pointer?
and
Code:
const char words[4]
doesnt work because it thinks your trying to assign the array list to the fourth element which is not big enought?