/*hey Guys Check this code and point all the problems in, i remember during the 
time i was in school this was my swap code*/
#include<stdio.h>
main(){
    printf("It Works\n");
    // Declaring variables and assigning values to it 
    int a=1;
    int b=2;
   // Calling the swap function and passing memory address as parameters
    swap(&a,&b);
    // pausing the screen using windows if linux please use getchar();
    
    //getchar();
    // uncomment this when using other platform.
    system("pause");
    }
    
   // declaring a pointer
    int swap(int *x, int *y)
    {
        printf("Before Swap \n");
        printf("X = %d ,Y = %d\n\n", *x,*y);
        // using pointers
           int temp=0;
           temp= *x;
           *x = *y;
           *y = temp;
        printf("After Swap\n");
        printf("X = %d, Y = %d\n",*x,*y);
        }


Prince Annan Koomson

--- On Mon, 2/22/10, Paul Herring <[email protected]> wrote:

From: Paul Herring <[email protected]>
Subject: Re: [c-prog] Re: swap two numbers
To: [email protected]
Date: Monday, February 22, 2010, 8:28 AM







 



  


    
      
      
      On Mon, Feb 22, 2010 at 5:54 AM,  <[email protected]> wrote:

> i'm confused. What's wrong with your suggestion? Why would you need to 
> allocate 'temp' if you're swapping the value (a number) at the location? 'A' 
> and 'B' already exist somewhere.



A and B (are copies of pointers that) point to something that has a size.



Temp is just a pointer that doesn't point anywhere.



The statement



Temp = A;



places whatever the _copy_ of pointer A in Temp, not the original

pointer. So calling the function with (e.g.):



int x, y;

Swap(&x, &y);



Won't actually swap anything - x will remain x and y will remain y.



The alternative John mentions whereby the statement becomes



*Temp = *A;



Is potentially worse, because it then copies whatever A points to into

whatever Temp points to, and the code, currently, doesn't tell Temp

where to point to. That ignores the facts that

1) there is no 'Void' type (it has a small v)

2) the assignment has no meaning, because 'void' has no size - i.e. a

void pointer points to an area of memory of indeterminate size. Even

MSVC refuses to compile such statements.



> Quoting John <jm5...@gmail. com>:

>

>> --- In c-p...@yahoogroups. com, Benjamin Scott

>> <benjamin_cail_ sc...@... > wrote:

>>>

>>> You can always use the following.

>>> Â

>>> Void Swap(Void *A, void *B)

>>> {

>>>   Void *Temp

>>>

>>>  Temp = A

>>>  A = B

>>>  B = temp

>>> };

>>

>> Did you mean something like:

>>

>>   *temp = *A

>>   *A = *B

>>   *B = *temp

>>

>> I hope not, because apart from not allocating memory for *temp, it is

>> never going to work because you cannot dereference a void pointer.

>>

>> Please try compiling your code before posting.



-- 

PJH



http://shabbleland. myminicity. com/env

http://www.chavgang s.com/register. php?referer= 9375



    
     

    
    


 



  






      

[Non-text portions of this message have been removed]

Reply via email to