-2

What I currently have is a simple function that basically just reassigns pointers given the parameter pointer but I'm getting an error saying that I'm using one of the variables uninitialized. Here's what I have and the error is being thrown on the line *x = *k;

float * program(const int *k)
{
    float *x;
    *x = *k;
    return x;
}

This has got to be a really simple fix, but I feel like I'm just missing it.

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
user1837411
  • 53
  • 1
  • 2
  • 6
  • 5
    What is `x` pointing at? – Oliver Charlesworth Jan 29 '14 at 00:12
  • 5
    If you don't mind asking, is [user3246779](http://stackoverflow.com/questions/21419439/typecasting-pointers-in-c) your classmate? – this Jan 29 '14 at 00:12
  • 6
    The compiler is right: `x` is unitialized. Without knowing where x is supposed to point, its impossible to tell what you're trying to do. – ApproachingDarknessFish Jan 29 '14 at 00:14
  • x=k; would mean that the values of the pointers are assigned... that is probably "closer" to what you meant – Grady Player Jan 29 '14 at 00:20
  • Your code is certainly invalid, but I can't tell exactly what you're trying to accomplish. Do you want to assign an integer value to a floating-point object, or do you want to do a pointer assignment that would, in effect, treat an `int` object *as if* it were a floating-point object? (The latter is rarely a good idea.) – Keith Thompson Jan 29 '14 at 00:24
  • 2
    "the line `*p = *x`" -- There's no such line. There is `*x = *k*` which obviously uses an uninitialized `x`. *This has got to be a really simple fix* -- No, actually, you have deep, fundamental understandings. – Jim Balter Jan 29 '14 at 00:27
  • Well, there is a really simple fix - just change `float *x;` to `float * x = malloc(sizeof(*x));`, and everything works perfectly, provided that the first of Keith's two options is the desired one, and he wants to assign an `int` value to a `float` (which is how it looks to me, but who knows). Obviously he'd have to `free()` the pointer at some stage. – Crowman Jan 29 '14 at 00:41

3 Answers3

5

Here's what your code is doing:

float * program(const int *k)
{
    // declare a pointer to a float, but don't initialize it,
    // so it is not actually pointing anywhere specific yet.
    float *x; 
    // Put the value pointed at by k into the location pointed at by x
    // But wait - we don't know where x is pointing, so this is BAD!
    *x = *k;
    return x;
}

And that is why your compiler is complaining.

Ergwun
  • 12,579
  • 7
  • 56
  • 83
0

It looks as if you're making a cast into a function, or achieving a cast via a function.

The compiler is correct that x is not initialized, so assigning to *x leads to undefined behaviour (and the compiler warning you are getting). This code might be what you want:

const float *program(const int *k)
{
    const float *x = (float *)k;
    return x;
}

You need the cast — unless you are compiling with way, way, way too many warnings disabled. Note that this is still giving you undefined behaviour, but it is a somewhat different form of undefined behaviour — with most systems, this code will reinterpret the int pointer as a float pointer and you'll get more or less sane results. With the uninitialized variable, you simply cannot tell what will happen; it is much worse to dereference an uninitialized pointer.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • This may or may not be UB ... it depends on where `k` came from. e.g., `const float* fp = program(malloc(sizeof *fp))` is ok, I think. – Jim Balter Jan 29 '14 at 00:31
0

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 :)

vad
  • 344
  • 2
  • 12