[英]How do I use char** ? (pointer to an array of chars)
So I'm trying to make a char**
, I fully understand how it works in the background and all that stuff but I don't seem to understand how to write the code for it.所以我正在尝试制作一个
char**
,我完全理解它在后台是如何工作的以及所有这些东西,但我似乎不明白如何为它编写代码。 I want to make a pointer to an array of chars which has a name in it.我想创建一个指向其中有名称的字符数组的指针。 I need help with storing a string in it (using strcpy() ) and print it after that.
我需要帮助在其中存储字符串(使用 strcpy() )并在此之后打印它。
char** name = (char**)malloc((strlen("MyName") + 1) * sizeof(char*));
strcpy(name, "MyName"); // I get an error right here
If you really want a pointer to an char array, you could do the following:如果你真的想要一个指向 char 数组的指针,你可以执行以下操作:
char** name = (char**)malloc(sizeof(char*)); //initialize the pointer
*name = (char*)malloc((strlen("MyName") + 1) * sizeof(char)); //initialize the array
strcpy(*name, "MyName");
First thing you should understand is that declaring a variable as a single pointer or a double pointer (or any other n pointer) doesn't actually tell whether the underlying variable holds a single value or an array of values.您应该理解的第一件事是,将变量声明为单指针或双指针(或任何其他 n 指针)实际上并不能说明底层变量是保存单个值还是值数组。
Single pointer points to a memory address on which actual value is stored.单个指针指向存储实际值的内存地址。 Double pointer points to a memory address on which single pointer is stored, and so on.
双指针指向存储单指针的内存地址,依此类推。
Now, to make a pointer to an array of char pointers you can use a single char pointer ( char*
) but I recommend to use double char pointer ( char**
) for maintainability purposes.现在,要创建指向 char 指针数组的指针,您可以使用单个 char 指针 (
char*
),但出于可维护性目的,我建议使用双 char 指针 ( char**
)。
Consider the following code:考虑以下代码:
char** names = (char**)malloc(100 * sizeof(char*));
It will allocate memory space for 100 char pointers ( char*
) on heap and return you a double pointer ( char**
) to the first single pointer ( char*
) in that memory space.它将为堆上的 100 个字符指针 (
char*
) 分配内存空间,并返回一个双指针 ( char**
) 到该内存空间中的第一个单指针 ( char*
)。 This means you will be able to save 100 char pointers (or 100 names in your case) inside that memory space.这意味着您将能够在该内存空间中保存 100 个字符指针(或在您的情况下为 100 个名称)。 Then you can use them like this:
然后你可以像这样使用它们:
char* name0 = "First Name"; // Saved on stack
char* name1 = malloc((strlen("Second Name") + 1) * sizeof(char)); // Saved on heap
strcpy(name1, "Second Name");
names[0] = name0;
names[1] = name1;
Also, please note that when saving a string on heap you need to add one more place for null character (manually).另外,请注意,在堆上保存字符串时,您需要为空字符再添加一个位置(手动)。
So I'm trying to make a
char**
, I fully understand how it works in the background and all that stuff but I don't seem to understand how to write the code for it.所以我正在尝试制作一个
char**
,我完全理解它在后台是如何工作的以及所有这些东西,但我似乎不明白如何为它编写代码。
Umm... No, not quite.嗯……不,不完全是。
To declare a pointer-to-char , you simply decalre:要声明一个指向 char的指针,您只需 decalre:
char *name = malloc (strlen("MyName") + 1);
Why?为什么? When you make your call to
malloc
, malloc
allocates a block of memory providing strlen("MyName") + 1
bytes and returns the starting address to that block of memory -- which you assign to name
.当您调用
malloc
, malloc
分配一块内存,提供strlen("MyName") + 1
个字节,并将起始地址返回到该内存块 - 您分配给name
。 You then can copy "MyName"
to name (with 1-byte remaining for the nul-terminating character).然后,您可以将
"MyName"
复制到 name(空终止字符剩余 1 个字节)。 The approach would be:方法是:
size_t len = strlen ("MyName");
char *name = malloc (len + 1); /* allocate len + 1 bytes */
if (name == NULL) { /* validate EVERY allocation */
perror ("malloc-name");
/* handle error by returning or exiting */
}
memcpy (name, "MyName", len + 1); /* no need to scan again for \0 */
/* do something with name - here */
free (name); /* don't forget to free name when you are done */
What then does char**
do?那么
char**
做什么呢?
When you are dealing with a pointer-to-pointer-to-char , you must first allocate for some number of pointers , then you can allocate and assign a block of memory to each of the pointers and use each pointer just as you have used name
above.当您处理指向字符的指针时,您必须首先分配一定数量的指针,然后您可以为每个指针分配和分配一块内存,并像以前一样使用每个指针上面的
name
。
For example:例如:
/* array of ponters to string-literals for your source of strings */
char *band[] = { "George", "Ringo", "Paul", "John" };
char **names;
size_t nmembers = sizeof band / sizeof *band;
/* allocate nmembers pointers */
names = malloc (nmembers * sizeof *names);
if (names == NULL) { /* validate EVERY allocation */
perror ("malloc-name_pointers");
/* handle error by returning or exiting */
}
/* now loop allocating for each name and copy */
for (size_t i = 0; i < nmembers; i++) {
size_t len = strlen (band[i]); /* get length */
names[i] = malloc (len + 1); /* allocate */
if (names[i] == NULL) { /* validate EVERY allocation */
perror ("malloc-names[i]");
/* handle error by returning or exiting */
}
memcpy (names[i], band[i], len + 1);/* no need to scan again for \0 */
}
/* output each */
for (size_t i = 0; i < nmembers; i++)
printf ("member[%zu]: %s\n", i + 1, names[i]);
Freeing names
is a two step process.释放
names
是一个两步过程。 You must free the memory allocated to each of the names
pointers and then free the pointers themselves, eg您必须释放分配给每个
names
指针的内存,然后释放指针本身,例如
for (size_t i = 0; i < nmembers; i++)
free (names[i]); /* free each allocated names[i] */
free (names); /* free pointers */
Now hopefully you more closely "... fully understand how it works"
.现在希望你更密切地
"... fully understand how it works"
。 Let me know if you have any questions.如果您有任何问题,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.