Thread: Can not find error in this code

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Can not find error in this code

    I am trying to compare how different functions work and I do not see whats wrong here. I expect to see 100 and 200 on the printf after the second function call and I get 2...2 no matter what I do (changed it from + to *). Can someone look and let me know the problem? Thanks.
    Code:
       void swap(int *swapa, int *swapb);
       
       int swap2(int swapa);
    
       main here....
    
             int aa = 1;
             int bb = 2;
    
             /*This works fine*/
             printf("%d is aa before function and %d is bb before function\n",aa, bb);
             swap(&aa, &bb);
             printf("%d is aa after function and %d is bb after function\n",aa, bb);
         
             /*Here is where problem is*/ 
             swap2(aa);
             printf("%d is aa now\n", aa); //
         
             swap2(bb);
             printf("%d is bb now\n", aa);
    
             functions here...
    
            void swap(int *swapa, int *swapb)
             { /*params are int* instead of int*/
             int temp;
             temp = *swapa; /* use * to follow the pointer back to the caller's memory*/
             *swapa = *swapb;
             *swapb = temp;
             }
        
    
             int swap2(int swapa)
             {
             int temp =100;
             int result = swapa + temp;
             return (result);
             }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Um, swap2 does not appear to swap anything. On the contrary, it returns an integer 100 greater than its argument. Since you do not use its return value when calling it, calling it has no net effect.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Thanks but why is aa & bb = 2

    Thanks I made the correction below:
    Code:
              aa = swap2(aa);
              printf("%d is aa now\n", aa);
         
              bb = swap2(bb);
              printf("%d is bb now\n", aa);
    and they both = 102 which is confusing because when doing a printf after 1st swap function it reads:

    2 is aa now and 1 is bb now

    Why are they not 101 and 102?'

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    printf("%d is bb now\n", aa);
    Notice anything special?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    The code you posted can't be the same as is running, since the output text is different.
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Yes I found that special something

    Thanks for your help! What a clumsy mistake.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code to find th square root of a number
    By CODY21 in forum C++ Programming
    Replies: 34
    Last Post: 10-29-2010, 09:27 AM
  2. Replies: 12
    Last Post: 06-08-2005, 11:23 AM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum C# Programming
    Replies: 0
    Last Post: 10-14-2002, 01:26 PM
  4. Code to find the day type of day in the year?
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 04-01-2002, 08:58 PM
  5. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM