Why this program shows error while initializing with float value?

Walkbitterwyrm Lightwarg 20 Reputation points
2023-09-03T22:14:36.0666667+00:00

Why the following program shows error (incompatible types when initializing type 'float *' using type 'double') when a float value is initialized with a float pointer?

#include <stdio.h>

int main()
{
    float *a = 2.5;

    printf("%f", a);

    return 0;
}

VS Code Error

Developer technologies C++
Developer technologies Visual Studio Other
Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. WayneAKing 4,931 Reputation points
    2023-09-04T01:28:17.7833333+00:00

    >when a float value is initialized with a float pointer?

    Where exactly in your posted code do you think that is happening?

    float *a = 2.5;

    This line creates a variable named "a" of type pointer to float, and attempts to initialize it with the double value 2.5 which clearly is an illegal memory address.

    • Wayne

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.