struct Data{
struct Data* prev;
int id;
char* name;
struct Data* next;
};
...
struct Data * _data;
struct Data * _data_b;
...
_data_b->prev = _data->prev;
_data_b->next = _data->next;
memcpy(data,_data_b, sizeof(struct Data)) //I don't know if this is correct
Just what is said in comment is my question
No need to use the type.
memcpy(data, _data_b, sizeof(Data));
Quote from: Stefan Pendl on May 12, 2012, 03:58:13 PM
sizeof(Data)
Your code would work only if you use C++ or if there is
typedef struct Data Data; somewhere.
Btw, I presume that
Flpower wants to copy data into data_b, so memcpy should be invoked like
memcpy(_data_b, _data, sizeof(struct Data)); or
memcpy(_data_b, _data, sizeof(*_data));.
If data and _data_b are pointers to the same type of structure:
*data = *_data_b;
Quote from: frankie on May 13, 2012, 01:09:03 PM
If data and _data_b are pointers to the same type of structure:
*data = *_data_b;
Hi frankie... They'd better be the same or even memcpy() would mess it up.
Of course.
But there can be a couple of cases that work with memcpy and not for the equality operator.
You can declare two structures with exaclty the same layout, but in two different declarations. The compiler doesn't check consistency between the two, but simply consider them two different variables rising an error.
The second case is for a structure fully defined, and another one which have the same contents at beginning then other variables or is an incomplete struct.
Anyway you are right, sometimes I just forget to be enaugh adherent to the question and consider some 'advanced' use (and also misuse) of 'C' programming. :-[
As you said in another thread 'C' will never be replaced easily because is enough low level for fast and efficient system programming and also enaugh high level for wathever else 8)
Even if googling you'll find some trials to write an operating system Java or C++ based base software is efficient only in 'C' that guarantee portability between different processors.
Quote from: frankie on May 13, 2012, 05:24:13 PM
Of course.
But there can be a couple of cases that work with memcpy and not for the equality operator.
You can declare two structures with exaclty the same layout, but in two different declarations. The compiler doesn't check consistency between the two, but simply consider them two different variables rising an error.
The second case is for a structure fully defined, and another one which have the same contents at beginning then other variables or is an incomplete struct.
A third case is nested structures... you can memcpy() to and from the nested struct but you can't always do it with the simple struct = struct; method.
Quote
Anyway you are right, sometimes I just forget to be enaugh adherent to the question and consider some 'advanced' use (and also misuse) of 'C' programming. :-[
:D We all do that from time to time, my friend. It's kinda hard to keep our favorite tricks at bey while helping a beginner get his/her legs.