void swap (int *first, int *second)
{
  int temp = *first;
  *first = *second;
  *second = temp;
}

int main ()
{
  int i = 5, j = 6;
  swap (i, j);
  printf ("i=%d j=%d\n", i, j);
}

warning:

[37] $ gcc -o ex321 ex321.c
ex321.c: In function `main':
ex321.c:14: warning: passing arg 1 of `swap' makes pointer from integer without a cast
ex321.c:14: warning: passing arg 2 of `swap' makes pointer from integer without a cast

Dani AI

Generated

The compiler warnings in 's example point to a type mismatch: the function was declared to accept pointer parameters but the call passed values of a different kind. That mismatch forces the compiler to convert between incompatible types, which is nonportable and often leads to undefined behaviour. correctly identified that the call-side and the function signature must agree.

To debug and prevent this class of warnings: make sure the function prototype is visible at the call site; include the correct standard headers for library functions you use; and run a build with a strict set of warnings and static analysis so these mismatches are caught early. Do not silence the compiler by casting integers to pointer types — that hides bugs and breaks portability. For background on conversions and why pointer/integer conversions are dangerous, see the C language guidance on conversions and the compiler warning documentation for practical flag choices (cppreference on conversions, GCC warning options).

If swapping two variables is the goal and you want a compact alternative that does not require pointer parameters, a simple macro can be used for same-typed lvalues (watch out for side effects in the macro arguments):

#define SWAP(type,a,b) \
  do { type _tmp = (a); (a) = (b); (b) = _tmp; } while (0)

For safer, maintainable code in larger projects, prefer clear function prototypes, unit tests, and static tools such as cppcheck or clang-tidy to keep these kinds of mistakes from reaching production.

Recommended Answers

All 2 Replies

Add -Wall option when you compile. (ignore!!_

Sorry another brain fart. You need to pass the address of the parameters to your swap function like so:

swap (&i, &j);

Thank you!!! I also had forgot to return something in main().

Problem Resolved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.