I am trying to write a program that takes two integers from the user and assigns them to pointers, and then passes those pointers to the function to multiply them and print the result. However the output is always 0. I'm a beginner and I'm failing to understand what I'm doing wrong. Help is appreciated. Here's my code:
#include <stdio.h>
int x, y, m;
void point(int *xPtr, int *yPtr);
int main()
{
int *xPtr, *yPtr;
printf("Enter two integers: \n");
scanf("%d %d", &x, &y);
point(&x, &y);
m = (*xPtr)*(*yPtr);
xPtr = &m;
printf("The result is %d", *xPtr);
return 0;
}
void point(int *xPtr, int *yPtr)
{
xPtr = &x;
yPtr = &y;
}
in main the variables xPtr and yPtr are never initialized
the assignments in point are local and without consequence out of point, if you want to modify the variables having the same name in main you can do :
void point(int **xPtr, int **yPtr)
{
*xPtr = &x;
*yPtr = &y;
}
also changing the declaration of point of course, and change the call in main to be :
point(&xPtr, &yPtr);
... However the output is always 0
I don't think you have any output with the program you give because you very probably cannot reach the print, you deference non initialized pointers, the behavior is undefined and generally catastrophic like a segmentation fault
Modifying your program :
#include <stdio.h>
int x, y, m;
void point(int **xPtr, int **yPtr);
int main()
{
int *xPtr, *yPtr;
printf("Enter two integers: \n");
scanf("%d %d", &x, &y);
point(&xPtr, &yPtr);
m = (*xPtr)*(*yPtr);
xPtr = &m;
printf("The result is %d", *xPtr);
return 0;
}
void point(int **xPtr, int **yPtr)
{
*xPtr = &x;
*yPtr = &y;
}
Compilation and executions :
pi@raspberrypi:/tmp $ gcc -Wall c.c
pi@raspberrypi:/tmp $ ./a.out
Enter two integers:
2 3
The result is 6pi@raspberrypi:/tmp $
pi@raspberrypi:/tmp $
pi@raspberrypi:/tmp $ valgrind ./a.out
==5992== Memcheck, a memory error detector
==5992== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==5992== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==5992== Command: ./a.out
==5992==
Enter two integers:
2 3
The result is 6==5992==
==5992== HEAP SUMMARY:
==5992== in use at exit: 0 bytes in 0 blocks
==5992== total heap usage: 2 allocs, 2 frees, 2,048 bytes allocated
==5992==
==5992== All heap blocks were freed -- no leaks are possible
==5992==
==5992== For lists of detected and suppressed errors, rerun with: -s
==5992== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
pi@raspberrypi:/tmp $
Out of that better :
\n
because as you can be above the result is not readable executing your program in a terminal,About the last remark and using your version of the code :
pi@raspberrypi:/tmp $ gcc -g -Wall cc.c
cc.c: In function ‘main’:
cc.c:16:10: warning: ‘xPtr’ is used uninitialized in this function [-Wuninitialized]
m = (*xPtr)*(*yPtr);
~^~~~~~
cc.c:16:18: warning: ‘yPtr’ is used uninitialized in this function [-Wuninitialized]
m = (*xPtr)*(*yPtr);
~^~~~~~
If anyway I execute it under valgrind :
pi@raspberrypi:/tmp $ valgrind ./a.out
==7143== Memcheck, a memory error detector
==7143== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==7143== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==7143== Command: ./a.out
==7143==
1 2Enter two integers:
==7143== Use of uninitialised value of size 4
==7143== at 0x104BC: main (cc.c:16)
==7143==
==7143== Invalid read of size 4
==7143== at 0x104BC: main (cc.c:16)
==7143== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==7143==
==7143==
==7143== Process terminating with default action of signal 11 (SIGSEGV)
==7143== Access not within mapped region at address 0x0
==7143== at 0x104BC: main (cc.c:16)
==7143== If you believe this happened as a result of a stack
==7143== overflow in your program's main thread (unlikely but
==7143== possible), you can try to increase the size of the
==7143== main thread stack using the --main-stacksize= flag.
==7143== The main thread stack size used in this run was 8388608.
==7143==
==7143== HEAP SUMMARY:
==7143== in use at exit: 0 bytes in 0 blocks
==7143== total heap usage: 2 allocs, 2 frees, 2,048 bytes allocated
==7143==
==7143== All heap blocks were freed -- no leaks are possible
==7143==
==7143== Use --track-origins=yes to see where uninitialised values come from
==7143== For lists of detected and suppressed errors, rerun with: -s
==7143== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
Erreur de segmentation
pi@raspberrypi:/tmp $