Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8331229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:21:04+00:00 2026-06-09T02:21:04+00:00

I’m reading a book to learn Objective-C and this program is suppose to show

  • 0

I’m reading a book to learn Objective-C and this program is suppose to show key concepts in dealing with pointers, and I’m really lost.

Is there some kind of conversion happening in the function’s arguments that turn p1, p2, &il, and &i2 to the value (*) of a pointer? Like p1 turns into *p1?

I thought a copy of the variable was passed into the function instead of the actual variable, so why was the value of the passed in variable changed after the function?

Also why am I getting a warning on the 3rd line that says: No previous prototype for function 'exchangeValues'?

Thank you!!

#import <Foundation/Foundation.h>

void exchangeValues (int *pint1, int *pint2) {
    int temp;

    temp = *pint1;
    *pint1 = *pint2;
    *pint2 = temp;
}

int main (int argc, char *argv[]) {
    @autoreleasepool {
        void exchangeValues (int *pint1, int *pint2);
        int il = -5, i2 = 66, *p1 = &il, *p2 = &i2;

        NSLog(@"il = %i, i2 = %i", il, i2);

        exchangeValues(p1, p2);
        NSLog(@"il = %i, i2 = %i", il, i2);

        exchangeValues(&il, &i2);
        NSLog(@"il = %i, i2 = %i", il, i2);
    }
    return 0;
}

Output:

2012-08-02 11:13:38.569 Test[381:707] il = -5, i2 = 66
2012-08-02 11:13:38.571 Test[381:707] il = 66, i2 = -5
2012-08-02 11:13:38.572 Test[381:707] il = -5, i2 = 66
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:21:06+00:00Added an answer on June 9, 2026 at 2:21 am

    I would say that’s a complex example if you are being taught about pointers!

    Is there some kind of conversion happening in the function’s arguments
    that turn p1, p2, &il, and &i2 to the value (*) of a pointer? Like p1
    turns into *p1?

    p1 and p2 are declared as int * (pointer to int) and are initialised with the address of i1 and i2 (using the & operator).

    I thought a copy of the variable was passed into the function instead
    of the actual variable, so why was the value of the passed in variable
    changed after the function?

    A copy of the variable is passed to the function, however in this case the variable of type int * (pointer to int). The reason the value is changing is because the exchangeValues() function is dereferencing those pointers and swapping the values. This is the only way (in C/Objective-C) a function can modify a variable outside of its own scope, other than the variable being assigned as the return value from a function.

    Also why am I getting a warning on the 3rd line that says: No previous
    prototype for function ‘exchangeValues’?

    You seem to have typed it in wrong; remove the line below @autoreleasepool:

    @autoreleasepool {
        void exchangeValues (int *pint1, int *pint2);   <-- delete this line
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
Specifically, suppose I start with the string string =hello \'i am \' me And
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.