pointers!!!

This is a discussion on pointers!!! within the C++ Programming forums, part of the General Programming Boards category; i have to write code for swapp() using int* as an argument.code is as follows--> #include<iostream> void swap_(int* a, int*b){ ...

  1. #1
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    Unhappy pointers!!!

    i have to write code for swapp() using int* as an argument.code is as follows-->

    #include<iostream>

    void swap_(int* a, int*b){

    int c=(int) a;

    int m=(int) b;

    int n=c;

    cout<<" the swapped value of a and b are"<<m<<"\t"<<n;

    }

    void main()

    {
    cout<<" \nenter a and b";
    int m,n;
    cin>>m>>n;
    int *l=&m;
    int *o=&n;
    swap_( l, o );
    }


    is it right or am i doing wrong??
    help me out.
    actually what is happening is that 'a' is storing the address of 'm' and 'b' is storing the adress value of 'n'.
    like this

    for example
    -----------
    the value in a= | 0xffcd | (this is address of m)
    -----------
    -----------
    the value in b= | 0xffce | (this is address of n)
    -----------

    now when i swap it.. value in a and b are swapped but i never get the value in integer.. i mean like if
    m=4,n=5. after swapping it should be m=5;n=4;and let me remind you that i am restricted to use only int* as the argument.
    would be glad to see some help'S message around.
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  2. #2
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77
    that's
    --------- ---------
    | 0xffcd| and | 0xffce|
    --------- ---------
    ;-)
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    look at the following:
    Code:
    #include <iostream.h>
    
    void swap(int* a, int* b)
    {
         int temp = *a;
         *a = *b;
         *b = temp;
    }
    
    int main()
    {
          int a = 4;
          int b = 16;
    
          swap(&a,&b);
          cout << a << " " << b;
          return 0;
    }
    whenever you need to do a swap, you'll need a temporary variable.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by The Dog
    whenever you need to do a swap, you'll need a temporary variable.
    Not necessarily. Here's another version of the swap function:
    Code:
    void swap(int* a, int* b)
    {     
    	*a^=*b^=*a^=*b;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    > *a^=*b^=*a^=*b;
    Very disappointing

    Even without that, this is a complete disaster if a and b happen to be pointing at the same location.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    > *a^=*b^=*a^=*b;
    Even without that, this is a complete disaster if a and b happen to be pointing at the same location.
    --------------------
    Yes, but the request was for an int swapper, and :
    from the faq:
    If the values are integers, a well-known trick using exclusive-OR could perhaps be used,....
    The fact it doesn't work for other structural types is irrelevant as this is not required here.

    And if the programmer passes pointers that are using the same end variable, then surely that's a bug in itself? I know this code doesn't highlight that fact though, so I suppose it's bad because its hiding a programmer's bug?

    Or is there something else your trying to tell me here?

    And next time, don't be so darn blunt.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >If the values are integers, a well-known trick using exclusive-OR could perhaps be used,....
    XOR swapping is also well known to be scary and no self-respecting C or C++ programmer would be caught dead using it. Yes it may work for integers, but you can't be sure of this. Better to use a temporary variable. Now for my next rant...

    >*a^=*b^=*a^=*b;
    .
    .
    >The fact it doesn't work for other structural types is irrelevant as this is not required here
    The types in general are irrelevant since this swap is wrong and results in undefined behavior. The modification of a variable more than once in between sequence points is undefined. The "correct" (if you can call it that) way would look something like this:
    Code:
    void swap(int* a, int* b)
    {     
      *a^=*b;
      *b^=*a;
      *a^=*b;
    }
    -Prelude
    My best code is written with the delete key.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >and no self-respecting C or C++ programmer would be caught dead using it.
    I agree, I wouldn't and haven't, if nothing, it's ugly. I only gave it to "prove a point". But it didn't (well, sort of), so I live and learn

    >Yes it may work for integers, but you can't be sure of this.
    Why can't you be sure of it (assuming you're using the "expanded" version of the code)?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Why can't you be sure of it
    What if both items you are swapping are the same item in memory? Even if the type is int, the result is something similar to putting tin foil in the microwave. And before you say that something like that shouldn't happen, remember how often impossible situations do happen.

    -Prelude
    My best code is written with the delete key.

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >same memory
    I'm glad that's all the problem is, I thought I was going mad, not being able to see another problem! Yes, I see what you mean, that particular occasion was covered ealier by Salem (and eskimo).

    For the purposes of the example, I was relying on the fact it wouldn't happen, but I suppose, in the real world, black magic can happen, and make things go a little strange!

    Anyway, enough of this convo, it reached it's end about 5 posts ago!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >and make things go a little strange!
    Strange behavior in C and C++? Never!

    >Anyway, enough of this convo, it reached it's end about 5 posts ago!
    That never stopped us before.

    -Prelude
    My best code is written with the delete key.

  12. #12
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    ok..

    tell me something

    void swap ( int* a , int* b )
    {
    /*cant i just swap the memory address???or is it just not possible??*/

    // and cant i print the swapped value inside this function ???

    //rather than printing in main()

    // what i mean to say is that.

    //now swapp the address and print the value stored

    //in a and b

    //cant i do that???

    //help me guys.

    //now i know that a= 0xfff ..and b= 0xfffe...

    int c = a;//c gets a's value (address of m)

    a = b; // a gets b's value ( address of n)

    b = c ;// b gets a's value ( address of m)

    //now what i want to do is that print the swapped value of m

    //and n right here using this method( not in main()).

    i would love to hear..help me



    void main(){
    //codes....i wanna use m and instead of a and b.
    int( &m, &n);}
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  13. #13
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > void main()
    ROFL LMAO!!!!!

    After this entire thread too..
    The world is waiting. I must leave you now.

  14. #14
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77
    Originally posted by Shadow
    > void main()
    ROFL LMAO!!!!!

    After this entire thread too..


    whats that now????
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  15. #15
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    This was just a long thread about undefined behavior.
    void main is undefined behavior.
    Undefined behavior is very bad.
    The world is waiting. I must leave you now.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 11:23 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 06:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21