vendredi 14 août 2015

Overwriting character in dynamically allocated array in c

I am trying to overwrite character in 2D array.

Here is my code

#include <stdio.h>
#include <stdlib.h>

int main(void){
    int i,j;

    char** arr=malloc(sizeof(char*)*4);
    for ( i = 0; i<4;i++){      
            arr[i] = malloc(sizeof(char)*4);            
        }
    for ( i = 0; i<4; i++){
        for ( j = 0; j<4; j++){         
            if((i==0&&j==0)||(i==0&&j==3)||(i==3&&j==0)||(i==3&&j==3)){         
                arr[i][j] = '=';    
                }
            else{
                arr[i][j] = ' ';
                }                               
            printf("%c", arr[i][j]);
            }   

        printf("\n");
        }       
    arr[1][1]= '+';
    printf("%c", arr);
    return 0;
}

2D array with spcae-bar is printed firstly and i am trying to overwrite '+' in the arr

therefore, firstly, the code will return 2D array.

Assume that 0 is space-bar since i cannot upload image yet in stackflow ;(

=00=

0000

0000

=00=

After then i want to add + in the arr[1][1], so 2D array will be printed as

=00=

0+00

0000

=00=

Any help would be appreciated



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire