Why does my C program output the memory address instead of the value?
I'm basically just taking user inputs as an array of ints and then trying to loop through the array to print each element out, but as soon as the program runs, it outputs I guess the 'memory addresses' of the elements instead of its actual value.
Please have a look at the following code:
#include <stdio.h>
int main(void) {
int array[5]; //the array with x items
int target = array[1]; //target is any second value of the array
for (int y = 0; y < 5; y ){ //taking 5 user inputs
if (y == 0){
printf("Enter your %dst item: ", y 1);
scanf("%d", &array[5]);
} else if (y == 1){
printf("Enter your %dnd item: ", y 1);
scanf("%d", &array[5]);
} else if (y == 2){
printf("Enter your %drd item: ", y 1);
scanf("%d", &array[5]);
} else{
printf("Enter your %dth item: ", y 1);
scanf("%d", &array[5]);
}
}
for (int x=0 ; x < 5; x ){ //printing out array
printf("Array[%d] = %d\n", x, array[x]);
}
}
Thanks!
CodePudding user response:
You didn't assign any value to the array.
int target = array[1];
doesn't make sense, since the array is not initialized. C programs are executed from top to bottom, assignment doesn't create some magic relation between variables:target
will not get magically updated later ifarray[1]
eventually gets initialized. If you want something like that, you could use pointers instead.&array[5]
doesn't make sense, since arrays in C are 0-indexed and you access the array out of bounds by writing to the 6th item. This causes undefined behavior and one possible outcome of undefined behavior is memory corruption.printf("Array[%d] = %d\n", x, array[x]);
doesn't make sense, since again, the array has not been initialized. The values printed will have indeterminate values, it could be anything.
You could fix your program like this:
#include <stdio.h>
int main (void)
{
int array[5];
int* target = &array[1];
for (int i = 0; i < 5; i )
{
const char* nstr[] = {"st", "nd", "rd", "th"};
int index;
if(i > 3)
index = 3;
else
index = i;
printf("Enter your %d%s item: ", i 1, nstr[index]);
scanf("%d",&array[i]);
}
for (int i=0; i < 5; i )
{
printf("Array[%d] = %d\n", i, array[i]);
}
printf("Target: %d\n", *target);
}
Output & input example:
Enter your 1st item: 1
Enter your 2nd item: 2
Enter your 3rd item: 3
Enter your 4th item: 4
Enter your 5th item: 5
Array[0] = 1
Array[1] = 2
Array[2] = 3
Array[3] = 4
Array[4] = 5
Target: 2
CodePudding user response:
First of all, you haven't read this array like you. This is better :
for (int y = 0; y < 5; y ){ //taking 5 user inputs
printf("Enter your %dst item: ", y 1);
scanf("%d", &array[y]);
}
Note, that I read &array[y], but not &array[5]. You have to put to scanf as arg the adress of destination of data. It's tha same like
for (int y = 0; y < 5; y ){ //taking 5 user inputs
printf("Enter your %dst item: ", y 1);
scanf("%d", array y);
}
So, your correct prog will be like this :
int main(void) {
int array[5]; //the array with x items
//int target = array[1]; //target is any second value of the array I dont know what you want here
for (int y = 0; y < 5; y ){ //taking 5 user inputs
printf("Enter your %dst item: ", y 1);
scanf("%d", array y);
}
for (int x=0 ; x < 5; x ){ //printing out array
printf("Array[%d] = %d\n", x, array[x]);
}
}