How to write a C program to type cast two pointers of different data types?

Walkbitterwyrm Lightwarg 20 Reputation points
2023-08-26T12:03:03.1866667+00:00

I want to write a program to assign two values---one is integer and another is float to two variables and then assign these two variables to two pointers---one is integer and another float. Then exchange their values with one another using type casting and then finally print their values using printf().

The program should be similar to the following program.

#include <stdio.h>

int main()
{
    int a, i;
    float b, f;

    a = 65;
    b = 65.5;

    f = (float) a;
    i = (int) b;

    printf("The value of 'i' is %d\n", i);
    printf("The value of 'f' is %.2f\n", f);

    return 0;
}

Please show a C program to do that.

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

6 answers

Sort by: Most helpful
  1. WayneAKing 4,931 Reputation points
    2023-08-29T19:05:42.28+00:00

    >In the above program, the pointer casting during dereferencing works

    >with variables but not while casting with of different types pointers.

    >Why type casting of pointers of different types gives garbage values?

    I believe you have been told before that the memory layout of integers and floats is quite different.

    So telling the compiler to treat the value in a float as if it were an integer by type-casting a pointer to that float will yield garbage essentially. The same if you tell the compiler via a type-cast of a pointer to treat an integer as if it contains a float value.

    Type-casting pointers does NOT convert the data layout in the variable being accessed. It simply instructs the compiler to access and use the contents of that object AS IF IT WERE defined (and therefore mapped in memory) as an object of the type to which it is being cast.

    Representation of Int Variable in Memory in C++

    https://www.geeksforgeeks.org/representation-of-int-variable-in-memory-in-cpp/

    C Float and Double

    https://www.geeksforgeeks.org/c-float-and-double/

    IEEE Standard 754 Floating Point Numbers

    https://www.geeksforgeeks.org/ieee-standard-754-floating-point-numbers/

    How are floating point numbers stored in memory?

    https://stackoverflow.com/questions/7644699/how-are-floating-point-numbers-stored-in-memory

    Floating point number representation

    https://www.cprogramming.com/tutorial/floating_point/understanding_floating_point_representation.html

    • Wayne
    0 comments No comments

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.