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.

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,123 questions
C#
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.
10,932 questions
C++
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.
3,732 questions
{count} votes

6 answers

Sort by: Most helpful
  1. Barry Schwarz 2,901 Reputation points
    2023-08-26T16:08:05.4966667+00:00

    Your sample program is the only legal way to convert values from one type to another. It must be done on the variables themselves, not on pointers.

    Since there are implicit conversions defined between most numeric types, the casts in your code are superfluous. "f = a;" and "f = (float)a;" are identical.

    A conversion between types can alter the value being converted. For example, "i = (int)b;" causes the the value of b (65.5) to be truncated (to the int value 65) before it is stored in i. This never happens with pointers.

    Any time you coerce the value of a pointer to point to an object of a different type and then dereference the new pointer, the bits in the object are unchanged and will be interpreted (usually with undesired results) according to the new type.

    So now you need to tell us what you really want to do. Don't describe C code; describe the real objective.


  2. WayneAKing 4,921 Reputation points
    2023-08-26T17:26:47.2133333+00:00

    Consider this code. Note that the casts are not needed, but without them VS will issue a warning about possible truncation.

    int a2, i2, *a2p;
    float b2, f2, *b2p;
    
    a2 = 65;
    b2 = 65.5;
    
    a2p = &a2;
    b2p = &b2;
    
    //f2 = *a2p; // same as with cast but gives compiler warning
    f2 = (float)*a2p;
    
    //i2 = *b2p; // same as with cast but gives compiler warning
    i2 = (int)*b2p;
    
    printf("The value of 'i2' is %d\n", i2);
    printf("The value of 'f2' is %.2f\n", f2);
    
    

  3. Bruce (SqlWork.com) 65,311 Reputation points
    2023-08-26T17:44:09.4066667+00:00

    casting a pointer just tells the compiler what datatype the memory location pointer is pointing to. it has no effect on the data. to casting datatypes with conversion only works with value types. so you need to cast the deference of a pointer.

    int main()
    {
        int a, i;
        float b, f;
        int* ap = &a;
        float* bp = &b;
        
        a = 65;
        b = 65.5;
    
        f = (float) *ap;
        i = (int) *bp;
    
        printf("The value of 'i' is %d\n", i);
        printf("The value of 'f' is %.2f\n", f);
    
        // double indirect
        int **app = &ap;
        float **bpp = &bp;
        
        f = (float) **bpp;
        i = (int) **app;
    
        printf("The value of 'i' is %d\n", i);
        printf("The value of 'f' is %.2f\n", f);
    
        return 0;
    }
    

  4. WayneAKing 4,921 Reputation points
    2023-08-27T17:40:08.4+00:00

    >is casting of pointers done in C?

    Yes. See the examples on this page:

    CHAPTER 10: Pointers to Functions

    https://sites.cs.ucsb.edu/~mikec/cs16/misc/ptrtut12/ch10x.htm

    Chapter is from:

    A Tutorial on Pointers and Arrays in C

    https://sites.cs.ucsb.edu/~mikec/cs16/misc/ptrtut12/pointers.htm

    • Wayne
    0 comments No comments

  5. Minxin Yu 11,751 Reputation points Microsoft Vendor
    2023-08-28T07:36:36.98+00:00

    Hi, @Walkbitterwyrm Lightwarg

    Please read the document: Conversions to and from Pointer Types

    the result may be undefined because of the alignment requirements and sizes of different types in storage.

    Pointer conversion between float and int causes undefined result.

    You need to use value conversion for float.

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

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.