打印我们的char结构和长度

时间:2022-03-25 22:54:25

My structure has a nested structure but it only consists of chars. Ex:

我的结构有一个嵌套结构,但它只包含字符。例如:

typedef struct {
     otherstruc struct2;
     char b[100];
}mainstruct;

typedef struct {
    char a[10];
}otherstruc;

Sometimes I am able to print the structure as a string but other times the program crashes. Ex:

有时我能够将结构打印为字符串,但有时程序会崩溃。例如:

printf("Main struct = %s\n", mainstruct);

What would be the correct way to print the contents of the structure?

打印结构内容的正确方法是什么?

2 个解决方案

#1


3  

printf("Main struct = %s\n", mainstruct);

is the wrong way to print. You cannot print a structure all at a time. You need to print element by element of a variable of that structure type. You need to use something like

是错误的打印方式。您无法一次打印所有结构。您需要逐个元素地打印该结构类型的变量。你需要使用类似的东西

mainstruct struct1;

and

printf("struct1.struct2.a %s\n", struct1.struct2.a);
printf("struct1.b value %s\n", struct1.b);

Note:

  1. to use a char array as string, it needs to be null-terminated.
  2. 要使用char数组作为字符串,它需要以null结尾。

  3. mainstruct is having a variable of type otherstruc. So, the definition of otherstruc has to come before mainstruct.
  4. mainstruct有一个其他类型的变量。因此,othertruc的定义必须在mainstruct之前。

#2


2  

First of all your code will not compile because at otherstruc struct2; line in struct mainstruct compiler has no idea what is it. So, declare otherstruct first:

首先,您的代码将无法编译,因为在otherstruc struct2; struct mainstruct编译器中的行不知道它是什么。所以,首先声明其他构造:

typedef struct {
    char a[10];
}otherstruc;

typedef struct {
     otherstruc struct2;
     char b[100];
}mainstruct;

Now you should note that you can't print a structure but its instance. First instantiate it

现在您应该注意,您不能打印结构,而是它的实例。首先实例化它

mainstruct instance;  

To print the contents you need to access it's fields like this

要打印内容,您需要访问它的字段,如下所示

printf("mainstruct.b         = %s\n", instance.b)
printf("mainstruct.struct2.a = %s\n", instance.struct2.a);

#1


3  

printf("Main struct = %s\n", mainstruct);

is the wrong way to print. You cannot print a structure all at a time. You need to print element by element of a variable of that structure type. You need to use something like

是错误的打印方式。您无法一次打印所有结构。您需要逐个元素地打印该结构类型的变量。你需要使用类似的东西

mainstruct struct1;

and

printf("struct1.struct2.a %s\n", struct1.struct2.a);
printf("struct1.b value %s\n", struct1.b);

Note:

  1. to use a char array as string, it needs to be null-terminated.
  2. 要使用char数组作为字符串,它需要以null结尾。

  3. mainstruct is having a variable of type otherstruc. So, the definition of otherstruc has to come before mainstruct.
  4. mainstruct有一个其他类型的变量。因此,othertruc的定义必须在mainstruct之前。

#2


2  

First of all your code will not compile because at otherstruc struct2; line in struct mainstruct compiler has no idea what is it. So, declare otherstruct first:

首先,您的代码将无法编译,因为在otherstruc struct2; struct mainstruct编译器中的行不知道它是什么。所以,首先声明其他构造:

typedef struct {
    char a[10];
}otherstruc;

typedef struct {
     otherstruc struct2;
     char b[100];
}mainstruct;

Now you should note that you can't print a structure but its instance. First instantiate it

现在您应该注意,您不能打印结构,而是它的实例。首先实例化它

mainstruct instance;  

To print the contents you need to access it's fields like this

要打印内容,您需要访问它的字段,如下所示

printf("mainstruct.b         = %s\n", instance.b)
printf("mainstruct.struct2.a = %s\n", instance.struct2.a);