我在C:
中有这样的功能char **collect_character_distribution(char *buffer, long lSize)
{
printf("Collecting character distribution...\n");
long x;
char distribution[256][2] = {0};
for (x = 0; x < lSize; x++)
{
distribution[(int)buffer[x]][0] = (char) buffer[x];
distribution[(int)buffer[x]][1]++;
}
return (char **) distribution;
}
我需要从上面的函数返回二维分布数组。我的主要功能如下:
int main()
{
char *buffer;
long lSize;
struct Bar result = funct();
char **distribution;
buffer = result.x;
lSize = result.y;
distribution = collect_character_distribution(buffer, lSize);
printf("%s\n", distribution);
//struct Bar encoding_tree = generate_encoding_tree(distribution);
return 0;
}
我收到以下错误:
警告:函数返回局部变量的地址[-Wreturn-local-addr]
如何解决此问题?
答案 0 :(得分:2)
您收到此错误,因为该数组是一个局部变量,并且您将其传递到其范围之外。
您需要为此
使用动态分配char **array = malloc(20 * sizeof(char *));
int i;
for(i=0; i != 20; ++i) {
array[i] = malloc(20 * sizeof(char));
}
现在您可以轻松返回array
编辑1
你所要做的就是改变
char distribution[256][2];
这个
char **array = malloc(20 * sizeof(char *));
int i;
for(i=0; i != 20; ++i) {
array[i] = malloc(20 * sizeof(char));
}
编辑2
要打印字符串,您应该循环
for(i=0; i<20; i++)
printf("%s\n", distribution[i]);
答案 1 :(得分:1)
简单的消息你的功能return
是一个局部变量。您的数组不存在于功能块之外。
在你的功能中,你可以这样做 -
char **distribution;
distribution=malloc(256*sizeof(char *));
for(int i=0;i<256;i++)
distribution[i]=malloc(20*sizeof(distribution[0][0]));
同样在main
,这个 -
printf("%s\n", distribution);
distribution
属于char **
类型,您无法将其传递给%s
说明符。
你可以循环 -
for(int i=0;i<256;i++)
printf("%s\n", distribution[i]);
#include <stdio.h>
#include <stdlib.h>
char **collect_character_distribution(char *buffer, long lSize);
int main()
{
char *buffer;
long lSize;
struct Bar result = funct();
char **distribution;
buffer = result.x;
lSize = result.y;
distribution = collect_character_distribution(buffer, lSize);
for(int i=0;i<256;i++)
printf("%s\n", distribution[i]);
//struct Bar encoding_tree = generate_encoding_tree(distribution);
for(int i=0;i<256;i++)
free(distribution[i]);
free(distribution);
return 0;
}
char **collect_character_distribution(char *buffer, long lSize)
{
printf("Collecting character distribution...\n");
long x;
char **distribution;
distribution=malloc(256*sizeof(char *));
for(int i=0;i<256;i++)
distribution[i]=malloc(20*sizeof(distribution[0][0]));
for (x = 0; x < lSize; x++)
{
distribution[(int)buffer[x]][0] = buffer[x];
distribution[(int)buffer[x]][1]++;
}
return distribution;
}
答案 2 :(得分:1)
您可以将char **distribution
作为参数传递给函数使用:
distribution = new char*[256]; distribution[0] = new char[2]; distribution[1] = new char[2];
然后您不需要从函数返回distribution
数组。问题是局部变量在其范围结束后被销毁,在您的情况下distribution
仅存在于函数collect_character_distribution
中。
答案 3 :(得分:1)
您将返回指向函数中本地声明的变量的指针。您应该做的是在您需要这些结果的范围内声明要存储结果的变量。然后将指向此变量的指针传递给要写入的函数。这些方面的东西:
void collect_character_distribution(char *buffer, long lSize, char distribution[256][2])
{
printf("Collecting character distribution...\n");
long x;
for (x = 0; x < 256; x++)
{
distribution[(int)x][0] = (char) x;
}
for (x = 0; x < lSize; x++)
{
distribution[(int)buffer[x]][1]++;
}
}
int main()
{
char *buffer;
long lSize;
struct Bar result = funct();
char distribution[256][2] = {0};
buffer = result.x;
lSize = result.y;
collect_character_distribution(buffer, lSize, distribution);
int i;
for ( i = 0; i < 256; i++ ) {
printf("%c: %d\n", distribution[i][0], distribution[i][1]);
}
//struct Bar encoding_tree = generate_encoding_tree(distribution);
return 0;
}