[英]Pass char** as an argument to a function in C
I know there are many topics of this kind but I've read several of them and still can't figure out what am I doing wrong. 我知道有很多这样的主题,但是我已经阅读了其中的几个主题,但仍然无法弄清楚我在做什么错。
I've successfully generated a char** array. 我已经成功生成了一个char **数组。 My bubble sort function probably works as well.
我的冒泡排序功能可能也可以正常工作。 But when I passed the generated array to the function, only 1 row is copied.
但是,当我将生成的数组传递给函数时,仅复制了一行。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
void sort(char** tab)
{
char* temp;
int i, j, size = sizeof(tab)/sizeof(tab[0]);
printf("%d\n", size);
for(i = 0; i < size; ++i)
{
for(j = i+1; j < size; ++j)
{
if(strcmp(tab[j-1], tab[j]) > 0)
strcpy(temp, tab[j-1]),
strcpy(tab[j-1], tab[j]),
strcpy(tab[j], temp);
}
}
for(i = 0; i < sizeof(tab)/sizeof(tab[0]); ++i)
puts(tab[i]);
}
int main()
{
srand(time(NULL));
int size = rand()%5+5, i, j, s;
char** tab = (char**)malloc(size * sizeof(char*));
for(i = 0; i < size; ++i)
{
s = rand()%9+1;
tab[i] = (char*)malloc(s+1);
for(j = 0; j < s; ++j)
tab[i][j] = 'a'+rand()%26;
tab[i][s] = 0;
}
for(i = 0; i < size; ++i)
puts(tab[i]);
puts("");
sort(tab);
return 0;
}
Here 's how the code works. 这是代码的工作方式。
And when I write size=5 before the loop in the function it returns segmentation fault. 当我在函数中的循环之前写入size = 5时,它返回分段错误。
Edit: Same with passing the size of the array as an argument: http://ideone.com/3Wvncq 编辑:与传递数组的大小作为参数相同: http : //ideone.com/3Wvncq
I've fixed all the problems and here's the final code . 我已经解决了所有问题,这是最终代码 。 I was misinterpreting segmentation fault as the result of assigning a fixed size instead of not allocating the temp variable.
由于分配固定大小而不是不分配temp变量,导致我误解了分段错误。 Thank you for all the answers.
感谢您的所有答案。
Don't calculate size inside function void sort(char** tab)
. 不要在
void sort(char** tab)
计算大小。 As in this function it will be calculated as - 与该函数一样,它将被计算为-
int i, j, size = sizeof(tab)/sizeof(tab[0]); // equivalent to sizeof(char **)/sizeof(char*) in function giving wrong length as you desire.
It's length in main
( size
is generated using rand
so no need to find it) and then pass it as argument to function sort
. 它是
main
的长度( size
是使用rand
生成的,因此不需要找到它),然后将其作为参数传递给函数sort
。
Declare your function like this - 这样声明您的功能-
void sort(char** tab,size_t size)
And while calling from main pass length of tab
to it - 在从
tab
主传递长度对其进行调用时-
sort(tab,size); // size will be number of elements in tab calculated in main
You get segmentation fault because of this - 由于这个原因,您会遇到细分错误 -
if(strcmp(tab[j-1], tab[j]) > 0)
strcpy(temp, tab[j-1]),
strcpy(tab[j-1], tab[j]),
strcpy(tab[j], temp);
temp
is uninitialized in sort
and still you pass it to strcpy
thus undefined behaviour . temp
是未初始化的sort
和你仍然把它传递给strcpy
从而未定义行为 。 Initialize temp
before passing to strcpy
.Allocate memory to temp
in function sort
. 在传递给
strcpy
之前初始化 temp
。在函数sort
中将内存分配给temp
。
In your sort
function you declare the temp
variable: 在您的
sort
函数中,声明temp
变量:
char* temp;
Later you use it as destination (and source) for string copying: 以后,您可以将其用作复制字符串的目标(和源):
strcpy(temp, tab[j-1]),
But nowhere in between do you make temp
point anywhere, temp
is uninitialized and that leads to undefined behavior and your crash. 但无处之间做你让
temp
点的任何地方, temp
是未初始化的并且导致不确定的行为和你的崩溃。
Don't use a pointer, instead declare it as an array of the largest string size possible. 不要使用指针,而是将其声明为可能的最大字符串大小的数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.