assigning first variable of struct to another

Viewed 73

I have struct point which has two variables and I don't know how to assign string from one to another. I'm not yet familiar with pointers, so I don't know if they should be used.

struct point
{
    int a, b;
    char name[5];
} A = {3, 5, "plane"}, B;

int main(){
    struct point B = {A.a, A.b, A.name};
    printf("%d %d %s", B.a, B.b, B.name);
}

Output is: 3 5 ♀. How do I assign string of A to B?

4 Answers

as to declare a string in C you must consider that the last element is always \0 (it is the NULL ascii char) so as to contain "plane" your array shoud be large lengh of "plane"+1 (6, {'p','l','a','n','e','\0'}).


If you want to use pointers:

Just declare B as a struct point pointer (*B) and then point it to A so as to print the values of A you just need to point B to them.

#include <stdio.h>

struct point
{
    int a, b;
    char name[6];
} A = {3, 5, "plane"}, *B;

int main(){
    //struct point B = {A.a, A.b, A.name};
    B = &A;
    printf("%d %d %s", B->a, B->b, B->name);
}

Without pointers:

You have only to replace struct point B = {A.a, A.b, A.name}; with B = A;.

#include <stdio.h>

struct point
{
    int a, b;
    char name[6];
} A = {3, 5, "plane"}, B;

int main(){
    //struct point B = {A.a, A.b, A.name};
    B = A;
    printf("%d %d %s", B.a, B.b, B.name);
}

I got this warning when I ran this program

main.c:12:34: warning: initialization makes integer from pointer without a cast [-Wint-conversion]
main.c:12:34: note: (near initialization for ‘B.name[0]’)

you cannot assign address to an array, declare name as a pointer or use strcpy as shown below.

Also your array name should have a space for \0, so declare it as char name[6];

int main(){
    struct point B =  {A.a, A.b};//, A.name};
    
    strcpy(B.name,A.name);
    
    printf("%d %d %s", B.a, B.b, B.name);
}

The problem is an array of 5 characters is one to less to hold a string "plane". You need an array with 6 chars (one for null terminator). Without that null terminator in place, when the variable is supplied as argument to %s conversion specifier, out of bound access will happen (in search of the null terminator), which will cause undefined behaviour.

Change

char name[5];

to

char name[6];

if you intend to use name as a string.

That said, to copy one structure variable values to another variable of the same type, you just need to use assignment operator =, like

 struct point B = A;

The problem with your code is that in this declaration

struct point B = {A.a, A.b, A.name};

the last initializer A.name has the pointer type char * that is used to initialize a character array. You may not initialize an array with a pointer.

You could just write

struct point B = A;

Take into account that due to this initialization

struct point
{
    int a, b;
    char name[5];
} A = {3, 5, "plane"}, B;

the character array A.name is not contain a string because there is no space in the array for the terminating zero character '\0' of the string literal.

As a result you may not use the format string %s in this call of printf

printf("%d %d %s", B.a, B.b, B.name);

Instead you can write

printf("%d %d %.*s", B.a, B.b, ( int )sizeof( B.name ), B.name);

That is your program can look like

#include <stdio.h>

struct point
{
    int a, b;
    char name[5];
} A = {3, 5, "plane"}, B;

int main( void ){
    struct point B = A;
    printf("%d %d %.*s", B.a, B.b, ( int )sizeof( B.name ),B.name);
}

If you want that the character array name would contain a string then you need to enlarge its size at least by one more character like

struct point
{
    int a, b;
    char name[6];
} A = {3, 5, "plane"}, B;

In this case you may use the following call of printf

printf("%d %d %s", B.a, B.b, B.name);
Related