Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Mac OS X Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-02-2009, 08:58 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 148
rraagg is on a distinguished road
Default Difference Between two code snippets?

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?
rraagg is offline   Reply With Quote
Old 08-02-2009, 10:41 AM   #2 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
FlyingDiver will become famous soon enough
Default

The first is an array of 4 pointers to chars (ie, C-string pointers). The second is an array of 4 chars (which isn't much at all).

joe
FlyingDiver is offline   Reply With Quote
Old 08-02-2009, 02:56 PM   #3 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 148
rraagg is on a distinguished road
Default

so when a char is not declared as a char[4] it can be of any size ie almost like a string variable ie char on its one is not 1 byte long?
rraagg is offline   Reply With Quote
Old 08-02-2009, 03:09 PM   #4 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
FlyingDiver will become famous soon enough
Default

Quote:
Originally Posted by rraagg View Post
so when a char is not declared as a char[4] it can be of any size ie almost like a string variable ie char on its one is not 1 byte long?
char foo - one char (one byte)
char *foo - a pointer (4 or 8 bytes)
char foo[4] - four chars (4 bytes)
char *foo[4] - an array of 4 char pointers (16 or 32 bytes)

joe
FlyingDiver is offline   Reply With Quote
Old 08-02-2009, 03:43 PM   #5 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 148
rraagg is on a distinguished road
Default

Thanks for the example Joe.
So these are the restrictions:
char foo - one char (one byte)
foo = "a";
char *foo - a pointer (4 or 8 bytes)
foo can only = &testchar ...this is where i get confused. Can *foo store anything(eg string) up to 8bytes long or can it just store a 4 to 8byte address
char foo[4] - four chars (4 bytes)
foo[1] = "r"; etc
char *foo[4] - an array of 4 char pointers (16 or 32 bytes)
so basically i can store four addresses for chars eg &testchar
rraagg is offline   Reply With Quote
Old 08-02-2009, 04:02 PM   #6 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
FlyingDiver will become famous soon enough
Default

Quote:
Originally Posted by rraagg View Post
char *foo - a pointer (4 or 8 bytes)
foo can only = &testchar ...this is where i get confused. Can *foo store anything(eg string) up to 8bytes long or can it just store a 4 to 8byte address
char foo[4] - four chars (4 bytes)
[/b][/i]
While you could probably make a pointer store something else, you'll have to fool the compiler to do it without generating warnings. That is, you can probably do:

Code:
char *foo = 'abcd';
But doing so will generate a warning about "incompatible types".

joe
FlyingDiver is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 470
14 members and 456 guests
alexeir, David-T, Dj_kades, foslock, iAppDeveloper, jeroenkeij, Mijator, myach, pipposanta, QuantumDoja, robsmy, sacha1996, SLIC, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,129
Posts: 402,928
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:28 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0