The code is invalid. At least the compiler should issue a message that there are assignments (initializations) of pointers of different types without castings.
That is there is no implicit conversion between the pointer types int *, the type of the parameters, and int **, the type of argument expressions.
In general the function invokes undefined behavior because it is not necessary that an object of the type int is large enough to be able to store a pointer value.
It seems your program works either because sizeof( int * ) is equal to sizeof( int ) or values of addresses are not large and can be stored in objects of the type int. Or there are swapped only low-significant parts of the pointers that are equal in size to sizeof( int ) while most significant parts are the same for the both pointers.
To show that the code is invalid just use one pointer that points to a variable with automatic storage duration and other that points to a dynamically allocated memory.
Here is a demonstration program.
#include <stdio.h>
#include <stdlib.h>
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main( void )
{
int x;
int *ptr_x = &x;
int *ptr_y = malloc( sizeof( int ) );
printf( "ptr_x = %p\n", ( void * )ptr_x );
printf( "ptr_y = %p\n", ( void * )ptr_y );
swap( ( int * )&ptr_x, ( int * )&ptr_y);
printf( "ptr_x = %p\n", ( void * )ptr_x );
printf( "ptr_y = %p\n", ( void * )ptr_y );
// This can produce a run-time error
//free( ptr_x );
}
The program output might look like for example
ptr_x = 0x7ffdcf002d98
ptr_y = 0x564608597eb0
ptr_x = 0x7ffd08597eb0
ptr_y = 0x5646cf002d98
As you can see there are swapped only less significant parts of the pointers that fit in object of the type int.
That is the expressions *a and *b within the function swap do not yield full values of pointers because they have the type int.
As a trick you could declare and define the function the following way:)
#include <stdint.h>
void swap(intptr_t *a, intptr_t *b)
{
intptr_t temp = *a;
*a = *b;
*b = temp;
}
//...
swap( ( intptr_t * )&ptr_x, ( intptr_t * )&ptr_y);
sizeof(int)just so happen to be equal tosizeof(int *)? Or if you're on a 64-bit system, doesn't the pointers use all 64 bits available to it, and the endianness just so happens to be so that the bits of the pointers match that of a 32-bitint? Try using pointers created some other way, for example global variables or returned bymalloc.