c - Pass Arbitrary Sized 2 Dimension Array -
hi studying sorting algorithms, want make simple program getting array of integers text file. while doing i'm having trouble , questions regarding arrays , functions take them parameters . here do:
#include<stdio.h> #include<stdlib.h> #define max_size 64 #define max_int_size 10000 void itobuff(const char* istring,const int** sint); int getistring(file* file,char strbuffer[][max_size],int max_int,int max); int main(int argc,char*argv[]){ char buffer[max_int_size][max_size]; int int_counter=0; int int_buffer[max_int_size]; file *file = fopen("myfile.txt","r"); getistring(file,buffer,max_int_size,max_size); return 0; }
my doubt definition of function
int getistring(file* file,char strbuffer[][max_size],int max_int,int max);
i want write function allows use size array. know wrong, logically want achieve can't figure out how.
int getistring(file* file,char strbuffer[][],int max_int,int max);
i know want may done in other way want know how it. example if want write function gets array , returns determinant of array, shouldn't forced restrict k size array. or more i-columns,j-rows array other array operation.
if using c implementation supports c 1999, supports variable-length arrays.
declare function takes variable-length array parameter this:
int getistring(file *file, size_t rows, size_t columns, char buffer[][columns]);
call function this:
result = getistring(file, rows, columns, buffer);
create buffer array this:
size_t rows = calculation number of rows; size_t columns = calculation number of columns; char (*buffer)[columns] = malloc(rows * sizeof *buffer); if (!buffer) handle error.
when done, free buffer array this:
free(buffer);
if numbers of rows , columns small, can define buffer array automatic storage instead of using malloc
, free
, this:
char buffer[rows][columns];
Comments
Post a Comment