I have referenced the following question:
Using pointers to emulate Pass-by-Reference in a Pass-by-Value function (C and C++)
I am attempting a very similar exercise except instead of implementing a 'swap' function, I'm attempting to implement a function that calculates the cube of an integer number. The perplexing thing for me is I get no output at all, not even the "hello world" test output. In fact all I get is the following:
process exited after 1.967 seconds with return value 3221225477
My code is as follows:
#include <stdio.h>
#include <stdlib.h>
int cube1(int *p);
int cube2(int a);
int main(int argc, char *argv[])
{
int x;
int *q;
x = 3;
*q = &x;
//output test
printf("hello world\n");
printf( "x = %d\n", x );
printf( "q = %p\n", q );
printf("%d cubed using variable passing by reference = %d\n", x, cube1(x));
printf("%d cubed using variable passing by value = %d\n", x, cube2(x));
system("pause");
return 0;
}
//simulated pass by reference
int cube1(int *p)
{
int temp = *p;
temp = temp*temp*temp;
*p = temp;
return *p;
}
//standard pass by value
int cube2(int a)
{
return a*a*a;
}