I am trying to learn arrays and pointers in C. I'm trying to write a program that in a function: gets 2 numbers from the user, puts them in an array, and returns them to the main function. I am getting an error and I don't understand what the problem is. Here is my code:
#include<stdio.h>
void get_nums(float *x)
{
float num1, num2;
printf("enter two numbers: ");
scanf("%f %f", &num1, &num2);
*x[0] = num1;
*x[1] = num2;
}
main(){
float x[2];
float (*p_x)[2] = &x;
get_nums(p_x[2]);
printf("Number 1: %f\nNumber 2: %f", x[0], x[1]);
return 0;
}
I am getting an error on these 2 lines
*x[0] = num1;
*x[1] = num2;
The error message is
Error: operand of '*' must be a pointer
I don't see what it is that is wrong. Does anyone see the problem?
EDIT: I changed the 2 lines to
x[0] = num1;
x[1] = num2;
and now I can run the program. However I get a new error after I enter the two numbers. The error message is:
Unhandled exception at 0x40e00000 in arraysandpointers.exe: 0xC0000005: Access violation.