I’m having a bit of issue with pointers and structures in c.
struct foo{
char a[15];
}
void asn_foo(struct foo *pa){
(*pa).a = "123";
}
main()
{
struct foo foo1[2], *pf;
pf = &(foo1[0]);
asn_foo(pf);
}
I’m trying to assign a new value by:
(*pa).a = "123";
but I have the error “Incompatible types in assignment” on this line.
What am I doing wrong?
You can’t assign new values to an array using the assignment operator, you have to copy the contents of the string
"123"into your array. Usestrcpyto do so:Another trick is also wrapping your array in a
struct(as you’ve done here), and assigning one struct to another in order to assign new values to your array.You can do:
f1.awill now hold"123".Also,
mainshould return anint.