0
#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?

4
Contributors
3
Replies
4
Views
11 Years
Discussion Span
Last Post by himanjim
0
#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.

1

> 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.

Votes + Comments
--Dave
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.