Amazon.com Widgets page 201, extern ?
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 18, 2013, 05:00:12 AM
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
|-+  Programming in Objective-C, 4th edition
| |-+  Chapter 10
| | |-+  page 201, extern ?
Pages: [1]   Go Down
Print
Author Topic: page 201, extern ?  (Read 530 times)
siavashApp
Newbie
*
Posts: 17






« on: July 01, 2012, 08:25:57 AM »

The text states that: " when dealing with external variables, you can declare a variable as extern in many places, but you can define it only once."
I don't understand this statement completely. Does it mean that we can not have a method like this:

- (void) setGlobVar:(int)var
{
   extern int gGlobVar;
   gGlobVar=var;
   gGlobVar=102;
}

?

Logged
afterDark
Global Moderator
Full Member
*****
Posts: 126






« Reply #1 on: November 28, 2012, 03:29:37 PM »

as I understood it: yes, you can have a method as you described. That method would change the value of the global variable, changing it for 'everyone' accessing that variable. Also in other methods, or in other files within the program.

The word 'extern' in front of the variable definition means that this variable is already defined in another file in this program. And that we would like to access that same variable (and not create a local variable for just this file). Meaning: any changes we make to it, will be made to the global variable. Or: if any other method in any file changes it, we will see that here too.
So 'extern' is like saying "we are defining a variable here because it is unknown yet in our file. But it is already defined in some other file in this program. And we mean to access that very same variable"
Logged

I am just an amateur with Objective-C, don't let the moderator label fool you. Working my way through the book slowly.
Tpdef
Newbie
*
Posts: 7






« Reply #2 on: December 02, 2012, 09:22:25 AM »

it's mean you can define the variable only once,
 int gGlobalVar=5;//this is the definition of the global /external variable,

 and if you choose to treat it as external variable, then this is your only place that you can define it,meaning in others files you can only use the extern directive to let the compiler know that you intend to access to a global and external variabele and assign it value ,but you can't define this variable in any other files again
like in this way
 int gGlobalVar=5;
i hope you understand me :-)
Logged
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.