简体   繁体   English

将结构的第一个变量分配给另一个

[英]assigning first variable of struct to another

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 ♀. Output 是:3 5 ♀。 How do I assign string of A to B?如何将 A 的字符串分配给 B?

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'} ).至于在 C 中声明一个字符串,您必须考虑最后一个元素始终是\0 (它是NULL ascii 字符)以便包含“平面”,您的数组应该是“平面”+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.只需将B声明为结构点指针 (*B),然后将其指向A即可打印 A 的值,您只需将B指向它们即可。

#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 = {Aa, Ab, A.name};您只需替换struct point B = {Aa, Ab, A.name}; with B = A; 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: 警告:初始化使 integer 来自没有强制转换的指针 [-Wint-conversion]
main.c:12:34: note: (near initialization for 'B.name[0]') main.c:12:34: 注意:(接近“B.name[0]”的初始化)

you cannot assign address to an array, declare name as a pointer or use strcpy as shown below.您不能将地址分配给数组,将name声明为指针或使用strcpy ,如下所示。

Also your array name should have a space for \0 , so declare it as char name[6];此外,您的数组name应该有\0的空间,因此将其声明为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" .问题是 5 个字符的数组是一个到更少的字符来保存字符串"plane" You need an array with 6 chars (one for null terminator).您需要一个包含 6 个字符的数组(一个用于 null 终止符)。 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 .如果没有 null 终止符,当变量作为参数提供给%s转换说明符时,将发生越界访问(在搜索 null 终止符时),这将导致未定义的行为

Change改变

char name[5];

to

char name[6];

if you intend to use name as a string .如果您打算将name用作字符串

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.最后一个初始化器A.name的指针类型为char * ,用于初始化字符数组。 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.字符数组A.name不包含字符串,因为数组中没有空间用于字符串文字的终止零字符'\0'

As a result you may not use the format string %s in this call of printf因此,您不能在printf的调用中使用格式字符串%s

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如果您希望字符数组name包含一个字符串,那么您需要至少将其大小再扩大一个字符,例如

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM