[英]passing an array of string to a function
My program is 我的程序是
#define ARRLEN 10
#define SIZEALCO 128
#define MAX_STRING_LENGTH 12
in main function, 在主要功能上
char TYPEDATA_XML_FN[ARRLEN][SIZEALCO];
char TYPEDATA_MD5_FN[ARRLEN][SIZEALCO];
char identifier[ARRLEN][MAX_STRING_LENGTH];
char Temppath[SIZEALCO];
int arraynum;
// ...
arraynum = 0;
for(arraynum = 0; arraynum <ARRLEN; arraynum++)
{
/* Create the file name with the path*/
strcpy(Temppath,"/fw/TYPEDATA/");
nameFil(Temppath,identifier[arraynum],TYPEDATA_XML_FN[arraynum],TYPEDATA_MD5_FN[arraynum]);
}
subfunction is : 子功能是:
void nameFil(char *SourPath,char *InPinName,char *FilePathNameXml,char *FilePathNameMd5)
{
sprintf(FilePathNameXml, "%s\\%s_TYPEDATA.XML",SourPath,InPinName);
sprintf(FilePathNameMd5, "%s\\%s_TYPEDATA.MD5",SourPath,InPinName);
}
I checked with your example. 我检查了你的例子。 I used (trial)
我用过(试用)
char** a = calloc(ARRLEN, sizeof(char *));
for(i = 0; i < ARRLEN ; ++i)
a[i] = ucmalloc(MAX_STRING_LENGTH);
pase(a);
subfunction : 子功能:
void pase(char b[ARRLEN][MAX_STRING_LENGTH])
{
// ...
}
Now I got the warning message as "warning: passing arg 1 of `pase' from incompatible pointer type". 现在,我得到了警告消息“警告:从不兼容的指针类型传递'pase'的arg 1”。
Actually, I would like to pass the full string array identifier,TYPEDATA_XML_FN,TYPEDATA_MD5_FN. 实际上,我想传递完整的字符串数组标识符TYPEDATA_XML_FN,TYPEDATA_MD5_FN。 Now I am passing single string to the subfunction.
现在,我将单个字符串传递给子功能。 Kindly guide me.
请指导我。 Thank you
谢谢
The prototype void pase(char b[ARRLEN][MAX_STRING_LENGTH])
is rather mis-leading, 原型
void pase(char b[ARRLEN][MAX_STRING_LENGTH])
颇具误导性,
void pase(char b[][MAX_STRING_LENGTH])
would be better, since otherwise there is the implication of bounds checking (the first array dimension is ignored). 这样会更好,因为否则会包含边界检查(忽略第一个数组维)。
The reason why you get "incompatible pointer type" is because a
is an array of pointers. 之所以得到“不兼容的指针类型”,是因为
a
是一个指针数组。 If a
was incremented (as a pointer itself) then the address would increase by the size of a pointer. 如果
a
增加(作为指针本身),则地址将增加指针的大小。 However, b
is an array of arrays of size MAX_STRING_LENGTH
, so if b
was incremented then the value would increase by MAX_STRING_LENGTH. 但是,
b
是大小为MAX_STRING_LENGTH
的数组的数组,因此,如果b
增加,则该值将增加MAX_STRING_LENGTH。
The way you have allocated the array a
will (probably) not give you contiguous memory, which is what is required here. 您分配数组
a
方式(可能)不会给您连续的内存,这是这里所需要的。 You could achieve what you want using an array of pointers, but you really must decide what you want to do. 您可以使用指针数组来实现所需的功能,但实际上必须决定要执行的操作。 If you want to use [][] notation then you need to
如果要使用[] []表示法,则需要
calloc(MAX_STRING_LENGTH,ARRLEN);
You are getting confused because although an one dimensional array char[]
behaves like a pointer char *
, in two dimensions a char[][N]
is not convertible to a char **
, being actually more like a (*char)[N]
(pointer to arrays of length n of char). 您会感到困惑,因为尽管一维数组
char[]
行为类似于指针char *
,但在二维中char[][N]
不能转换为char **
,实际上更像是(*char)[N]
(指向长度为char的数组n的指针)。
So if you want to make a function that receives a two dimensional array, you have two choices: 因此,如果要创建一个接收二维数组的函数,则有两种选择:
Use pointers to pointers: 使用指向指针的指针:
void f(char ** array, int nrows, int ncols);
To create a char**, do like you are already doing now: create an array for pointers and call malloc for each one of them. 要创建一个char **,就像现在已经做的那样:创建一个指针数组,并为每个指针调用malloc。
Use two dimensional arrays: 使用二维数组:
void f(char array[][NCOLS], int nrows); //note: NCOLS is a compile time constant now //NROWS is the first dimension and can be omited from array[NROWS][NCOLS]
The tricky bit is malloc-ing a two dimensional array: 棘手的是正在分配一个二维数组:
char (*my_array)[NCOLS]; //my_identifiers is a pointer to arrays of length NCOLS // it can be passed to any function expecting a car[][NCOLS] my_array = malloc(number_of_rows*(sizeof *my_array));
You can also make it easier to understand all of this with a good choice of typedefs: 您还可以通过选择typedef使其更容易理解所有这些内容:
typedef char MY_STRING[MAX_STR_LENGTH];
//define my strings as arrays of MAX_STRING_LENGTH
void f(MY_STRING array[]);
...
MY_STRING *arr = malloc(nstrings*sizeof(MY_STRING));
f(arr);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.