字符串使用C数组的动态分配 [英] Dynamic Allocation of an Array of Strings in C
问题描述
有关家庭作业,我需要定义分配内存以字符串数组(这是为一个结构)的功能。
For home-work, I need to define a function that allocate memory to an array of strings (which is into a struct).
每个字符串的长度被给出:MAX_WORD_LEN + 1(= 10 + 1)的
我不得不为 LEN
串号, LEN
在输入收到分配内存。
The length of each string is given: MAX_WORD_LEN+1 (=10+1)
I have to allocate memory for len
number of strings, len
is recieved in the input.
用绳子定义的数组结构体(给):
Struct with the array of strings definition (given):
struct dict{
int len;
char (*dict0)[MAX_WORD_LEN+1];
char (*dict1)[MAX_WORD_LEN+1];
};
我不明白的声明字符(* dict0)MAX_WORD_LEN + 1];
- 函数声明也给出:结果
无效createDict(结构字典* myDict);
- The function declaration is also given:
void createDict(struct dict* myDict);
这是我写的,但我不知道它的工作原理,我有困难的时候检查它的编译器。我也写了基于来自网站和其他网站后,并没有真正理解它:
This is what I wrote, but I'm not sure it works, and I have a difficult time checking it in the compiler. I also wrote it based on post from this and other websites and do not really understand it:
选项1:
void createDict(struct dict* myDict)
{
myDict->(*dict0) = malloc( (myDict->len)*sizeof(char*));
myDict->(*dict1) = (char**) malloc( (myDict->len)*sizeof(char*));
for(int i=0;i<(myDict->len);i++)
{
(myDict->(*dict0)[i]) = (char*)malloc((MAX_WORD_LEN+1)*sizeof(char));
(myDict->(*dict0)[i]) = (char*)malloc((MAX_WORD_LEN+1)*sizeof(char));
}
}
选项2:
(myDict->(*dict0)[MAX_WORD_LEN+1]) = malloc((myDict->len) * sizeof(char*));
(myDict->(*dict1)[MAX_WORD_LEN+1]) = malloc((myDict->len) * sizeof(char*));
请向我解释...
推荐答案
首先,你需要了解以下三个声明的区别。为简便起见,假设 N
是MAX_WORD_LEN + 1相匹配的大小:
First you need to understand the difference between the following three declarations. For the sake of brevity, assume N
is MAX_WORD_LEN+1 to match your sizing:
char data[N]; // array of N chars
char *data[N]; // array of N char *pointers* (i.e. char *)
char (*data)[N]; // pointer to array of N chars
记住高于一切,指针的变量的持有地址,并实现定义。就像一个 INT
变量保存一个实现整数值,指针变量保存的实现地址。
Remember above all else, pointers are variables that hold an "address" and are implementation-defined. Just like an int
variable holds the value of an implementation integer, a pointer variable holds an implementation address.
在几乎所有情况下,可以适当的malloc()
内存使用与非关联化的基本目标的sizeof()运算符指针类型。有些情况下,这是不直观或容易presentable,但下面的内容应该有所帮助:
In almost all cases, you can properly malloc()
memory for a pointer type using the sizeof() operator with the underlying target dereferenced. There are some cases where this is not intuitive or easily presentable, but the following should help:
// allocates sizeof(Type) block
Type *p = malloc(sizeof(*p));
// allocates N*sizeof(Type) contiguous blocks
// note: we'll use this style later to answer your question
Type *pa = malloc(N * sizeof(*pa));
这将工作不管是什么键入
是。这一点很重要,因为在你的情况下,你必须声明为指针:
This will work no matter what Type
is. This is important, because in your case you have a pointer declared as :
char (*dict)[N];
由于我们已经在上面讨论的,该声明类型的指针(指针对N-字符)。需要注意的是没有实际存储器已被分配。这只是一个指针;仅此而已的。因此,您可以使用上述语法安全分配一个单独的元素:
As we already discussed above, this declares a pointer of type (pointer-to-N-chars). Note that no actual memory has been allocated yet. This is just a pointer; nothing more. Therefore, you can safely allocate a single element using the above syntax as:
// allocate single block
char (*dict)[N] = malloc(sizeof(*dict));
但是,这只占一个条目。您需要 LEN
项,所以:
// allocate 'len' contiguous blocks each N chars in size
char (*dict)[N] = malloc(len * sizeof(*dict));
现在字典
被安全地寻址从0阵列。(LEN-1)。你可以在你的数据复制,如:
Now dict
is safely addressable as an array from 0..(len-1). You can copy in your data such as:
strcpy(data[0], "test");
strcpy(data[1], "another test");
只要源字符串不超过的N-字符(包括零终止),这将正常工作。
So long as the source string does not exceed N-chars (including the zero-terminator), this will work correctly.
最后,完成后不要忘记释放你分配的:
Finally, don't forget to free your allocation when finished:
free(dict);
扰流
myDict->dict0 = malloc( myDict->len * sizeof(*(myDict->dict0)));
myDict->dict1 = malloc( myDict->len * sizeof(*(myDict->dict1)));
这篇关于字符串使用C数组的动态分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!