Could you recommend some good and extensive tutorials on passing 2d arrays to functions in C? Whenever I think I start to understand the subject one little program modification proves to me that I have no clue what's going on and why.
>>61743751
A 2d array is an array of arrays. An array is a pointer to a block of memory. Pass a pointer-to-pointer and two dimensions.int dosomething(int **array, int max_x, int max_y) {
for(int i = 0, i < max_x, i++)
for(int j = 0, j < max_y, j++)
printf("array[%d][%d] is %d\n", i, j, array[i][j]);
}
>>61743846
How do I interpret such error
note: expected ‘char **’ but argument is of type ‘char (*)[(sizetype)(max_chars)]’|
>>61744145
What is a char**? It is "a pointer to (or an array of) a pointer to char".
What is a char (*)[...]? It is "a pointer to (or an array of) an array of chars of size ...".
The compiler expected you to pass a char[][] but you passed a char*[].
>>61744145
Post code
Everything is easier with code
>>61744214
Sorry, I got it backwards. The compiler expected a char*[] but you passed it a char[][]. Th solution is changing the type of the argument, from char** x to char (*x)[whateversize].
int holds a single integer
int* holds a pointer to an int and contiguous memory allocation allows you to store subsequent ints
int** holds a pointer to a int* and contiguous memory allocation allows you to store subsequent int*s
Someone post the "we must go deeper" thing from Inception.
>>61744223
Ok, so more or less I have something like that (will have to divide into few posts)#include <stdio.h>
#include "textfunc.h"
int main()
{
FILE *fp = fopen("test.txt","r");
int row_count=countrows(fp);
printf("Text file have %d rows.\n", row_count);
int max_chars=maxlinechars(fp);
printf("Text file longest row have %d chars.\n", max_chars);
char file_array[row_count][max_chars];
ftoa(fp,file_array[row_count][max_chars],max_chars);
int rows;
rows=0;
while(rows<row_count)
{
puts(file_array[rows]);
rows++;
}
fclose(fp);
return 0;
}
>>61744423#ifndef TEXTFUNC_H
#define TEXTFUNC_H
#include <stdio.h>
int countrows(FILE *fp);
int maxlinechars(FILE *fp);
int max_chars;
void ftoa(FILE *fp, char file_array[][max_chars],int row_count);
#endif
for some reason I'm getting connection error every time I want to post few code blocks.
>>61744449
I don't why but I can't post the last part of program
https://pastebin.com/u1ZDQgTk
What I want to do is a simple program that loads text file into array where every row is seperate array element so
array[0]="some text"
array[1]="foo"
...
array[row_count]="bar"
The purpose of function ftoa is to load the contents of file to the array.
>>61744489
char ** list;
list = malloc sizeof(char **) * number of lines
for (i=0;i <Numberoflinesinfile; i++)
Then malloc each string
List[i] = malloc size of char * string length
Read file into this string until end of line character detected
The return your list, a char **
>>61744489
Is max_chars a global constant? Is it a global variable? Is it a macro?
>>61744423
>>61744449
https://pastebin.com/62iSqLfi
>>61743751
Dumb frogposter
>>61745171
https://pastebin.com/B5hUE0vA