-1

I am unable to assign one structure variable (R2 = R1) to another structure variable. Please help me to understand, why the following program is not getting compiled and what is the best way to assign one structure variable to other?

I'm tired with the pointers way. Still code is not getting compiled...

Code1:

#include<stdio.h>

struct Record
{
    int ID;
    char Name[];
} R1 = {1234, "King"}, R2;

R2 = R1;

int main()
{
    printf("%d   %s \n", R1.ID, R1.Name);
    printf("%d   %s \n", R2.ID, R2.Name);
}

Code2:

#include<stdio.h>

struct Record
{
    int ID;
    char Name[];
} R1 = {1234, "King"}, *R2;

R2 = &R1;

int main()
{

    printf("%d   %s \n", R1.ID, R1.Name);
    printf("%d   %s \n", R2->ID, R2->Name);
}
phuclv
  • 37,963
  • 15
  • 156
  • 475
Bahubali
  • 141
  • 1
  • 2
  • 8
  • And please, **declare** and **set** the two struct variables **in** the `main()` function. It hurts the eyes.. – YaatSuka Oct 18 '17 at 03:36
  • I tried below, #include struct Record { int ID; char Name[10]; }R1={1234,"King"},*R2; R2=&R1; int main() { printf("%d %s \n",R1.ID,R1.Name); printf("%d %s \n",R2->ID,R2->Name); } Still not working – Bahubali Oct 18 '17 at 03:37
  • See my updated question.. – Bahubali Oct 18 '17 at 03:40

3 Answers3

3

You can try this.

#include <stdio.h>
#include <string.h>
typedef struct _Record
{
    int ID;
    char Name[10];
} Record;

int main()
{
    Record R1, R2;
    R1.ID = 1234;
    strcpy(R1.Name, "king");
    R2 = R1;
    printf("%d   %s \n",R1.ID,R1.Name);
    printf("%d   %s \n",R2.ID,R2.Name);
}

>>>
1234 king
1234 king

As @piedar said, the following also works

#include <stdio.h>
#include <string.h>
struct _Record
{
    int ID;
    char *Name;
} R1={1234,"king"}, R2;

int main()
{
    R2 = R1;
    printf("%d   %s \n",R1.ID,R1.Name);
    printf("%d   %s \n",R2.ID,R2.Name);
}
Lion Lai
  • 1,862
  • 2
  • 20
  • 41
  • Hi Zian, thanks..But why my Code 1 and Code 2 are not working? Could you please explain? – Bahubali Oct 18 '17 at 03:41
  • 1
    Did you try your edited code? No, it will *not* work, you cannot assign to a `char[]`. Printing that will show you that `R2.name` is an empty string. – bnaecker Oct 18 '17 at 03:50
  • Hi @user1031438 your code isn't working because u're not declaring the variable properly and inside a scope with the variable type. You're trying to use R1 & R2 as if they were already declared somewhere in program as global variable. – Nicolas Guerin Oct 18 '17 at 03:50
  • @bnaecker I fixed it. btw, can you explain why using `char Name[]` would not work here. – Lion Lai Oct 18 '17 at 04:00
  • 1
    @ZianLai That is an array. You cannot assign to an array, as far as I'm aware. You must use `char *name`, which can be pointed anywhere, including at an existing `char[]` array. – bnaecker Oct 18 '17 at 04:03
  • It seems copying is automatic if the size is known; probably the compiler emits memcpy() or equivalent. The `char*` is just an offset into rodata and can be copied (safely?) since it has static storage. – piedar Oct 18 '17 at 04:21
  • @ZianLai, The code is running after putting R2=R1; in main(). But as per C standards, is there any rule that, we can't assign structure variables outside any function? What is the concept working here? – Bahubali Oct 18 '17 at 06:12
  • @user1031438 according to my experiences, you can't run code outside of function. You can only declare variables outside of functions. Hope it helps. – Lion Lai Oct 18 '17 at 06:22
  • @bnaecker that's not true. Arrays wrapped inside structs will be copied as normal. That's one trick when you want to pass arrays as values to functions. See [Deep copying structs with char arrays in C (How to copy the arrays?)](https://stackoverflow.com/q/29989516/995714). The problem here is just about the empty `[]` when declaring the array – phuclv Nov 11 '20 at 07:48
0

In Code1, you can't use R2=R1 outside C functions, otherwise GCC will report errors. Just move R2=R1; into the main function and then your program will work normally.

Similarly, move R2=&R1; into the main function.

haolee
  • 892
  • 9
  • 19
0

You haven't reserved any space for the string. An array with no size at the end of a struct is called flexible array member

struct Record
{
    int ID;
    char Name[];
}

You have to allocate some memory for the string like this

struct Record R1 = malloc(sizeof(struct Record) + 10 * sizeof(char));

Alternatively declare Name as a char* then allocate memory and copy the string like normal. Remember to delete the pointer when you finished using it

Or just avoid flexible array member and declare a fixed size for Name

phuclv
  • 37,963
  • 15
  • 156
  • 475