Keep in mind that:
A pointer is a variable that contains memory address. Other words, content of a pointer is memory address. To utilize the address, you have to request the system reserve it for you via malloc, calloc...
Now let's examine your code:
- Third line: float *x; --> you declares a pointer x pointing to a memory address which will be used to store (some) float number(s). However the address is not allocated yet so it would be anywhere in your system memory and certainly is not reserved for you only.
- Fourth line: *x = *k; --> you access a memory allocation that hasn't been allocated --> a running error. If you are lucky (there is no program access this memory), you can get value of k.
What you should do here is to allocate a memory reserved for x by calling malloc, calloc, ...
If k points to only 1 int number, your code should be:
float *program(const int *k)
{
float *x;
x = (float *)malloc(sizeof(float));
*x = *k;
return x;
}
And if k points to an arry of int numbers, try to figure out by yourself :)