简体   繁体   English

如何在 C 中初始化二维数组

[英]How to initialize a 2d array in C

I am trying to figure out a way to initialize my 2d string array and Iwanted to know if there is a better way of doing it than what I have coded below.我试图找出一种方法来初始化我的二维字符串数组,并且我想知道是否有比我在下面编码的更好的方法来做到这一点。

Is there also a way to not provide the size of the array as I will be writing to it later on, so I do not know the size (currently set to size 30).还有一种方法可以不提供数组的大小,因为我稍后会写给它,所以我不知道大小(当前设置为大小 30)。

Example of array content at a later stage : "Football","Rugby","Tennis"后期数组内容示例:"Football","Rugby","Tennis"

char sports_array[30][81];

int i;

for (i=0; i<30; i++){
  strcpy(sports_array[i],"");
}

The problem with this is that if the array only holds 2 values, then I will have to still loop through the rest (size 30) which I don't want as these will be empty.这样做的问题是,如果数组只包含 2 个值,那么我仍将不得不遍历我不想要的其余部分(大小 30),因为这些值将是空的。 I need the array to adjust depending on how many values it holds later down the line.我需要根据它稍后保存的值来调整数组。

Any guidance would be great.任何指导都会很棒。

You don't have to iterate over all indices.您不必遍历所有索引。

In case of pointer ( char* or char** ) you can test for the condition NULL , in case of an char array ( char[] ) you can test for '\0' .如果是指针( char*char** ),您可以测试条件NULL ,如果是 char 数组( char[] ),您可以测试'\0'

In your case ( char sports_array[30][81] = {""} ) you could do the following:在您的情况下( char sports_array[30][81] = {""} ),您可以执行以下操作:

for (int i=0; sports_array[i][0] != '\0'; ++i) { ... }

Hence discarding all empty strings.因此丢弃所有空字符串。

If you work with fixed size arrays ( char[] ), allways initialize with "" , {""} or {'\0'} .如果您使用固定大小的数组 ( char[] ),请始终使用""{""}{'\0'}进行初始化。

If you want to work with malloc and friends (therefore using char pointers - char* or char** ), always make sure to initialize the pointers with NULL (either manually or calloc ).如果您想与malloc和朋友一起工作(因此使用 char 指针 - char*char** ),请始终确保使用NULL初始化指针(手动或calloc )。

If you can guarantee that your pointers (strings) are proper initialized, testing for either NULL or '\0' can (and mostly) should be the condition for any loop.如果您可以保证您的指针(字符串)已正确初始化,那么测试NULL'\0'可以(并且大多数情况下)应该是任何循环的条件。

Since you commented that the strings will not be modified, an array of pointers could be used.由于您评论说不会修改字符串,因此可以使用指针数组。

#include <stdio.h>
#include <stdlib.h>

int main ( void) {
    char *sports_array[30] = { NULL}; // initialize 30 pointers to NULL
    int array_count = 0; // count of used pointers.

    sports_array[0] = "Football"; // assign a string literal to a pointer
    printf ( "%s\n", sports_array[0]);
    ++array_count;

    sports_array[1] = "Rugby";
    printf ( "%s\n", sports_array[1]);
    ++array_count;

    for ( int each = 0; each < array_count; ++each) {
        printf ( "%s\n", sports_array[each]);
    }

    return 0;
}

In the declaration of the array you could write for example例如,在数组的声明中,您可以编写

char sports_array[30][81] = { "" };

in this case all characters of the array are zero-initialized.在这种情况下,数组的所有字符都初始化为零。

If you want to reinitialize it such a way that each element of the array would contain an empty string then you can write如果您想重新初始化它,使数组的每个元素都包含一个空字符串,那么您可以编写

for ( size_t i = 0; i < 30; i++ )
{
    sports_array[i][0] = '\0';
}

Then you can write for example然后你可以写例如

strcpy( sports_array[0], "Football" );
strcpy( sports_array[1], "Rugby" );
strcpy( sports_array[2], "Tennis" );

If you want to allocate strings dynamically then you can declare the array like如果要动态分配字符串,则可以像这样声明数组

char * sports_array[30] = { 0 };

In this case all elements of the array will be null pointers.在这种情况下,数组的所有元素都是空指针。

If you do not know how many strings the array will contain then you can declare a pointer like如果您不知道数组将包含多少个字符串,那么您可以声明一个指针,如

char **sports_array = NULL;

and then use the function realloc when a new string is added to the array.然后在将新字符串添加到数组时使用函数realloc

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

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