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 | C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other
A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{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' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.