How to return a multidimensional character array from a function in a header file in C -
i have main file, , header file.
in main file, want return 2d char array char function header file. char function following:
char character_distribution(int length, char redistribution[length][2]) { char *buffer, distribution[256][2] = {0}; long lsize; struct bar result = funct(); buffer = result.x; lsize = result.y; length = collect_character_distribution(buffer, lsize, distribution); reorganize_character_distribution(length, distribution, redistribution); return redistribution; }
and main function follows:
#include <stdio.h> #include "character_distribution.h" void main() { int length; char distribution[length][2]; distribution = character_distribution(length, distribution[length][2]); int a; for(a = 0; < length; a++) { printf("%c\n", distribution[a][0]); } }
when run code, following error:
warning: return makes integer pointer without cast
how can fix problem?
void character_distribution(int length, char redistribution[][2]) { char *buffer, distribution[256][2] = {0}; long lsize; struct bar result = funct(); buffer = result.x; lsize = result.y; length = collect_character_distribution(buffer, lsize, distribution); reorganize_character_distribution(length, distribution, redistribution); } int main() { int length = 2; //initialize char distribution[length][2]; character_distribution(length, distribution); int a; for(a = 0; < length; a++) { printf("%c\n", distribution[a][0]); } return 0; }
if have return 2d array, 1 way (easy way) put in struct
struct distribution_struct { char x[256]; char y[2]; }; struct distribution_struct character_distribution(int length, char redistribution[][2]) { struct distribution_struct dis; //initialize struct values //return struct }
and way manually allocate memory 2d array in function , return
char** character_distribution(int length, char redistribution[][2]) { //use malloc create array , loop populate }
Comments
Post a Comment