-4

This is my school project and I only make skeleton but compiler says

[Warning] assignment from incompatible pointer type

I changed some pointers but it can't work. Can anyone help me?

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

int main(int argc, char *argv[])
{

    int *col_num, *row_num, **matrix;
    double k, l;
    int i, j;

    printf("Enter your column number:\n");
    scanf("%d", &col_num);
    printf("Enter your row number:\n");
    scanf("%d", &row_num);

    matrix[*col_num] = (int **) malloc(*col_num * sizeof(int));
    if (matrix[*col_num] == NULL)
        printf("No free Memory!");
    matrix[*row_num] = (int **) malloc(*row_num * sizeof(int));
    if (matrix[*row_num] == NULL)
        printf("No free Memory!");

    for (i = 0; i < *col_num; i++)
        for (j = 0; j < *row_num; j++)
        {

            printf("Please Enter %d%d matrix:", i + 1, j + 1);
            scanf("%lf", &matrix[i][j]);
        }

    for (i = 0; i < *col_num; i++)
    {
        for (j = 0; j < *row_num; j++)
        {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }

    free(matrix);
    return 0;
}
Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62
UtkuErem
  • 1
  • 3
  • 2
    What is the full error including line number? – Paul Rooney Dec 20 '16 at 10:12
  • 2
    `scanf("%lf",&matrix[i][j]);` : `%lf` --> `%d` Also `matrix`'s allocation is wrong – BLUEPIXY Dec 20 '16 at 10:13
  • You have a lot of confusion on what is a pointer and a pointer to pointer.... – LPs Dec 20 '16 at 10:15
  • 3
    What are you thinking `matrix[*col_num] = (int **) malloc(*col_num * sizeof(int));` does? – LPs Dec 20 '16 at 10:17
  • 2
    You should gain basic knowledge about pointers and addresses of variables. `row_num` and `col_num` are (uninitialized) pointers to int. With `scanf("%d", &col_num);` you assign the input value as address where it points to. (Remember: You don't have memory allocated there) Then you dereference `col_num`: `matrix[*col_num] = (int **)...` – Gerhardh Dec 20 '16 at 10:18
  • 1
    I second @Gerhardh. Start with a *very* simple program which reads an int from the standard input and prints it. Only then proceed to a one-dimensional array. Make sure you understand that. Only then proceed to (the various ways to implement) two-dimensional arrays. – Peter - Reinstate Monica Dec 20 '16 at 10:20
  • 1
    @PeterA.Schneider _two-dimensional array_....that isn't a pointer to pointer... ;) – LPs Dec 20 '16 at 10:21
  • @LPs ... but you *can* `**p` it ;-) .... – Peter - Reinstate Monica Dec 20 '16 at 10:22
  • @UtkuErem, can you explain more what this code is supposed to do? – RoadRunner Dec 20 '16 at 10:38
  • @PaulRooney errores are matrix[*col_num] = (int **) malloc(*col_num * sizeof(int));" and "matrix[*row_num] = (int **)malloc(*row_num * sizeof(int));" – UtkuErem Dec 20 '16 at 10:48
  • @Gerhardh i know but my teacher wants to compose row sum and column sum vectors using dynamic memory allocation. – UtkuErem Dec 20 '16 at 10:50
  • @RoadRunner this program made a matrix. User enter column number and row number after that user enter numbers and program write on the screen. i don't write yet but program make vector sum and row sum also if i add file reader i get extra points – UtkuErem Dec 20 '16 at 10:52
  • @UtkuErem I think Gerhardh is suggesting you walk before you run. If you don't understand the foundations the more complex stuff will be out of your reach. The pointer to pointer stuff is tough to grok, no mistake about it. – Paul Rooney Dec 20 '16 at 10:55

1 Answers1

1

Firstly, in this segment:

int *col_num, *row_num, **matrix;

printf("Enter your column number:\n");
scanf("%d", &col_num);
printf("Enter your row number:\n");
scanf("%d", &row_num);

col_num and row_num shouldn't be pointers, and you could just do this instead:

int col_num, row_num, **matrix;

printf("Enter your column number:\n");
scanf("%d", &col_num);
printf("Enter your row number:\n");
scanf("%d", &row_num);

Furthermore, checking the return value of scanf() is not a bad idea either, just in case the user enters some rubbish.

You also seem to write things like matrix[*row_num] = ......... alot in your program, which is just unnecessary. You shouldn't have to reference *row_num and *col_num anywhere. Instead just call row_num and col_num by themselves, and that should be fine.

If you want to use **matrix, which is hard to use at first, you need to use malloc carefully to accomplish this. Please also see Should I cast the return value of malloc.

First start by:

  • Allocating memory for the rows:

    int **matrix = malloc(row_num * sizeof(*matrix)); /* check return value */
    
  • Then for each row, malloc() some memory for the columns:

    for (i = 0; i < row_num; i++) {
        matrix[i] = malloc(col_num * sizeof(*matrix[i])); /* check return value */
    
  • Then free the pointers at the end:

    free(matrix[i]);
    
    free(matrix);
    

After using these ideas, your code should look something like this(unless I have misunderstood the purpose of your code):

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

int
main(int argc, char *argv[]) {
    int row_num, col_num, **matrix;
    int i, j;

    printf("Enter your column number: ");
    if (scanf("%d", &col_num) != 1) {
        printf("Invalid input\n");
        exit(EXIT_FAILURE);
    }

    printf("Enter your row number: ");
    if (scanf("%d", &row_num) != 1) {
        printf("Invalid input\n");
        exit(EXIT_FAILURE);
    }

    matrix = malloc(row_num * sizeof(*matrix)); /* Or sizeof(int *) */
    if (!matrix) {
        printf("Cannot allocate memory for pointer.\n");
        exit(EXIT_FAILURE);
    }

    for (i = 0; i < row_num; i++) {
        matrix[i] = malloc(col_num * sizeof(*matrix[i])); /* Or sizeof(int) */
        if (!matrix[i]) {
            printf("Cannot allocate memory for pointer.\n");
            exit(EXIT_FAILURE);
        }

        for (j = 0; j < col_num; j++) {
            printf("Please Enter %d%d matrix: ",i+1,j+1);
            if (scanf("%d", &matrix[i][j]) != 1) {
                printf("Invalid input\n");
                exit(EXIT_FAILURE);
            }
        }
    }

    printf("Your 2D array:\n");
    for (i = 0; i < row_num; i++) {
        for (j = 0; j < col_num; j++) {
            printf("%d ", matrix[i][j]);
        }
        free(matrix[i]);
        matrix[i] = NULL;
        printf("\n");
    }

    free(matrix);
    matrix = NULL;

    return 0;
}
Community
  • 1
  • 1
RoadRunner
  • 25,803
  • 6
  • 42
  • 75