Search code examples
cdynamic-memory-allocationdouble-pointer

Double pointer in c, assignment warning for my code


I don't know why I am getting warnings for the following code.

#include<stdio.h>
#include<malloc.h>
int main()
{
    int **p;
    int i,j;
    for(i=0;i<5;i++)
    {
        if(i==0)
            p=(int*)malloc(1*sizeof(int));
        else
            p=(int*)realloc(p,(i+1)*sizeof(int));
        p[i]=(int*)malloc(5*sizeof(int));

        for(j=0;j<5;j++)
        {
           p[i][j]=j;
        }
    }
    for(i=0;i<5;i++)
    {
        for(j=0;j<5;j++)
            printf("%5d",p[i][j]);
        printf("\n");
    }

    return 0;
}  

Is there any other way to allocate memory dynamically for a double pointer.


Solution

  • This compiles with no warning

      int **p;
      p = (int **) malloc(sizeof(void *));