-1

I have several numbers that i want to insert into Matrix.

This is how i am get all my numbers one by one:

int i, n;
int ch;
int *arr;
int dimension;
int numbers = 0;
char str[512];

// User input.
fgets(str, sizeof str, stdin);

    for (i = 0; i <= (strlen(str)); i++)
    {
        if (str[i] != '\0' && !isspace(str[i]))
        {
            int num = atoi(&str[i]);
            numbers++;
            if (i == 0)
            {
                dimension = num;
                arr = allocatearraysize(dimension);
            }

            // Here i want to add the current number to my `Maxtix`.
        }
    }

free(arr);

int* allocatearraysize(int size)
{
    return (int *)malloc(size * size * sizeof(int));
}

So i try:

arr[0][0] = num; 

only for see if thats works but got an error:

expression must have pointer-to-object type

Edit

So if my input is 2 1 2 3 4:

the first number (2) means that my matrix should be 2x2 and i expected 4 numbers after this number (2).

In case the numbers of number not match the first number for example:

3 1 2 3 4

The first number here is 3 so after this number i excepted to 9 numbers so in this case i only want to print error message.

But any way i want to insert this numbers into my Matrix.

user2908206
  • 265
  • 3
  • 16
  • 1
    shouldn't it be `num = atoi(str[i])`? – kulan Nov 23 '17 at 11:20
  • 1
    @GokulanRavi No it should not – M.M Nov 23 '17 at 11:22
  • 1
    `atoi` expects a `char *` as parameter, not a char. You have to put just `atoi(str)`. Please read the manual of a function before using it – josemartindev Nov 23 '17 at 11:22
  • 1
    This code has quite a lot of problems, starting with the fact you are iterating over each character of an input string but it looks like you want to just read the string into an int. – John Zwinck Nov 23 '17 at 11:22
  • Thats right, i only want int – user2908206 Nov 23 '17 at 11:23
  • arr points to a 1-d array. For the (i, j) element you should write `arr[i * row_size + j]`, where `i` is the row index and `j` is the column index. Otherwise use a double pointer – Simone Cifani Nov 23 '17 at 11:23
  • What is the input you are giving ? If you give 3 1 2 3, then it will create a 1D array of 3*3 ie. of size 9. But your for loop is running only 4 times ? – Bhawan Nov 23 '17 at 11:24
  • for example 2 1 2 3 4, the first number (2) means that this matrix will be 2x2 so after the number 2 i need another 4 numbers – user2908206 Nov 23 '17 at 11:25
  • Are you sure you want a Matrix (like arr[20][100]) or do you want a dynamic array (like arr[99]) where the size (99) is dynamic? (Which you control with malloc)... – Grantly Nov 23 '17 at 11:29
  • If the number of numbers after the first number (which is the array dimension) is wrong i just print error message. – user2908206 Nov 23 '17 at 11:30
  • Please edit your question with extra clarification....What inputs you use, and what outputs you expect – Grantly Nov 23 '17 at 11:30
  • Please see my edit – user2908206 Nov 23 '17 at 11:35
  • Thanks for the edit. For a start, shouldn't you define your Matrix (2-dimensional array) as int **arr; ? Which means alot of small changes to your code – Grantly Nov 23 '17 at 11:39
  • What do you mean ? – user2908206 Nov 23 '17 at 11:41
  • You use the word Matrix, which is a multi dimensional array (usually). You are only declaring a single dimensional array with int *arr; Perhaps you want to research Matrices in C first...Google for an example – Grantly Nov 23 '17 at 11:44
  • See here perhaps for a few examples, not sure which one you are wishing to use... http://www.geeksforgeeks.org/dynamically-allocate-2d-array-c/ – Grantly Nov 23 '17 at 11:51
  • @Grantly Geeks for geeks is a trash site. Sure, they show the correct way to do this in C90 (1st example), but they don't show the correct way to do this in standard C. Please don't link to external trash sites written by students, when we have SO that contains much higher quality posts written by domain experts. – Lundin Nov 23 '17 at 12:14
  • @Lundin LOL yes I didn't spend much time finding an example for the OP...I didn't realise it was trash - just thought the 3/4 examples were worthwhile for the OP to look at... Yes I overlooked some SO examples as they seemed too complex for the OP at the moment. Please add a better link if you have time. Thanks :) – Grantly Nov 23 '17 at 12:16
  • 1
    @Grantly I already closed this as a duplicate, you can follow the [link](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays). Sure, I am partial since I wrote that post, though if you look at [How do I correctly set up, access, and free a multidimensional array in C?](https://stackoverflow.com/questions/12462615/how-do-i-correctly-set-up-access-and-free-a-multidimensional-array-in-c) you find the same kind of recommendations posted by domain experts, C programming book authors and members of the C standard committee. – Lundin Nov 23 '17 at 12:19
  • @Lundin Awesome, thanks. (Upvoted too) – Grantly Nov 23 '17 at 12:20

1 Answers1

0

You can't access arrays with var[i][j] if it's not a 2-dimensional array.

A possible answer would be :

int i, j, n;
int ch;
int **arr;
int size;
int numbers = 0;
char str[512];

fgets(str, sizeof str, stdin);

size = atoi(&str[0]);

if(size > 1) {
    arr = (int**) malloc(size * sizeof(int*))

    for (i = 0; i < size; i++)
        arr[i] = (int*) malloc(size * sizeof(int));

    // Fill with arr[i][j]

    free(arr);
}
else {
    fprintf(stderr, "Size must be a valid number");
}
Marcadia
  • 538
  • 1
  • 4
  • 9
  • This is not a 2-dimension array either, yet you can access elements through `[x][y]`. Your answer contradicts itself. – Lundin Nov 23 '17 at 12:04
  • @Lundin do you say that because he is actually using a pointer to a pointer? (Which is not strictly a 2D array)? I'm just thinking that this dynamic allocation makes it clear to the OP - even though its not exactly accurate... – Grantly Nov 23 '17 at 12:07
  • @Grantly Yes, this is not a 2D array, it is a form of lookup table. It is significantly slower than a dynamically allocated 2D array. In addition, it is more dangerous and error prone. In this case there is no need to use a pointer-based lookup table. Do not write slow and dangerous code when you can write fast and safe code to achieve the very same thing. – Lundin Nov 23 '17 at 12:11
  • @Lundin forget about stricts explanations, obviously it's a mind representation. I'm just giving a simple solution to this problem. I don't think OP is looking for the fastest, strictest code – Marcadia Nov 23 '17 at 12:13
  • @Lundin Thanks, yeah I hear you :) Be good to see your alternative in code, but I don't want to ask you to detail it here... Its a good exercise for the OP to navigate the alternatives – Grantly Nov 23 '17 at 12:14
  • How exactly is this code simpler than `int (*arr)[size] = malloc( sizeof[size][size] );` ... `free(arr);`? Ignoring the fact that your answer contains a major memory leak bug when you free() the allocated – Lundin Nov 23 '17 at 14:49