Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
August 22, 2017, 05:43:16 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Double declaration in 13.14  (Read 6004 times)
Qpido
Newbie
*
Posts: 7


« on: May 11, 2012, 04:43:44 AM »

I don't quite understand why you defined this in the main part of the file in program 13.14.

Code: (Objective-C)
        void exchange (int *pint1, int *pint2);

This
Code: (Objective-C)
void exchange (int *pint1, int *pint2)
{
    int temp;
    
    temp = *pint1;
    *pint1 = *pint2;
    *pint2 = temp;
}

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        int i1 = -5, i2 = 66, *p1 = &i1, *p2 = &i2;
        
        NSLog(@"i1 = %i, i2 = %i", i1, i2);
        exchange(p1, p2);
        NSLog(@"i1 = %i, i2 = %i", i1, i2);
        exchange(&i1, &i2);
        NSLog(@"i1 = %i, i2 = %i", i1, i2);

Works just a fine as
Code: (Objective-C)
void exchange (int *pint1, int *pint2)
{
    int temp;
    
    temp = *pint1;
    *pint1 = *pint2;
    *pint2 = temp;
}

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        void exchange (int *pint1, int *pint2);
        int i1 = -5, i2 = 66, *p1 = &i1, *p2 = &i2;
        
        NSLog(@"i1 = %i, i2 = %i", i1, i2);
        exchange(p1, p2);
        NSLog(@"i1 = %i, i2 = %i", i1, i2);
        exchange(&i1, &i2);
        NSLog(@"i1 = %i, i2 = %i", i1, i2);

You mention something about this I think earlier in the chapter, but I don't quite get it still.

You say we should put them in a different sheet and just import them, so  "void exchange (int *pint1, int *pint2);" isn't necessary right?

Thanks

Q~
Logged
eagle74
Newbie
*
Posts: 24


www.cucinareblog.it


WWW Email
« Reply #1 on: June 06, 2012, 06:02:27 AM »

HI Qpido, I have the same problem as you, I really can't understand why it's declared twice, could it be an error of typing?? Roll Eyes

...hope someone could help us... Huh Huh Huh
Logged

"The more i know people the more i love dogs."Socrate
clouded
Full Member
***
Posts: 123


« Reply #2 on: June 07, 2012, 02:01:55 PM »

The lesson was about trading pointer objects... but what you guys seem to be confused about is the structure of functions.

Basically this is it:

Code: (Objective-C)
#include <file.names>
//function prototype; <-- not always necessary, depends on compiler

// function declaration
{
     function calculations;
}

// main.m
{
     function(call)
     return 0;
}

The way Qpido put it in his main may work as the function prototype, but i'm not sure...
What I think Qpido is confused about is the difference between a method and a function, very similar, but the differences are encapsulation, privacy, and recursion... it's very important to know the differences between all of these and their uses.
Logged
eagle74
Newbie
*
Posts: 24


www.cucinareblog.it


WWW Email
« Reply #3 on: June 07, 2012, 10:55:03 PM »

Hy clouded, thanks for your precious explanation  Wink

I looked back in the test and I saw that the double declaration is used only when function is dealing with pointer, for example program 13.7(block to calculate the nth triangular number) doesn't use it.

is it correct?  Huh
Logged

"The more i know people the more i love dogs."Socrate
Pages: [1]   Go Up
  Print  
 
Jump to:  

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 ゥ 2009 classroomM.com. All rights reserved.