Why type conversion is required for memory allocation to a pointer?

Debojit Acharjee 455 Reputation points
2023-08-12T05:07:37.7733333+00:00

I want to know why type conversion is required when assigning memory to a pointer?

int *ptr_a = (int*)malloc((sizeof(int)));

In the above line of code why the (int*) is used?

Moreover, the following code means to store the value of character.

char *a = 'A';

But this can't be applicable for an integer type.

int *a = 1; // This will not store the value

So how come int *ptr_a is able to store the memory allocation value?

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,449 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,904 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Vahid Ghafarpour 22,800 Reputation points
    2023-08-12T07:19:25.46+00:00

    Assigning an integer value directly to a pointer as in int *a = 1; is not correct. This doesn't work because an integer value doesn't inherently represent a valid memory address. If you want to store an integer value using a pointer, you need to allocate memory for that integer malloc:

    int *ptr_a = (int*)malloc(sizeof(int));
    *ptr_a = 1; // Storing the integer value 1 in the memory pointed to by ptr_a
    
    
    0 comments No comments

  2. RLWA32 48,056 Reputation points
    2023-08-12T08:31:51.1+00:00

    int *ptr_a = (int*)malloc((sizeof(int))); In the above line of code why the (int*) is used?

    Actually it depends on whether the line of code is being compiled as C or as C++. The malloc function returns a void* pointer. In C it is acceptable to assign a void* pointer to a pointer variable of a different type without a cast. However, in C++ such an assignment is an error and so a cast is required.

    char *a = 'A';

    The Visual Studio C compiler issues a warning for the above statement and the C++ compiler emits an error for it.

    When compiled as C this statement does not store the 'A' character to memory. It actually places the value of 'A' (0x41) into the pointer variable. Of course, this creates a pointer that will cause errors and probably termination due to an access violation if it is used. Same problems for an int.

    badptr

    badptr2


  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  4. Bruce (SqlWork.com) 74,531 Reputation points
    2023-08-16T23:02:27.32+00:00

    the line

    char *a = 'A';

    should cause a warning/error in modern compiler, because 'A' (char literal) is upcast to an int (65) and assigned to a pointer. using a cast to bypass error:

    char* a = (char*) 'A';
    printf("%p\n", a); //0x41 (65 in hex)
    printf("%s\n", a); //undefined behavior as 65 is a random memory address  
    

    while:

    char *a = "A";            // set to a to string literal address
    printf("%p: %s\n", a, a); //<some address>: A
    

    is valid, because a is being set to the address of the string literal.

    note: due to string literal pooling, the following should print the same address:

    char *a = "A";         // set to a to string literal address
    char *b = "A";         // set to b to string literal address
    printf("%p %p\n", a, b); //values should match
    printf("%d", a == b);  //1 (addresses match)
    
    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.