简体   繁体   English

将数据存储在char *数组中

[英]Storing data in an array of char*

My task is to store data in an array of char* in main(), and each row of the data being in its own string. 我的任务是将数据存储在main()中的char *数组中,数据的每一行都位于其自己的字符串中。 I am suppose to hard-code the given data. 我想对给定的数据进行硬编码。 Is this the right way to do it. 这是正确的方法吗?

#include <stdio.h>

int main(void) {
    int i;

    char* numbers[5] {"12, 34, 56, 78",
                      "82.16, 41.296",
         "2, -3, 5, -7, 11, -13, 17, -19",
        "9.00009, 90.0009, 900.009, 9000.09, 90000.9"};
    for(i=0;i<5;i++){
    //print//
    }


}
char* numbers[5] {"12, 34, 56, 78",
                  "82.16, 41.296",
                  "2, -3, 5, -7, 11, -13, 17, -19",
                  "9.00009, 90.0009, 900.009, 9000.09, 90000.9"};

You forget the = 你忘了=

And there are only 4 strings in your array, change to 而且您的数组中只有4个字符串,请更改为

char *numbers[] = {"12, 34, 56, 78",
                   "82.16, 41.296",
                   "2, -3, 5, -7, 11, -13, 17, -19",
                   "9.00009, 90.0009, 900.009, 9000.09, 90000.9"};

And here 和这里

for(i=0;i<5;i++){

Don't use magic numbers like 5 , instead, use the sizeof operator in order to get the correct size: 不要使用像5这样的幻数,而是使用sizeof运算符来获取正确的大小:

for (i = 0; i < (sizeof numbers / sizeof *numbers); i++){

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

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