[英]Creating 2 (or 3?) dimensional array of string in C
I want to create an array of string like: 我想创建一个字符串数组,如:
{{"zero","zero"},
{"zero","one"},
{"one","zero"},
{"one","one"}}
If I am not wrong I need 3D array. 如果我没看错,我需要3D阵列。 How can I create it with using dynamic memory allocation and how to return it from function?
如何使用动态内存分配创建它,以及如何从函数中返回它? I mean how is my function should be (char ***getString etc...) and in my main function, how can I use it?
我的意思是我的函数应该是什么(char *** getString等),在我的主要函数中,我该如何使用它? Is
char ***string = getstring();
是
char ***string = getstring();
work? 工作?
I didn't get I have to use 3d array yet, I tried it with 2 mallocs. 我没有得到必须使用3d数组的信息,我尝试了2个malloc。 I create string of array in function like
我在函数中创建数组字符串
char** editKeys = malloc(128 * sizeof(char*));
for (int i = 0; i < 128; i++ )
{
editKeys[i] = (char*) calloc(2, sizeof(char));
}
and it works in function. 它在功能上起作用。 After that I calledmy function from main like
之后,我从main调用了我的函数
char **editFile=getEditFile();
printf("%s",editFile[0][0]);
at this point I failed and now I can't be sure is it 2d or 3d and my brain is failed me too. 在这一点上我失败了,现在我不确定是2d还是3d,我的大脑也使我失败了。 Can't think how can I turned it in 3d array.
想不到如何将其转换为3D阵列。
If your compiler supports variable length arrays then you can allocate the array the following way 如果您的编译器支持可变长度数组,则可以通过以下方式分配数组
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void f( size_t n1, size_t n2, size_t n3, char s[n1][n2][n3] )
{
for ( size_t i = 0; i < n1; i++ )
{
if ( i < n1 / 2 )
{
strcpy( s[i][0], "zero" );
}
else
{
strcpy( s[i][0], "one" );
}
if ( i % 2 == 0 )
{
strcpy( s[i][1], "zero" );
}
else
{
strcpy( s[i][1], "one" );
}
}
}
int main(void)
{
enum { N1 = 4, N2 = 2, N3 = 5 };
char ( *p )[N2][N3] = malloc( sizeof( char[N1][N2][N3] ) );
f( N1, N2, N3, p );
for ( size_t i = 0; i < N1; i++ )
{
printf( "\"%s\" \"%s\"\n", p[i][0], p[i][1] );
}
free( p );
return 0;
}
The program output is 程序输出为
"zero" "zero"
"zero" "one"
"one" "zero"
"one" "one"
Or you can indeed allocate an array of arrays of arrays. 或者,您确实可以分配一个由数组组成的数组。
For example 例如
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void f( char ***s, size_t n1, size_t n2, size_t n3 )
{
for ( size_t i = 0; i < n1; i++ )
{
if ( i < n1 / 2 )
{
strcpy( s[i][0], "zero" );
}
else
{
strcpy( s[i][0], "one" );
}
if ( i % 2 == 0 )
{
strcpy( s[i][1], "zero" );
}
else
{
strcpy( s[i][1], "one" );
}
}
}
int main(void)
{
enum { N1 = 4, N2 = 2, N3 = 5 };
char ***p = malloc( N1 * sizeof( char ** ) );
for ( size_t i = 0; i < N1; i++ )
{
p[i] = malloc( N2 * sizeof( char * ) );
for ( size_t j = 0; j < N2; j++ )
{
p[i][j] = malloc( N3 );
}
}
f( p, N1, N2, N3 );
for ( size_t i = 0; i < N1; i++ )
{
printf( "\"%s\" \"%s\"\n", p[i][0], p[i][1] );
}
for ( size_t i = 0; i < N1; i++ )
{
for ( size_t j = 0; j < N2; j++ )
{
free( p[i][j] );
}
free( p[i] );
}
free( p );
return 0;
}
Atain the program output is 到达程序输出为
"zero" "zero"
"zero" "one"
"one" "zero"
"one" "one"
There is also a third approach where the number of pointers can be less than in the last case. 还有第三种方法,其中指针的数量可以小于最后一种情况。 All you need is to allocate a one dimensional array of pointers to first elements of two dimensional arrays.
您需要做的就是将一维指针数组分配给二维数组的第一个元素。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum { N1 = 4, N2 = 2, N3 = 5 };
void f( char ( **s )[N3], size_t n1 )
{
for ( size_t i = 0; i < n1; i++ )
{
if ( i < n1 / 2 )
{
strcpy( s[i][0], "zero" );
}
else
{
strcpy( s[i][0], "one" );
}
if ( i % 2 == 0 )
{
strcpy( s[i][1], "zero" );
}
else
{
strcpy( s[i][1], "one" );
}
}
}
int main(void)
{
char ( **p )[N3] = malloc( N1 * sizeof( char ( * )[N3] ) );
for ( size_t i = 0; i < N1; i++ )
{
p[i] = malloc( N2 * sizeof( char[N3] ) );
}
f( p, N1 );
for ( size_t i = 0; i < N1; i++ )
{
printf( "\"%s\" \"%s\"\n", p[i][0], p[i][1] );
}
for ( size_t i = 0; i < N1; i++ )
{
free( p[i] );
}
free( p );
return 0;
}
And at last (I hope) there is a forth approach to declare at first an array of pointers to arrays. 最后(我希望)是有一种第四种方法,首先声明一个指向数组的指针数组。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum { N1 = 4, N2 = 2, N3 = 5 };
void f( char * ( *s )[N2], size_t n1, size_t n3 )
{
for ( size_t i = 0; i < n1; i++ )
{
if ( i < n1 / 2 )
{
strcpy( s[i][0], "zero" );
}
else
{
strcpy( s[i][0], "one" );
}
if ( i % 2 == 0 )
{
strcpy( s[i][1], "zero" );
}
else
{
strcpy( s[i][1], "one" );
}
}
}
int main(void)
{
char * ( *p )[N2] = malloc( N1 * sizeof( char * [N2] ) );
for ( size_t i = 0; i < N1; i++ )
{
for ( size_t j = 0; j < N2; j++ )
{
p[i][j] = malloc( N3 * sizeof( char ) );
}
}
f( p, N1, N3 );
for ( size_t i = 0; i < N1; i++ )
{
printf( "\"%s\" \"%s\"\n", p[i][0], p[i][1] );
}
for ( size_t i = 0; i < N1; i++ )
{
for ( size_t j = 0; j < N2; j++ ) free( p[i][j] );
}
free( p );
return 0;
}
If the compiler indeed supports variable length array then the first approach is the best. 如果编译器确实支持可变长度数组,则第一种方法是最好的。
Note: In the approaches sometimes some parameter as for example n2
is not used because I knwo that it is equal to 2
. 注意:在这些方法中,有时不使用某些参数,例如
n2
因为我知道它等于2
。 But in general it should be specified. 但通常应指定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.