what is the difference while typecasting a pointer as &(int*) and (int*)& while assigning it to some pointer variable?

how to cast a pointer to an integer in c
type casting pointers in c++
pointer casting in c tutorial
type casting in c
cast void pointer to int
cast from pointer to integer of different size
cast pointer to char c
type casting void pointer in c

If we have a void pointer and we try to assign it the address of some pointer variable (for instance, a pointer to int ), then what is the difference between the statements 1 and 2.

For statement 2 , I am getting lvalue error, but I am not getting the reason behind this - ptr is lvalue only so why does it show an error?

int a;
int *ptr =&a ;
1. void *p =(int*)&ptr;
2. void *p=&(int*)ptr;

According to section 6.5.4 of the C99 specification, line 4, footnote 89, available online here:

A cast does not yield an lvalue

When we look at your code:

int a;
int *ptr =&a ;
1. void *p =(int*)&ptr;
2. void *p=&(int*)ptr;

In case of 1: A void pointer is assigned what has been typecast to int*. This is valid. In fact the following statements also are valid:

3. void *p = (int*)123;
4. void *p = (int*)a;
5. void *p = (int*)ptr;

But in case of 2: (int*)ptr has become an rvalue and then you are using & on that rvalue. It fails stating: lvalue required as unary '&' operand

For 2., it is not the assignment to void* that is causing the problem but the operator & on an rvalue.

What are the rules for casting pointers in C?, to c which is of type char . A char is one byte of memory, so when d is dereferenced, you get the value in that one byte of memory. In the diagram below, each cell represents one byte. In the following code lines, A is an int type variable, D is variable of type double, and ch is a variable of type char. Pa is declared as a pointer to int variables, Pd is declared as a pointer to double type variables, and Pc is declared as pointer to character type variables. Pa is assigned the value &A.

In your example the second one void *p=&(int*)ptr; has incorrect syntax as & requires an lvalue.

int *n;
void *p = &n;
void *q = &(void *)n;  //incorrect syntax will not compile at all
void *w = &(int *)n;   //incorrect syntax will not compile at all

So the difference is: &(some_type *)n will not compile at all, and does not have any sense. Sample code.

&(int*)ptr is just not valid in C.

Why? Because p and (type *)p are completely different. p is object which reference can be obtained (type *)p is only the value without the object behind it and consequently it does not have the address.

Typecasting in C, to another. It is used in computer programming to ensure variables are correctly processed by a function. Another common typecast is converting a floating point number to an integer. It is important to note the following key differences: Typecasting refers to the conversion of one data type to another by the user, whereas type conversion refers to the automatic conversion of one type of data to another. We generally use typecasting when both the data types are incompatible with each other.

It is the same behaviour while casting with int :

int a;
int *ptr = &(int)a;

Typecasting Definition, Note the difference between the type casting of a variable and type casting of a While for type casting a double pointer Pd into pointer for int the code is as  Typecasting is converting one data type into another one. It is also called as data conversion or type conversion. It is one of the important concepts introduced in 'C' programming.

Typecasting | C Programming Questions and Answers, When a variable is typecast into a different type, the compiler is An integer pointer should only point to integer data, while a character pointer  Typecasting is making a variable of one type, such as an int, act like another type, a char, for one single operation. To typecast something, simply put the type of variable you want the actual variable to act as inside parentheses in front of the actual variable.

Type Casting Of Pointers in C, Do not convert a pointer value to a pointer type that is more strictly aligned than the cast or the pointer is converted to a void pointer ( void * ) and then to a different type, Some architectures require that pointers are correctly aligned when  Given that ph is a pointer to an int the first one does the correct thing, it prints out the value of the pointer. The second one's behavior depends on the size and representation of int and pointers on your system. The value is 'really' a pointer but you are telling the runtime its an int. ON many many systems pointers and ints are 32 bits.

EXP36-C. Do not cast pointers into more strictly , You can use it when you want to typecast a pointer of derived class to the base 262: What is the difference between the seekg and seekp member functions? What is type casting in C Language? Converting an expression of a given type into another type is known as type casting. typecasting is more use in c language programming. Here, It is best practice to convert lower data type to higher data type to avoid data loss. Data will be truncated when the higher data type is converted to lower.

  • Both are wrong - just use void *p = ptr;.
  • &(int *)ptr is equivalent to &((int *)ptr) so has the effect of computing (int *)ptr as a temporary (which is not an lvalue) first and the & then attempts to obtain the address of that temporary. (int *)&ptr computes the address of ptr (i.e. &ptr) first - which is valid since ptr is an lvalue - and then converts the value to be of type int *.
  • Why the downvotes? This is a good question. And @Peter please use the answers section for answers.
  • @Peter no - it is an equivalent to error: lvalue required as unary ‘&’ operand consequently does not have any sense
  • ptr is an int *, not a void *, therefore &ptr has type int **. You can't implicitly convert to void ** as it puts the conversion outside the scope of the representation.
  • @Leushenko it does matter in this question as only one has the valid systax.
  • DV? Any reason ?
  • int *n; void *p = &n; this is no-go. It will compile but will crash sooner-or-later when you reuse it back.
  • @tlrz of course but he asked another question which I have answered. What is the difference? The difference is: one will compile (abstracting from the sense of it), the second will not.
  • How does this answer the question? This does not explain the difference of the 2 statements.
  • @Gerhard it will not compile at all
  • I am not all-knowing but int *ptr = &(int){a};works fine.
  • @P__J__ correct, and the OP already noticed that. That is the reason why the OP asked in the first place.
  • &(int){a} is a completely different case. It is taking the address of an anonymous object of type int that was initialized to a.