如何在C中初始化2D阵列

发布于 2025-02-10 02:15:15 字数 361 浏览 2 评论 0原文

我正在尝试找出一种初始化我的2D字符串数组的方法,并且我想知道是否有比下面编码的更好的方法。

是否有一种方法可以不提供数组的大小,因为我以后会写信给它,因此我不知道尺寸(当前设置为30号)。

稍后阶段的数组内容的示例:“足球”,“橄榄球”,“网球”

char sports_array[30][81];

int i;

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

的问题是,如果数组只有2个值,那么我将不得不循环遍历其余的(尺寸30)(尺寸30)不想要这些会是空的。我需要数组来调整,具体取决于其以后在线下的值。

任何指导都很棒。

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).

Example of array content at a later stage : "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. I need the array to adjust depending on how many values it holds later down the line.

Any guidance would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

趁年轻赶紧闹 2025-02-17 02:15:16

您不必迭代所有索引。

对于指针(char*char **),您可以在char阵列中测试条件nullchar [] )您可以测试'\ 0'

在您的情况下(char sports_array [30] [81] = {“”}),您可以执行以下操作:

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

因此丢弃所有空字符串。

如果您使用固定大小数组(char []),则使用“”{“}{' \ 0'}

如果您想与malloc和朋友一起工作(因此使用char Pointers -char*char **),请始终确保初始化用null(手动或calloc)指针。

如果您可以保证您的指针(字符串)是适当的初始化,请测试null'\ 0' can can can can and ock '\ 0'应该是任何循环的条件。

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'.

In your case (char sports_array[30][81] = {""}) you could do the following:

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'}.

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).

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.

枯叶蝶 2025-02-17 02:15:16

在阵列的声明中,您可以编写,例如

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

在这种情况下,该数组的所有字符均为零始于。

如果要重新定位它,以使数组的每个元素都包含一个空字符串,那么您可以写作,

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

例如,

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

如果您想动态分配字符串,则可以编写,然后您可以像

char * sports_array[30] = { 0 };

在这种情况下 一样声明数组阵列将是无效的指针。

如果您不知道该数组将包含多少个字符串,则可以声明一个指针

char **sports_array = NULL;

,然后在添加新字符串时使用函数realloc

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文