Amazon.com Widgets Question about pointers
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 21, 2013, 05:14:34 PM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Chapter Study
| | |-+  Chapter 15 - Numbers, Strings, and Collections
| | | |-+  Question about pointers
Pages: [1]   Go Down
Print
Author Topic: Question about pointers  (Read 563 times)
webstyles
Newbie
*
Posts: 4


WWW




« on: June 22, 2011, 04:54:17 AM »

Hi All,

I just started with OBJ-C yesterday (coming from 10+ years of php).

I started reading a pdf on OBJ-C that suggested I check out C first, so I read a pdf on C.

From what I understand, C could not really handle strings, so what it does is it creates an array of separate characters and returns the pointer for the first character (is this correct?)

Based on that (and taking into account I come from a very long background of PHP where variables don't need to be declared, types can be changed whenever needed, types can change automatically, etc)

Is this C issue with strings still in effect?

I ask this because xcode will give me an error if I try to do this:

NSString var1 = @"Hello World";

but this works

NSString *var1 = @"Hello World";

It's baffling my mind mainly because I sort of feel things should be more evolved now (like in C# where pointers are no longer an issue)

Is there a way to keep things simpler in OBJ-C ? (i.e., what gest to me is the fact that an int can be int var1 = 1; but strings must be pointers?Huh)

thanks, and sorry if this sounds a bit off the top and confusing (but year, I am confused.)

Regards
Logged

Random ramblings of a dangerous mind: www.simonfranco.com
fujilla
Global Moderator
Sr. Member
*****
Posts: 270


WWW




« Reply #1 on: June 22, 2011, 05:47:52 AM »

Here goes, my first attempt at answering a question.

Your second statement "NSString *var1 = @"Hello World";" works because the * is saying you are creating an object called var1 based on the NSString class.

With regards to learning C first, Steve's book assumes no knowledge of C to teach you Objective-C, and he does go into C in the book anyway.
Logged
webstyles
Newbie
*
Posts: 4


WWW




« Reply #2 on: June 22, 2011, 06:45:53 AM »

Thanks for your answer.

Correct me if I'm wrong, but doesn't the fact that I start with the type definition (NSString) already say I'm creating an object based on the NSString class?

My question is more related to the fact that it seems strings MUST be pointers (i.e. with the * before the var name)...
Or does the * not mean the same in OBJ-C ?

To put it another way: is it possible to define strings without the * ?

thanks
Logged

Random ramblings of a dangerous mind: www.simonfranco.com
fujilla
Global Moderator
Sr. Member
*****
Posts: 270


WWW




« Reply #3 on: June 22, 2011, 06:55:07 AM »

Sorry, yes, the * is a pointer to an object, whereas the "int var1 = 1" as stated in your original question is creating a variable.

All objects are accessed using pointers.
« Last Edit: June 22, 2011, 07:02:27 AM by fujilla » Logged
webstyles
Newbie
*
Posts: 4


WWW




« Reply #4 on: June 22, 2011, 07:34:51 AM »

thanks for taking the time to answer again... My question still stands though...

I'll ask it yet another way:

Can I create a variable (that holds a string) without using the * (i.e. without using a pointer) ?


thanks
Logged

Random ramblings of a dangerous mind: www.simonfranco.com
fujilla
Global Moderator
Sr. Member
*****
Posts: 270


WWW




« Reply #5 on: June 22, 2011, 08:11:05 AM »

Although I am new to this myself, I am pretty sure you cannot create a string variable, other than creating a NSString object.

Also, NSString cannot be changed once declared, so you will have to use NSMutableString if you need to be able to change it at some point.

You may come across the char datatype, however this is only a one character datatype.

Nick

p.s. Sorry it has taken so long to reply, I keep getting the message "The last posting from your IP was less than 300 seconds ago. Please try again later." when I press the post button.
Logged
webstyles
Newbie
*
Posts: 4


WWW




« Reply #6 on: June 22, 2011, 08:17:34 AM »

Thanks.
This clarifies my question. Even though I don't like the answer. (I like to keep things coherent and having to use pointers every time I need a string, but not when i use int or float is just weird for me. It's the 10+ years of PHP and how easy variables and arrays are there that's messing up my learning curve here)

Thanks again, I expect I'll be here everyday with more silly questions.

Regards

Simon
Logged

Random ramblings of a dangerous mind: www.simonfranco.com
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.