[英]dynamically allocated 2d array in C
I need some clarity in creating a 2d array for characters. 在为字符创建2d数组时需要一些清晰度。 I know this that
我知道
char *str="hellowww";//here its a constant string
str can point to "hellowwww" string, without any memory allocation. str可以指向“ hellowwww”字符串,而不分配任何内存。 so
所以
char *str[5]
should be able to do same for each element. 应该能够对每个元素执行相同的操作。 So if that is the case,
所以如果是这样,
1) Is char *str[5] is a 2d character array? 1)char * str [5]是2D字符数组吗?
2) When do we need to use malloc and free in this case? 2)在这种情况下,什么时候需要使用malloc和free?
3) I know I can create a loop for malloc as we do in 2d integer array, but is there a point? 3)我知道我可以像在2d整数数组中那样为malloc创建一个循环,但是有一点吗?
#include <stdio.h>
#include <string.h>
int main(void)
{
char *arr[6];
int i, j;
arr[0] = "hurr1";
arr[1] = "hurr2";
arr[2] = "hurr3";
arr[3] = "hurr4";
arr[4] = "hurr5";
arr[5] = "hurr6";
for(i = 0; i < 6; i++)
{
for(j = 0; j<strlen(arr[i])+1;j++)
printf("%c", arr[i][j]);
printf("\n");
}
}
Above program just works without any malloc or free, and I can access each character. 上面的程序无需任何malloc或free就可以工作,并且我可以访问每个字符。
char *str;
allocates enough bytes for a pointer, which it expects at some later time will be filled in with the address of a character (probably the first of a sequence, and possibly by allocating memory dynamically). 为一个指针分配足够的字节,它希望在以后的某个时间用一个字符的地址填充(可能是序列的第一个,并且可能通过动态分配内存)。
char *str = "hello";
allocates the pointer, and also allocates a 6-byte buffer (possibly in read-only memory) for the string "hello" and its terminating 0 byte, and make the pointer point to it. 分配指针,还为字符串“ hello”及其终止的0字节分配6字节的缓冲区(可能在只读存储器中),并使指针指向它。
I don't know what you mean "without any memory allocation". 我不知道您的意思是“没有任何内存分配”。
char *str[5];
Allocates a block of memory for five consecutive pointers, which it expects will be filled in at some later time. 为五个连续的指针分配一个内存块,它预期将在以后的某个时间填充。 Is this a 2d array?
这是二维数组吗? No, although if you happen to assign each of the pointers the address of a sequence of characters,
str[x][y]
will access the y
th character of the x
th string, making it look like a 2d array. 否,尽管如果您恰巧为每个指针分配了一个字符序列的地址,
str[x][y]
将访问第x
个字符串的第y
个字符,使其看起来像2d数组。
char *str[5]
should be able to do same for each element
应该能够对每个元素执行相同的操作
That is absolutely correct: each of the five elements of str
can point to a string. 这是绝对正确的:
str
的五个元素中的每一个都可以指向一个字符串。
- Is
char *str[5]
is a 2d character array?char *str[5]
是2D字符数组吗?
No. It is a one-dimension array of character pointers. 否。它是一维字符指针数组。 You can treat it as a 2d array, but you have flexibility to not have some dimensions filled in if you wish.
您可以将其视为2D数组,但是可以灵活选择是否填写某些尺寸。
- When do we need to use malloc and free in this case?
在这种情况下,何时需要使用malloc和free?
It depends on what you would like to put into elements of str[5]
, and whether or not you'd like to modify these things. 这取决于您要在
str[5]
元素中添加什么,以及是否要修改这些内容。 If you put string literals in all five elements, you don't need malloc
at all, but the strings you have would not allow modifications. 如果将字符串文字放在所有五个元素中,则根本不需要
malloc
,但是拥有的字符串将不允许修改。
- I know I can create a loop for
malloc
as we do in 2d integer array, but is there a point?我知道我可以像在2d整数数组中那样为
malloc
创建一个循环,但是有一点吗?
Again, this depends on what you are trying to achieve. 同样,这取决于您要实现的目标。 You can have a loop that allocates memory dynamically, you could use string literals, use pointers to statically allocated modifiable strings, and anything else that you can do with a character pointer.
您可以使用一个循环来动态分配内存,可以使用字符串文字,使用指针来静态分配可修改的字符串,以及可以使用字符指针进行的其他操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.