Making an Array to Hold Arrays of Character Arrays in C

后端 未结 5 1526
暖寄归人
暖寄归人 2021-01-17 10:04

My C is a little more than rusty at the moment, so I\'m failing to create something I think should be pretty basic.

Allow me to refer to character arrays as strings

5条回答
  •  佛祖请我去吃肉
    2021-01-17 10:49

    if storing text in a .c or .h file having more than one line of text, equivalent to the idea of an array of char arrays. can do this:

    char * text[] = {
      "message1",
      "message2",
      "message3"
    };
    

    can also use char *text[], char near *text[], char far *text[], char huge *text[]. has to have an asterisk or star character for a pointer.

    a for loop can be used to display text:

    char i; // int type can also be used
    for (i = 0, i < 3; i++)
      printf("%s\n", text[i]);
    

    other loops:

    char i = 0;  // may not be zero when declared as "char i;" only
    while (i < 3) {
      printf("%s\n", text[i]);
      i++;
     }
    

    or

    char i = 0;  // may not be zero when declared as "char i;" only
    do {
     printf("%s\n", text[i]);
     i++;
    } while (i < 3);
    

提交回复
热议问题