1

Im new to C coding. In order to learn the language i want to do following little "program".

I want to scan inputs , and put them into 2d array - but i do now know how many elements i want to scan / input = i want to dynamicly create 2d array. And here is the problem. I know how i can dynamicly create 1d array e.g

  int MAX;
  int *arr;
  scanf("%d",&MAX);
  arr=(int*)malloc( Max * sizeof ( int ) )

i found how to allocate 2d array e.g

  int X;
  int Y;
  int **arr;
  scanf("%d%d",&X,&Y);
  arr=(int*) malloc ( X * Y * sizeof ( int* ) )

But i havent found the thing i need the most = create 2d array and increase its " memory or size " every time new item is being added. For example - what i would like to achieve.

    int **arr;
    int index=1;
    int X;
    int Y;
    arr=(int *) malloc ( index * sizeof ( int ) );
    while (! feof ){
       scanf("%d%d",&X,&Y);
       if ( index > 1 ){
         index ++;
         arr=realoc( arr*, index * sizeof ( arr* ) )
         arr[iX][0]=X;
         arr[iX][1]=Y;


       } else{
      arr[iX][0]=X;
      arr[iX][1]=Y;
      index++;
    }

    }

this was my attempt and i failed horribly.. how can dynamicly change size of 2d array on every input (or every iteration of loop ) i am aware of this solution but all answer are used with predefined y-osis of array. e.g arr[][25]

8
  • Possible duplicate of defining a 2D array with malloc and modifying it Commented Nov 28, 2015 at 18:40
  • Consider using realloc(). Commented Nov 28, 2015 at 18:42
  • could you please explain how could i achieve it with realloc? Commented Nov 28, 2015 at 18:55
  • Did you take a look at the link I posted? There are plenty of examples there. Commented Nov 28, 2015 at 18:55
  • i am , but as i said all solutions use predefined size of "second dimension" where i do not know how many inputs are gonna be there. Commented Nov 28, 2015 at 18:59

1 Answer 1

0

First you need a field of pointers, each pointer in the field of pointers points to the first element of a row/column of your matrix. Each time you want to add a value to your row/column you have to increase your row/column with realloc. Each time you want to add a new row/column you have to add one pointer to your field of pointers. Thus you need two conditions of truncation, one for the row/column and one for the field of pointers.

The following code shows an example how that can be realized. However be aware that the program may terminate due to allocation errors.

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

main(){
//numberofrows points to the pointers used for the columns countrows tells us how many pointers we have
char **numberofrows;
int countrows=0;
//countcolumns tells us how much memory has to be allocated for the current column
int countcolumns=0;
numberofrows=malloc(sizeof(char*));
//t is needed for the first run of the loop otherwise the program will likely terminate
int t=0;
//truncation condition: e signals the end of the usersinput
while(t==0||numberofrows[countrows-1][countcolumns-1]!='e'){
    countcolumns=0;
    countrows++;
    t=0;
    //allocation of new array and reallocation of pointer array
    numberofrows[countrows-1]=malloc(sizeof(char));
    *numberofrows=realloc(*numberofrows,sizeof(char*)* countrows);

    //truncation condition \n: end of current column, condition e: end of input for 2d array 
    while(t==0||(numberofrows[countrows-1][countcolumns-1]!='\n'&&numberofrows[countrows-1][countcolumns-1]!='e')){
        countcolumns++;
        //change for t to activate truncation conditions
        t=1;
        // info for what postion new value is needed realloc to make place for additional value and setting of new value
        printf("row: %d, column: %d\n",countrows,countcolumns);
        numberofrows[countrows-1]=realloc(numberofrows[countrows-1],sizeof(char)*countcolumns);
        numberofrows[countrows-1][countcolumns-1]=getchar();
        //clears input buffer
        if(numberofrows[countrows-1][countcolumns-1]!='\n')
            while(getchar()!='\n'){}

    }
}

//prints transposed 2d array    
int tc=0,k=0;
while(tc<countrows){
    k=0;

    while(numberofrows[tc][k]!='\n'&& numberofrows[tc][k]!='e'){
        printf("%c ",numberofrows[tc][k]);
        k++;
    }
    printf("\n");
        tc++;
    }


}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.