简体   繁体   English

如何打印具有多种数据类型的数组

[英]How do i print an array with multiple data types

My school assignment states the following i need to create a typedef struct with 2 data type's (Char,Int),create 4 elements for it, put these values into an array and print the array.我的学校作业指出以下我需要创建一个具有 2 个数据类型(Char、Int)的 typedef 结构,为其创建 4 个元素,将这些值放入一个数组并打印该数组。 i can manage it up to the point of creating the array.我可以管理它直到创建数组。 After that it gets a bit fussy for me,之后对我来说有点挑剔,

#include <stdio.h>

typedef struct {
    char *Series;
    char KiloWatts;
    int  Age;
} Data_T;

int main(){
     Data_T Motor[4] = {{"B", 3, 2004},{"T", 5, 2005},{"B", 3, 2004},{"A", 2, 2002}}; 
     int i;

     for (i=0; i<4;i++)
         printf("%n", Motor[i]); 
     }

     return 0;
}

I know that the printf right now is not working so is there a function to do this or do i need to write down everything manually?我知道 printf 现在无法正常工作,所以是否有 function 可以执行此操作,还是我需要手动写下所有内容?

You need to do the work yourself.你需要自己做这项工作。 One option is this:一种选择是:

 for (i=0; i<4;i++)
     printf("%s %c %d", Motor[i].Series, Motor[i].KiloWatts, Motor[i].Age); 
 }

However, this approach can be quite useful:但是,这种方法可能非常有用:

void Motor2String(char *dest, Data_T motor) {
    sprintf(dest, "%s %c %d", motor.Series, motor.KiloWatts, motor.Age);
}

And just for clarification.只是为了澄清。 Your array does not have multiple data types.您的数组没有多种数据类型。 That's not even allowed.这甚至是不允许的。 What you have is an array where every element is of type Data_T .你所拥有的是一个数组,其中每个元素的类型Data_T

Unlike other languages, such as C++ with cout and Python with print , C doesn't have a universal "print this" function which knows how to print everything (once you implement the appropriate instance method). Unlike other languages, such as C++ with cout and Python with print , C doesn't have a universal "print this" function which knows how to print everything (once you implement the appropriate instance method). So, yes, you would have to print each field manually.所以,是的,您必须手动打印每个字段。

Of course, you can easily set up a function for this.当然,您可以为此轻松设置 function。 Eg,例如,

void
print_data(const Data_t *data)
{
    printf("%s, %c, %i\n", data->Series, data->KiloWatts, data->Age);
}

There is one way that you could go through, using how it's done in the Linux kernel where there are dedicated *_ops structures to operate on the main struct .有一种方法可以让您通过 go,使用它在 Linux kernel 中的完成方式,其中有专用的*_ops结构来操作主struct These ops structures have function pointers to functions that take in a pointer to the struct.这些ops结构具有 function 指向函数的指针,这些函数接受指向该结构的指针。 An example for file operations is here .文件操作的一个例子是here This is the more object-oriented way apparently as this LWN article explains but that's for you to choose if this design makes sense.正如这篇 LWN 文章所解释的那样,这显然是更面向对象的方式,但如果这种设计有意义,则由您选择。

So for this case, something like this could work:所以对于这种情况,这样的事情可能会起作用:

#include <stdio.h>

struct Data;

typedef  struct {
    void (*print)(struct Data const* ptr);
}Data_T_ops;

typedef struct Data{
    char *Series;
    char KiloWatts;
    int  Age;
    Data_T_ops const* ops;
}Data_T;

void print_Data_T(Data_T const* ptr) {
    if(ptr) {
        printf("KW: %s\n", ptr->Series);
        printf("%d\n", ptr->Age);
    }
}

static Data_T_ops const data_t_ops = {
    .print = print_Data_T,
};

int main(){
     Data_T Motor[4] = {
         {"B", 3, 2004, &data_t_ops},
         {"T", 5, 2005, &data_t_ops},
         {"B", 3, 2004, &data_t_ops},
         {"A", 2, 2002, &data_t_ops},
    }; 
     int i;
     for (i=0; i<4;i++)
         Motor[i].ops->print(&Motor[i]);
}

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

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