C
CViniciusM
Hello,
The output of the code below is:
number = 10
number = 0 (or other number except 15)
Why the output of the second 'number' is not 15? Why the 'number' address is
not changed?
Thanks in advance, Vinicius.
#include <stdio.h>
#include <stdlib.h>
void change_int(int *num);
int main(void)
{
int *number;
number = (int *) malloc(sizeof(int));
*number = 10;
printf("number = %d\n", *number);
change_int(number);
printf("number = %d\n", *number);
free(number);
system("PAUSE");
return 0;
}
void change_int(int *num)
{
int *temp;
temp = num;
num = (int *) malloc(sizeof(int));
*num = 15;
free(temp);
}
The output of the code below is:
number = 10
number = 0 (or other number except 15)
Why the output of the second 'number' is not 15? Why the 'number' address is
not changed?
Thanks in advance, Vinicius.
#include <stdio.h>
#include <stdlib.h>
void change_int(int *num);
int main(void)
{
int *number;
number = (int *) malloc(sizeof(int));
*number = 10;
printf("number = %d\n", *number);
change_int(number);
printf("number = %d\n", *number);
free(number);
system("PAUSE");
return 0;
}
void change_int(int *num)
{
int *temp;
temp = num;
num = (int *) malloc(sizeof(int));
*num = 15;
free(temp);
}