Home>

I want to exchange pointer values, but it doesn't work and I'm stumbled
I would be grateful if you could give me a recommended reference book that describes how to use the pointer
It would be helpful if you could give me advice on the code

code
#include<stdio.h>
int input_int (int * pd);
void swap_int (int * pdata1, int * pdata2);
int main (void)
{
    int data1, data2;
    while (input_int (&data1)! = EOF&&input_int (&data2)! = EOF) {
        printf ("** Before exchange ** \ n");
        printf ("data1 =% d \ n", data1);
        printf ("data2 =% d \ n", data2);
        swap_int (&data1,&data2);
        printf ("** After exchange ** \ n");
        printf ("data1 =% d \ n", data1);
        printf ("data2 =% d \ n", data2);
        printf ("\ n");
    }
    return 0;
}

void swap_int (int * pdata1, int * pdata2)
{
  int tmp;
    tmp = * pdata1;
    * pdata1 = * pdata2;
    * pdata2 = tmp;

    printf ("Input num:");
    return scanf ("% d% d",&* pdata1,&* pdata2);


}


Error details
swpint.c: In function 'swap_int':
swapint.c: 48: 10: warning: 'return'with a value.in function returning void
return scanf ("% d% d",&* pdata1,&* pdata2);
swapint.c38: 6note: declared here
void swap int (int * pdata, int * pdata2)
Is the error message

The execution image is
input num: 10
input num: 20
** Before replacement **
data1 = 10
data2 = 20
** After exchange **
data1 = 20
data2 = 10
Execution image when it can be executed

c
  • Answer # 1

      

    data1 = data2;

    * data1 = * data2;

  • Answer # 2

    Warning is a function that does not return a value (that is, void), but it is trying to return a value.
    ・ Stop return
    ・ A function that is not void
    Either.

    But before, the questioner himself probably doesn't understand the swaip_int () function.
    What kind of input (it is an argument), what kind of processing is performed, what is returned (or nothing is returned),
    Please organize.

    If you stop

    return and create input_int (), I think the expected behavior is for the time being.