-2
#include <stdio.h>

void circle(int *,int *,int *);

int main() {
    int x,y,z;
    printf("please enter 3 numbers = ");
    scanf("%d %d %d",&x,&y,&z);

    circle(&x,&y,&z);

    printf("x=%d  y=%d  z=%d",x,y,z);


}

void circle(int *a,int *b ,int *c) {
    int *d;
    d=*a;
    *a=*b;

    *b=*c;
    *c=d;
}

The above code is for circularly exchanging the values of x, y, z using functions. In the above code I have used *d or d is an unsigned integer, still if in

d=*a 

if x (or a) = -1 then also the code works.

Student
  • 805
  • 1
  • 8
  • 11
  • 1
    Don't ignore the warnings your compiler gives you - it means your code is wrong. If you're not getting warnings (cos you really should with the above code), turn them on – Chris Turner Jul 26 '18 at 16:11
  • btw, good naming of functions doesn't hurt. I would've expected this to be a drawing function... –  Jul 26 '18 at 16:13
  • `d=*a;` is assigning integer to pointer. `*c=d;` is assigning pointer to integer. Perfect recipe for troubles. – Eugene Sh. Jul 26 '18 at 16:26

2 Answers2

1
void circle(int *a, int *b, int *c) {
  int *d;
  d = *a;
  ...

Here d = *a; puts an int into a pointer.

Decent compilers should issue a warning such as:

'=': 'int *' differs in levels of indirection from 'int'

As the size of int is usually smaller or equal than the size of a pointer, your code still works, but it is undefined behaviour, it may not work on another platform. Undefined behaviour includes "apparetly working fine".

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
0

If you are wondering why your program still works when you assign a negative value to an unsigned integer, you should know what happens when you assign a negative value to an unsigned integer on this link:

What happens if I assign a negative value to an unsigned variable?

Farooq Hanif
  • 1,779
  • 1
  • 15
  • 22