[英]C passing array of pointers
This relates to C. I am having some trouble understanding how I can assign strings to char pointers within arrays from a function. 这与C有关。我在理解如何将字符串分配给函数数组中的char指针时遇到了一些麻烦。
#include <stdio.h>
#include <string.h>
void function(char* array[]);
int main(void)
{
char* array[50];
function(array);
printf("array string 0: %s\n",array[0]);
printf("array string 1: %s\n",array[1]);
}
void function(char* array[])
{
char temp[] = "hello";
array[0] = temp;
array[1] = temp;
return;
}
Ideally, I would like the main printf function to return 理想情况下,我希望主printf函数返回
array string 0: hello
array string 1: hello
But I'm having trouble understanding arrays of pointers, how these pass to functions and how to manipulate them in the function. 但是我在理解指针数组,如何将它们传递给函数以及如何在函数中进行操作方面遇到困难。 If I declare a string like
char temp[] = "string"
then how do I assign this to one of the main function array[i]
pointers? 如果我声明了像
char temp[] = "string"
这样的char temp[] = "string"
那么如何将其分配给主函数array[i]
指针之一? (assuming I have my jargon right) (假设我的行话正确)
char temp[] = "hello";
only creates a local, temporary array inside the function. 仅在函数内部创建一个本地临时数组。 So when the function exists, the array will be destroyed.
因此,当函数存在时,数组将被销毁。
But with array[0] = temp;
但是用
array[0] = temp;
you're making array[0]
point to the local array temp
. 您正在将
array[0]
指向本地数组temp
。
After the function returns, temp
doesn't exist anymore. 函数返回后,
temp
不再存在。 So accessing array[0]
which pointed to temp
will cause undefined behavior. 因此,访问指向
temp
array[0]
将导致未定义的行为。
You could simply make temp
static, so it also exists outside the function: 您可以简单地将
temp
静态,因此它也存在于函数外部:
static char temp[] = "hello";
Or, you could copy the "hello"
string to array[0]
and array[1]
. 或者,您可以将
"hello"
字符串复制到array[0]
和array[1]
。 For copying C-strings, you normally use strcpy
. 要复制C字符串,通常使用
strcpy
。
char temp[] = "hello";
strcpy(array[0], temp);
strcpy(array[1], temp);
However , before copying you need to make sure array[0]
and array[1]
point to memory that has enough space to hold all characters of "hello"
, including the terminating null character. 但是 ,在复制之前,您需要确保
array[0]
和array[1]
指向具有足够空间来容纳"hello"
所有字符(包括终止空字符)的内存。 So you have to do something like this before calling strcpy
: 因此,在调用
strcpy
之前,您必须执行以下操作:
array[0] = malloc(6);
array[1] = malloc(6);
(6 is the minimum numbers of characters that can hold "hello"
.) (6是可容纳
"hello"
的最小字符数。)
how do I assign this to one of the main function array[i] pointers
如何将其分配给主函数array [i]指针之一
Referring 1. 参考1。
This 这个
char temp[] = "hello";
isn't an assigment, but an initialisation. 不是任务,而是初始化。
This 这个
char temp[];
temp[] = "hello";
would not compile (the 2nd line errors), as an array cannot be assigned. 无法编译(第二行错误),因为无法分配数组。
Referring 2. 参考2。
This 这个
char* array[50];
defines an array of 50 pointers to char
, it could reference 50 char
-arrays, that is 50 C-"strings". 定义了一个由50个指向
char
指针组成的数组,它可以引用 50个char
-arrays,即50个C-“字符串”。 It cannot hold the C-"strings" themselfs. 它本身不能容纳C-“字符串”。
Example 例
Applying what is mentioned above to your code whould lead to for example the following: 将上面提到的内容应用于您的代码,可能会导致例如以下情况:
#include <stdio.h>
#include <string.h>
void function(char* array[]);
int main(void)
{
char* array[50];
/* Make the 1st two elements point to valid memory. */
array[0] = malloc(42); /* Real code shall test the outcome of malloc()
as it might fail and very well return NULL! */
array[1] = malloc(42);
function(array);
printf("array string 0: %s\n",array[0]);
printf("array string 1: %s\n",array[1]);
return 0;
}
void function(char* array[])
{
char temp[] = "hello";
/* Copy the content of temp into the memory that was allocated to
the array's 1st memebrs in main(). */
strcpy(array[0], temp);
strcpy(array[1], temp);
return;
}
first, you need to allocate the destination. 首先,您需要分配目的地。
second, char temp[] = "hello";
第二,
char temp[] = "hello";
in function() is local variable. 在function()中是局部变量。 you cannot use their outside of the function.
您不能在功能之外使用它们。 use
strcpy
to copy the content. 使用
strcpy
复制内容。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void function(char* dest)
{
char temp[] = "hello";
strcpy(dest, temp);
return;
}
int main(void)
{
// or just char dest[10] = {0};
char *dest = malloc(10);
function(dest);
printf("dest: %s\n", dest);
}
In you program, you defined char* array[50];
在您的程序中,您定义了
char* array[50];
, so you need to create memory space for each item: ,因此您需要为每个项目创建存储空间:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void function(char* a[])
{
char temp[] = "hello";
strcpy(a[0], temp);
strcpy(a[1], temp);
return;
}
int main(void)
{
char *a[50];
int i = 0;
for (i = 0; i < 50; ++i)
a[i] = malloc(10);
function(a);
printf("a[0]: %s\n", a[0]);
printf("a[1]: %s\n", a[1]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.