Casting one C structure into another

c cast to struct pointer
cast struct to another struct golang
c cast struct
c cast int to struct
casting pointers in c
c++ cast struct to int
type casting structure in c
type casting in c

I have two identical (but differently named) C structures:

typedef struct {
      double x;
      double y;
      double z;
} CMAcceleration;


typedef struct {
    double x;
    double y;
    double z;   
} Vector3d;

Now I want to assign a CMAcceleration variable to a Vector3d variable (copying the whole struct). How can I do this?

I tried the following but get these compiler errors:

vector = acceleration;           // "incompatible type"
vector = (Vector3d)acceleration; // "conversion to non-scalar type requested"

Of course I can resort to set all members individually:

vector.x = acceleration.x;
vector.y = acceleration.y;
vector.z = acceleration.z;

but that seems rather inconvenient.

What's the best solution?

That's your only solution (apart from wrapping it into a function):

vector.x = acceleration.x;
vector.y = acceleration.y;
vector.z = acceleration.z;

You could actually cast it, like this (using pointers)

Vector3d *vector = (Vector3d*) &acceleration;

but this is not in the specs and therefore the behaviour depends on the compiler, runtime and the big green space monster.

[PDF] Casting pointers, pointers of one type to pointers of other, arbitrary types. □ See pointers/casting/​weak_typing.c. □ The results A string in C is (literally) an array of chars terminated by a null They allow your program to impose logical structure on memory. C - Type Casting Converting one datatype into another is known as type casting or, type-conversion. For example, if you want to store a 'long' value into a simple integer then you can type cast 'long' to 'int'. You can convert the values from one type to another explicitly using the cast operator as follows −

You could use a pointer to do the typecast;

vector = *((Vector3d *) &acceleration);

casting struct to struct, You can simply cast your structure to a struct listhdr (because the first I've failed to find any info on casting structures to other struct types. Typecasting in C By Alex Allain Typecasting is a way to make a variable of one type, such as an int, act like another type, such as a char, for one single operation.

You use an utility function for that:

void AccelerationToVector( struct CMAcceleration* from, struct Vector3d* to )
{
     to->x = from->x;
     to->y = from ->y;
     to->z = from->z;
}

Typecast of two structs, I'm struggeling about with the typecast of one struct into another. I want to do (​myFirstStruct*) is the C way to cast. In C++ is the worst way to  If the structures are of compatible types, yes, you can, with something like: memcpy (dest_struct, source_struct, sizeof (*dest_struct)); The only thing you need to be aware of is that this is a shallow copy. In other words, if you have a char * pointing to a specific string, both structures will point to the same string.

Why dont you use.

typedef CMAcceleration Vector3d;

(instead of creating a whole new structure)

in that case vector = acceleration; compiles just fine.

[PDF] Coping with Type Casts in C, As a result, it may not be safe for a programmer to add new elds to a struct S, because the code may rely on the memory layout of other structs that share a. Through class conversion, one can assign data that belongs to a particular class type to an object that belongs to another class type. Let there be two classes ‘A’ and ‘B’. If we want to allocate the details that belong to class ‘A’ to an object of class ‘B’ then this can be achieved by – where ‘=’ has been overloaded for

memcpy(&vector, &acceleration, sizeof(Vector3d));

Please note that this works only, if the physical layout of the structs in memory are identical. However, as @Oli pointed out, the compiler is not obliged to ensure this!

void pointer to struct pointer cast - Keil forum - Software Tools, It seems, a void pointer cannot be casted to any other pointer type. I just need ONE pointer, which can point to different structures, that means, I need to cast a void This term has a specific meaning in the context of the 'C' programming  Type Casting in C Language Type casting is a way to convert a variable from one data type to another data type. For example, if you want to store a long value into a simple integer then you can typecast long to int. You can convert values from one type to another explicitly using the cast operator.

cast to structure not working - Keil forum - Software Tools, But it's a different C standard than most of us are used to. Contrary to the OP's belief, that's not actually a cast, either. It's a compound literal, one of the things that  Type Casting Converting an expression of a given type into another type is known as type-casting. We have already seen some ways to type cast: Implicit conversion Implicit conversions do not require any operator. They are automatically performed when a value is copied to a compatible type. For example:

5.3.2 Struct Pointer Cast of Void Pointer (Sun Studio 12: C User's , In the following example, the void pointer vp, is cast as a struct pointer. With lint -​Xalias_level=weak (or higher), this generates a warning. struct foo { int a; int b  For simple structures you can either use memcpy like you do, or just assign from one to the other: RTCclk = RTCclkBuffert; The compiler will create code to copy the structure for you.

C - Type Casting, C - Type Casting - Converting one datatype into another is known as type casting or, type-conversion. For example, if you want to store a 'long' value into a  Part 2: "You can use this implementation to create linked list of arbitrary structures (ie. struct mydata), without needing to modify the code from part one.This is possible by adding a field of type struct listhdr in the beginning of your own datastructure (see example).

Comments
  • Can't you just typedef (say typedef struct CMAcceleration Vector3d) ? Ooops, someone had already pointed out...
  • +1: Good answer. Describes both the only method that's guaranteed to work, and the method that will usually work in practice, and the reason why this method is technically undefined.
  • +1 I would only add that the casting technique is pretty common - it is not like its truly evil.
  • +1 for wrapping it in a function. Even something as trivial as this is well worth making a subroutine for.
  • What happens if we declare CMAcceleration as struct { Vector3d vec; };? Then CMAcceleration instances will have Vector3d in first sizeof(Vector3d) bytes. Would that eliminate strict aliasing when performing pointer casting?
  • Then we wouldn't need to cast pointers anymore. We could just straightfowardly assign vector = acc.vec;.
  • It should be pointed out that the compiler is not obliged to ensure that both structs are packed and aligned in the same way.
  • This is undefined behaviour because of strict aliasing. cellperformance.beyond3d.com/articles/2006/06/…
  • @Secure That's a shame because I would like to use this technique to not copy but actually alias (change the type of) a struct.
  • @Michael: Specify that when using gcc or clang, the code must be compiled with the -fno-strict-aliasing flag (other compilers may use a flag with the same name, or may be less aggressive in their aliasing optimizations than gcc or clang).
  • I get a warning: 'typedef struct Vector3d Vector3d' does not refer to the unqualified type, so it is not used for linkage. Also in this case, CMAcceleration is in a weakly linked framework, so I refrain from using it in my .h file.
  • If the CMAcceleration struct is coming from a separate framework, you would be best advised to do the field-by-field copy, instead of the memcpy or type-punning tricks, to make your code robust in the event of any future changes in the other framework. (Even if you know the struct layouts are identical today, maybe they won't remain that way in subsequent releases.)