Why in this program the value stored in one pointer gets corrupted after casting?

Walkbitterwyrm Lightwarg 20 Reputation points
2023-08-20T11:44:08.03+00:00

Why in this C program the type conversion of the character pointer "ptr_c" to a integer pointer "ptr_i" changed the data but when the integer pointer "ptr_i" is converted to character pointer "ptr
_c" there was no data loss.

#include <stdio.h>

int main()
{
    int a, *ptr_i;
    char c, *ptr_c;

    a = 65;
    c = 'A';

    ptr_i = &a;
    ptr_c = &c;

    ptr_i = (int*) ptr_c;
    ptr_c = (char*) ptr_i;
        
    printf("The value of \"ptr_i\" is %d\n", *ptr_i);
    printf("The value of \"ptr_c\" is %c\n", *ptr_c);

    return 0;
}
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

3 answers

Sort by: Most helpful
  1. Rer 80 Reputation points
    2023-08-20T13:39:05.92+00:00

    My answer may be not professional enough. FYI :-) (poor english)

    when you dereference a pointer, the type of it is important.

    We know that int takes up 4 bytes of memory while char does only 1.

    One example are as followed:

    (%x: print in hex)

    #include <cstdio>
    #include <cstdlib>
    
    

    The output on my pc is:

    a: -1412567278

    in hex:

    a: abcdef12

    ptc_a: 61fdfc

    pti_a: 61fdfc

    *ptc_a: 12

    *pti_a: abcdef12

    *(ptc_a + 1): ffffffef

    c: V

    in hex:

    c: 56

    ptc_c: 61fdfb

    pti_c: 61fdfb

    *ptc_c: 56

    *pti_c: cdef1256

    *(ptc_c + 1): 12

    In this program, varibles a and c are storaged like this:

    address | values, type

    0x61fdff | ab, int High address

    0x61fdfe | cd, int

    0x61fdfd | ef, int

    0x61fdfc | 12, int <- ptc_a, pti_a

    0x61fdfb | 56, char Low address <- ptc_c, pti_c

    When you print *ptc_a, it shows 12, which is only 1 byte long in 0x61fdfc.

    Similiarly, when you print *pti_c, it shows cdef1256, which takes up 4 bytes from 0x61fdfb to 0x61fdfe. a and c are all storaged in stack from high to low address. However, data of a are in an inverse order. 0x61fdfc is the highest of a and 0x61fdff the lowest.

    So, in your example, ptr_i actually reads the data of c as well as a, because ‘A’ is only a 1-byte data. And dereference of it cause the data loss. On the contrary, ptr_c only reads the lowest byte of a(65), and a happens to only need one byte to store, so when you dereference ptr_c, you get exactly 65.

    I'm a beginner in programming so if there's a mistake please let me notice.


  2. Bruce (SqlWork.com) 82,221 Reputation points Volunteer Moderator
    2023-08-20T16:08:38.9233333+00:00

    ptr_I is the address of an int, ptr_c is the address of a char. If you assign ptr_c to ptr_i, the compiler knows this is bad code. So you cast the char* it int* to force the assignment. As both variables are address there are was no conversion. But after the assignment ptr_i is no longer points to a valid int address, it’s a bad pointer. As it has the same value as ptr_c assigning the value to ptr_c has no effect.


  3. Barry Schwarz 4,871 Reputation points
    2023-08-20T18:33:56.8233333+00:00
    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.