#include <stdio.h>
struct FUN {
char x;
char *y;
int z[20];
};
int main(void) {
struct FUN fn1;
struct FUN fn2;
struct FUN fn3[10];
struct FUN fn4[50];
return 0;
}
is an assignment like fn4[23] = fn3[5] invalid?
#include <stdio.h>
struct FUN {
char x;
char *y;
int z[20];
};
int main(void) {
struct FUN fn1;
struct FUN fn2;
struct FUN fn3[10];
struct FUN fn4[50];
return 0;
}
is an assignment like fn4[23] = fn3[5] invalid?
#include <stdio.h> struct FUN { char x; char *y; int z[20]; }; int main(void) { struct FUN fn1; struct FUN fn2; struct FUN fn3[10]; struct FUN fn4[50]; return 0; }
is an assignment like fn4[23] = fn3[5] invalid?
What happened when you tried it? ;)
Set up a function to move the parts of FUN from one value to another.
> is an assignment like fn4[23] = fn3[5] invalid?
Well it's fine as far as the syntax is concerned.
However, the pointer inside the structure presents big problems.
fn1.y = malloc( 10 * sizeof *fn1.y );
fn2 = fn1;
free( fn1.y );
// fn2.y is now a dangling pointer.
Structure assignments in C know nothing about the internals of the struct, it's just a handy wrapper around memmove( &fn2, &fn1, sizeof fn2 );
In C++, we would use a proper copy constructor to replicate what the pointer pointed to rather than just making a copy of the pointer.
structures can be copied directly into each other but copying array as such leads to error. In such cases copy the structures using pointers to the strutures.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.