For my school project i have to convert 1D char array to 2D int array and i have to send this array to another functions to work on it. but for some reason i cant create the 2D int array correctly or pass it to another functions. i need to pass the whole array! do i need to use malloc or pointers?
#include <stdio.h>
void ft_test(char *str,int w,int h)
{
int ct = 0, ct2 = 0, i = 0, j = 0;
int stt[w][h];
//this converts the chars to integers and puts it inside stt arrray
while(str[ct])
{
if (str[ct] == '\n')
{
j = 0;
i++;
}
if (str[ct] == '.')
{
stt[i][j] = 1;
j++;
}
else if (str[ct] == 'o')
{
stt[i][j] = 0;
j++;
}
ct++;
}
/*this is just to display the array but somehow there are errors in the displayed
matrix, some places needs to be 0 but displayed as 1. if you display the matrix
in the while before this, it works. but in this while it doesnt*/
ct = 0;
while (ct < h)
{
ct2 = 0;
while (ct2 < w)
{
printf("%d",stt[ct][ct2]);
ct2++;
}
printf("\n");
ct++;
}
}
int main()
{
int w = 0, ct = 0, h = 0;
char stt[] = ".o.o...o\n........\n...o..o.\n";
//counting width & height while displaying it like a matrix
while (stt[w] != '\n')
w++;
while (stt[ct] != '\0')
{
if (stt[ct] == '\n')
h++;
printf("%c",stt[ct]);
ct++;
}
printf("\n");
ft_test(stt,w,h);
return 0;
}
i dont get any errors. but the int array's some variables are incorrect. what am i missing?
instead of giving -1 vote why not describe what is wrong with my question so that i can improve it?