Solved

Question about asterisk

Posted on 2008-10-29
5
931 Views
Last Modified: 2013-11-25
I have been 'programming' in Access and VBA for years and outside of High School and College programming classes years ago, that is the extent of my programming.  I am now trying to learn Objective-C to create Iphone apps.  I got a couple great books and found some great tutorial sites, but I have some 'basic' questions that I can not seem to find the answers to anywhere.  One of which is regarding the use of the asterisk.  One of my references mentions this is a method for using a pointer, but I don't understand why sometimes it is used and sometimes it is not.  In reference to they code below, it looks like there are two ways an asterisk is being used.  One is in front of a variable name, such as name, description, and imageURL.  The other is in the last line after 'NSString'.  

I am not a type that will just type something because I am told to.  I gotta understand why :)

Thanks in advance,

Michael
@interface Animal : NSObject {

	NSString *name;

	NSString *description;

	NSString *imageURL;

}
 

@property (nonatomic, retain) NSString *name;

@property (nonatomic, retain) NSString *description;

@property (nonatomic, retain) NSString *imageURL;
 

-(id)initWithName:(NSString *)n description:(NSString *)d url:(NSString *)u;

Open in new window

0
Comment
Question by:UniqueData
  • 2
  • 2
5 Comments
 
LVL 24

Accepted Solution

by:
fridom earned 250 total points
ID: 22838721
* indicates a pointer to something.

So if you have
NSString it's a data-strucutre iwth all kind of elements it uses all the space it needs

if you have NSString *str; than str is pointer it means an indirektion.

Let's make an example, in "the normal world"

I know where you can buy some Coke, you don't know. But you also do not know me. But you know someone knowing me. So you can ask  this
someone to ask me where  one can get a coke. That's the indirection. The guy you know if  the * in between ;-)

In computer terms the * is a pointer to some location  in memory where (hopefully the right) thing is referenced.

This is better explained in any book about learning C. So try to get your hand on one of those books

Regards
Friedrich
0
 
LVL 7

Author Comment

by:UniqueData
ID: 22842139
but what about line #11 in the code above.  There is no variable name, just (NSString *)
0
 
LVL 5

Assisted Solution

by:AJVienna
AJVienna earned 250 total points
ID: 22844093
-(id)initWithName:(NSString *)n description:(NSString *)d url:(NSString *)u;

the return type is: id, which is an object in Objective-C
the method name is "initWithName"
it has three arguments. All three arguments are NSString*.
The variables they are placed in are n, d and u. You call it
like this:

Animal *animal = [[Animal alloc] initWithName:@"name" description:@"some description" url:"www.animals.com"]

So there are variable names. So I do not get your point.
0
 
LVL 5

Expert Comment

by:AJVienna
ID: 22844138
Another example (plain C) which I hope you will find useful to understand pointers.
The * is used for declaring pointers and dereferencing them (to understand this look at the example).
The & is used to get the address (in memory) of another variable.
int a = 3, b = 4;

int *p = NULL;

p = &a; // p now points to the memory address a is stored in

*p=2;   // now we write 2 to the memory address p is pointing to

// a now contains 2

p = &b; // now p points to the address where b is stored.

*p=5;

// b now contains 5
 

// you can also have pointers to pointers

int **pp = NULL

pp=&p;

// now pp is pointing to the address of p

*pp=&a;

// now the memory at address of p is changed to point to a 

*p=10;

// thus the line above changes the memory add address a and gives 

// thus a is now 10.

Open in new window

0
 
LVL 7

Author Comment

by:UniqueData
ID: 22898342
Thank you much for your posts.  I guess the thing that threw me off on the (NSString *)d is that the * was within the (), I would expect it directly in front of the variable, but I think I get it.
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now