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 > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-25-2011, 01:54 PM   #1 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 4
bamboo is on a distinguished road
Default newbie question about objective-c

i'm new to programming world, am interested in iphone dev.
right now i'm reading Stephen Kochan's book "Programming in Objective-C 2.0".
and had some problems i couldn't figured out on my own.
I posted the following question on the official forum for the book, but it seemed like no one's there. I googled this site.

"I was reading pointers on chapter13 today, and had a question.
since a array name without a subscript is a pointer to the beginning of the array,
why can't we use increment on the array name like ++array,
and when you assign it to a pointer variable, you can do that with pointer?

for example, we have a character array
char array[] = {'a','b','c','\0'}
and you cannot do this:
NSLog(@"%c", *(++array));
although this works fine:
NSLog(@"%c", *(array+1));

then you assign it to a pointer:
char *ptr = array;
then it works:
NSLog(@"%c", *(++ptr));

anyone knows why???
"
bamboo is offline   Reply With Quote
Old 05-08-2011, 05:27 PM   #2 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 66
Sami Gh is on a distinguished road
Default

You can't consider arrays as pointers. Although arrays are mentioned sometimes as a pointer to the beginning of the array, there are differences between both of them.
One of the differences at your case is that arrays address cannot be changed, so when you try to increment the array then you are trying to manipulate the address which isn't allowed. However, in the second case you take the initial address and incremented it by one which didn't do any changes to the array address.
When you define a pointer and assigns its value to the beginning of the array, then you are dealing with the pointer (third case). So incrementing it and decrementing it will not affect the address of the array.

Hope that helps.

Quote:
Originally Posted by bamboo View Post
i'm new to programming world, am interested in iphone dev.
right now i'm reading Stephen Kochan's book "Programming in Objective-C 2.0".
and had some problems i couldn't figured out on my own.
I posted the following question on the official forum for the book, but it seemed like no one's there. I googled this site.

"I was reading pointers on chapter13 today, and had a question.
since a array name without a subscript is a pointer to the beginning of the array,
why can't we use increment on the array name like ++array,
and when you assign it to a pointer variable, you can do that with pointer?

for example, we have a character array
char array[] = {'a','b','c','\0'}
and you cannot do this:
NSLog(@"%c", *(++array));
although this works fine:
NSLog(@"%c", *(array+1));

then you assign it to a pointer:
char *ptr = array;
then it works:
NSLog(@"%c", *(++ptr));

anyone knows why???
"
__________________
Sami Gh is offline   Reply With Quote
Old 05-12-2011, 11:47 PM   #3 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 4
bamboo is on a distinguished road
Default

thanks for your reply. I think i kinda get the idea now.
bamboo 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: 485
16 members and 469 guests
7twenty7, AlanFloyd, AppsBlogger, David-T, HemiMG, iAppDeveloper, imac74, Jaxen66, lovoyl, Music Man, mutantskin, Paul Slocum, SLIC, solardrift, unicornleo, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,683
Threads: 94,131
Posts: 402,932
Top Poster: BrianSlick (7,990)
Welcome to our newest member, unicornleo
Powered by vBadvanced CMPS v3.1.0

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