Why asterisk is used with data type in type conversion wit a pointer?

Debojit Acharjee 455 Reputation points
2023-07-31T11:19:56.85+00:00

Why the asterisk is used with the "int" keyword in a type conversion with a pointer but not with a variable?

In the following program why the asterisk is used two times with the "int" keyword while type conversion with a pointer but not with a float variable.

#include <stdio.h>

int main()
{
   float a = 2.5;
   int b;

   b = (int) a; // Why asterisk is not used with "int"?

   void *ab_ptr = &b;
    
   printf("Value of 'a' is %d\n", *(int*)ab_ptr); // Why two times asterisk is used here?

   return 0;
}

Developer technologies | C++
Developer technologies | Visual Studio | Other
Developer technologies | C#
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. RLWA32 49,541 Reputation points
    2023-07-31T11:55:37.6866667+00:00

    Maybe the following example will clarify by breaking down your code into two separate statements -

    #include <stdio.h>
    
    int main()
    {
        float a = 2.5;
        int b;
    
        b = (int)a; // Why asterisk is not used with "int"?
    
        void* ab_ptr = &b;
    
        printf("Value of 'a' is %d\n", *(int*)ab_ptr); // Why two times asterisk is used here?
    
        // Use multiple statements for illustration
    
        int *iptr = (int*)ab_ptr;  // cast void pointer to pointer to an int
    
        int x = *iptr; // dereference int pointer
    
        printf("Value of 'a' is %d\n", x);
    }
    

    Perhaps using additional parenthesis would also clarify the meaning of the printf statemet -

        printf("Value of 'a' is %d\n", *((int*)ab_ptr)); // Why two times asterisk is used here?
    
       b = (int) a; // Why asterisk is not used with "int"?
    

    No asterisk is needed because casting a variable from one type to another is not a dereferencing operation. There are no pointers involved here.


  2. Barry Schwarz 3,746 Reputation points
    2023-07-31T15:20:24.78+00:00

    You need to recognize that not all asterisks are dereference operators. There a multiple cases where C uses the same character to have multiple meanings depending on context.

    A pointer has two "properties" of interest. One is the value of the pointer. This the address of the object pointed to. The other is the value of the object the pointer points to.

    You use the dereference operator when and only when you want to work with the value pointed to rather than the value of the pointer itself.

    Your first assignment statement does not use any pointers. You access the value of directly, convert it to an int value, and store the converted value directly into b. No dereference operator needed.

    The asterisk in the definition of ab_ptr is NOT a dereference operator. It used to inform the compiler that ab_ptr is a pointer rather than an object of the type pointed to.

    In your call to printf, the parenthetical expression before ab_ptr is called a cast operator. It causes the compiler to generate the code necessary to convert the value of ab_ptr (the address it contains) from one type to another. The asterisk in this expression is also NOT a dereference operator. It simply specifies that the result of the conversion should be have type pointer to int and not int. Thus the result of the conversion is a pointer. You do not care what this value actually is. You want to access the value pointed to. That is why there is a dereference operator to the left of this expression.

    The asterisks used in your code serve multiple purposes. The ones that are part of type specification simply identify pointers. The (one) dereference operator applied to a pointer value causes the compiler to generate code to access the value pointed to rather than the pointer value itself.


  3. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-07-31T16:06:29.17+00:00

    in the line:

       printf("Value of 'a' is %d\n", *(int*)ab_ptr);// cast ab_ptr as (int*) then deference
    

    could have been written clearer:

    printf("Value of 'a' is %d\n", (int)*ab_ptr); // cast the deference

    note: the cast is required, so the compiler knows how many bytes to copy to the stack, as this is a pass by value.


  4. Debojit Acharjee 455 Reputation points
    2023-08-11T12:28:30.84+00:00

    I think no one had got my question. I want to know why asterisk is used with the datatype keyword when explicit conversion of pointer. Why this expression:

     *(int*)ab_ptr); 
    

    can't be written as:

     **(int)ab_ptr); 
    

  5. Minxin Yu 13,501 Reputation points Microsoft External Staff
    2023-08-14T06:12:30.77+00:00

    Hi,

    **(int)ab_ptr);

    (int)ab_ptr

    You explicitly cast the pointer to an int. But,

    *(int)ab_ptr

    operand of '*' must be a pointer.

    It is syntactically incompatible and your code will not compile.

    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.