-1

I got some problem, i need to fill a 2D integer tab, so i made this function :

int **ft_fill_tab(void) {
    int **res;
    int row;
    int col;

    // creating a 15 cols by 10 rows TAB
    res = (int **)malloc(sizeof(int *) * 10);
    res[0] = (int *)malloc(sizeof(int) * 2);
    res[0][1] = 15;
    res[0][2] = 10;
    row = 1;
    col = 0;
    while (row < res[0][1])
    {
        res[row] = (int*)malloc(sizeof(int) * res[0][1]);
        while (col <= res[0][0])
        {
            res[row][col] = 0;
            col++;
        }
        row++;
        col = 1;
    }
    return (res);
}

... which i use/call from my main.c like this :

int main (void) {
    // ...
    int **tab_test;
    tab_test = ft_fill_tab();
    // ...
    return (0);
}

And when i tried to compile my program, gcc said to me : warning: assignment makes pointer from integer without a cast (main.c, on ft_fill_tab(); call row)

I also tried to cast the return value of my function as any ways as possible (even in main file), but i failed to understand from where this error came up.

... Any idea? Thanks from the future !

1 Answers1

3

Shouldn't this

res[0][1] = 15;
res[0][2] = 10;

be

res[0][0] = 15;
res[0][1] = 10;

?

Led by my sense of symmetry your code should look like this:

// creating a 15 cols by 10 rows TAB
int ** res = malloc(sizeof(int *) * 10);
res[0] = malloc(sizeof(int) * 2);

res[0][0] = 15;
res[0][1] = 10;

{
  int row = 1;
  while (row < res[0][1])
  {
    int col = 1;

    res[row] = malloc(sizeof(int) * res[0][0]);    
    while (col < res[0][0])
    {
        res[row][col] = 0;
        col++;
    }

    row++;
  }
}

Btw, I assume you left out error checking of the calls to malloc() just for the sake of readability.


Referring the error message you quote:

It looks like main() tries to call int **ft_fill_tab(void) without knowing anything about it, probably because it's defined after main() in your code. So the compiler assumes int **ft_fill_tab(void) to return the default, which is int.

This the compiler then tries to assign to int ** tab_test which leads to the error you get.

To fix this add a prototype of int **ft_fill_tab(void) before it is used, here before main():

#include <stdlib.h> /* for malloc at least */

int **ft_fill_tab(void);

int main(void)
{
  ...
  int **tab_test = ft_fill_tab();
  // ...
  return (0);
}

int **ft_fill_tab(void)
{
  ...
}
alk
  • 69,737
  • 10
  • 105
  • 255
  • Thanks for malloc (OMG i tried to malloc 3D tab xD), and i willingly decided to not fill the [0][0] integer. But problem is not solved, and it's still the same warning error : _main.c:XX: warning: assignment makes pointer from integer without a cast_ _(on ft_fill_tab() call)_ – user3054131 Dec 01 '13 at 10:28
  • and i also added proto in my header file, which i included at top of my main file – user3054131 Dec 01 '13 at 10:35
  • In C there is not need to cast the results of `malloc/calloc/realloc` nor is it recommended: http://stackoverflow.com/a/605858/694576 However you have to include `` to get there prototypes. – alk Dec 01 '13 at 10:36
  • maybe by using pointer from a ulgy **int var in my main? T-T (the ft_fill_tab function is not in main.c file) – user3054131 Dec 01 '13 at 10:37
  • and i also added all needed include in my header file – user3054131 Dec 01 '13 at 10:39
  • if I put in comments the ft_fill_tab() call row, gcc compile it without error – user3054131 Dec 01 '13 at 10:41
  • If it would be like you said, the code would compile without errors. The code most likely misses the protoype for `int **ft_fill_tab(void)`. Also consider typos and/or having a break ... ;-) – alk Dec 01 '13 at 10:46