-1

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?

ZozeR
  • 78
  • 1
  • 10

2 Answers2

1

What you mean is the following.

#include <stdio.h>
#include <string.h>

void ft_test( const char *s, size_t rows, size_t cols )
{
    unsigned int a[rows][cols];
    memset( a, 0, sizeof( a ) );

    for ( size_t i = 0, j = 0; *s != '\0'; ++s )
    {
        if ( *s == '\n' )
        {
            j = 0;
            ++i;
        }
        else 
        {
            if ( *s == '.' ) a[i][j] = 1;
            ++j;
        }
    }

    for ( size_t i = 0; i < rows; i++ )
    {
        for ( size_t j = 0; j < cols; j++ )
        {
            printf( "%u", a[i][j] );
        }
        putchar( '\n' );
    }                
}

int main( void )  
{
    char s[] = ".o.o...o\n........\n...o..o.\n";

    size_t rows = 0, cols = 0;

    for ( char *p = s, *q = s; *p; q = p )
    {
        size_t n;

        ++rows;

        p = strchr( q, '\n' );

        if ( p == NULL ) n = strlen( q );
        else n = p - q;

        if ( cols < n ) cols = n;

        p = q + n + 1;
    }

    ft_test( s, rows, cols );
}     

The program output is

10101110
11111111
11101101
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Try this :

char array1D[50];
int height = 10;
int width = 50/10;
int** array2D = (int**)malloc(sizeof(int*)*height); //Allocate the rows for the new array
for(int i = 0; i < height; ++i){
    array2D[i] = (int*)malloc(sizeof(int)*width);  //For each row, allocate columns
    for(int j = 0; j < width; ++j){
        array2D[i][j] = convert(array1D[j + width*i]); //convert the character to an integer 
    }
}

For more information, see : how to Map a 2D array onto a 1D array :

Midnight Exigent
  • 615
  • 4
  • 15