1

I know these question sounds really stupid or easy to you guys but i'm still kind of blurry here. Hope that you guys can clear out my doubts and help me have a firm grasp on this concept.

NSHost *instance = [NSHost currentHost];
NSString *answer = [instance localizedName];
NSLog(@"%@",answer);

For the first line of code, Does currentHost method of NSHost class return address of the instances OR all method of class return address of its instances? I should ask what does [NSHost currentHost] returns. For the 2nd line of code, Can you store a pointer in a variable? Since localizedName returns a pointer, then storing pointer in a variable(variable answer) is like storing a variable in a variable because pointer is also a variable when we first set it.I am saying this because the book I am reading says that localizedName returns a pointer to an instance of NSString.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
user3090658
  • 157
  • 2
  • 9

1 Answers1

0

For the first line of code, Does currentHost method of NSHost class return address of the instances OR all method of class return address of its instances? I should ask what does [NSHost currentHost] returns.

1) Here currentHost method is used for just creating the object of NSHost.

2) These method just use available network administration services to discover all names and addresses for the host requested.

When you call this [NSHost currentHost] you will get the below output returns:-

<NSHost 0x100b28db0> xyz-im-123.local ((
    "xyz-im-123.local",
    "XYZ-IM-123.local",
    localhost
) (
    "fe80::ca2a:14ff:fe3f:8767%en0",
    "11.2.2.34",
    "17.170.163.86",
    "fe80::1%lo0",
    "127.0.0.1",
    "::1"
))

For the 2nd line of code, Can you store a pointer in a variable? Since localizedName returns a pointer, then storing pointer in a variable(variable answer) is like storing a variable in a variable because pointer is also a variable when we first set it.I am saying this because the book I am reading says that localizedName returns a pointer to an instance of NSString.

For this second point, please follow detail in this link Understanding pointers?

Community
  • 1
  • 1
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
  • Hi the link you provided does not answer my question. – user3090658 Dec 18 '13 at 12:58
  • Can you explain second point little bit more? – Hussain Shabbir Dec 18 '13 at 13:00
  • The thing is i always thought that we must assign an address to a pointer. But the code NSHost *instance = [NSHost currentHost]; does not assign address to the pointer instance. If I were to assign the address to the pointer why dont i do something like this NSHost *instance = &[NSHost currentHost];. – user3090658 Dec 18 '13 at 13:07
  • ok can you look on this Pointers http://www.drdobbs.com/mobile/pointers-in-objective-c/225700236 – Hussain Shabbir Dec 18 '13 at 13:13
  • The website you gave me doesn't answer my question I know what everything the site says but can you please answer my question up there in the 3rd comment? – user3090658 Dec 18 '13 at 14:55
  • 1
    You are correct that NSHost *instance = [NSHost currentHost]; does not assign address to the pointer instance. Though, it just used for creating the object and assigning value to NSHost. If you want to assign the address to the pointer. then you have to write the syntax like this, NSHost *instance = [NSHost currentHost]; NSHost **pointerAddress=&(instance); NSLog(@"pointerAddress=%@",*pointerAddress); – Hussain Shabbir Dec 20 '13 at 12:11
  • 1
    Also in objective-c every class symbol append asterisk sign. So for assigning the address to the pointer you need to make double asterisk sign. If it is primitive data types then single asterisk sign will be required like that, int x = 45; int *y = &x; NSLog(@"The value was %d", *y); – Hussain Shabbir Dec 20 '13 at 12:13