Thanks for pointing me in the right direction. I googled and got help from Binky Pointer Fun Video along with the reading material Pointer Basics.
Now I finally grasp the concept of pointers I think, thanks to the author differentiating and terming the words Pointers & Pointees.
Here's the simplest pointer code I could find. Example code 3 works but when I try to modularize the same code into its own function in Example code 4 then it no longer works.
Can somebody show me how to do this the correct way?
Example code 3 - [ OK ]
Code:
#include <stdio.h>
#include <stdlib.h>
void input(int *, int *);
//int main(int argc, char *argv[])
int main(void)
{
// 1
int* x;
int* y;
// 2
x = malloc(sizeof(int));
y = malloc(sizeof(int));
// 3
input(x, y);
// 4
*x = 1;
*y = 3;
// 5
// x = y;
// 6
printf("x = %d\n", *x);
printf("y = %d\n\n", *y);
system("PAUSE");
return 0;
}
/*************
* Functions
*************/
/* ___________________________________________________________________________*/
/* Input */
void input(int* x, int* y)
{
}
/* ___________________________________________________________________________*/
Example code 4 - [ Error ]
Code:
#include <stdio.h>
#include <stdlib.h>
void input(int *, int *);
//int main(int argc, char *argv[])
int main(void)
{
// 1
int* x;
int* y;
// 2
// x = malloc(sizeof(int));
// y = malloc(sizeof(int));
// 3
input(x, y);
// 4
// *x = 1;
// *y = 3;
// 5
// x = y;
// 6
printf("x = %d\n", *x);
printf("y = %d\n\n", *y);
system("PAUSE");
return 0;
}
/*************
* Functions
*************/
/* ___________________________________________________________________________*/
/* Input */
void input(int* x, int* y)
{
// 1
// int* x;
// int* y;
// 2
x = malloc(sizeof(int));
y = malloc(sizeof(int));
// 4
*x = 1;
*y = 3;
// 5
}
/* ___________________________________________________________________________*/