[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Could you recommend some good and extensive tutorials on passing

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 15
Thread images: 1

File: sad_frog_ukiyoe.jpg (32KB, 276x416px) Image search: [Google]
sad_frog_ukiyoe.jpg
32KB, 276x416px
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
Thread posts: 15
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.