Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3665
  • Last Modified:

2d array-pointer

double **p;

.. ... .

... . ..

p= (double *) malloc(sizeof(double) * ( m*n)) ;
... ..

for ( i .... .. .
     for ( j ......


it is not working because of molloc operation. i try to do, sorted numbers which lenght is m*n... like array[m][n]..
3 Solutions
Kent OlsenData Warehouse Architect / DBACommented:
Hi thefirstfbli,

You don't need to do a double dereference here.  The structure that you're trying to create is a table of doubles, NOT a table of pointers to doubles.

Just define the structure as "double *p" and don't get caught up in the double dereferencing.

Good Luck,
thefirstfbliAuthor Commented:
ok but

for ( i=0;i<n;i++)
  but in this line ;


it gives error..

i did this like


for ( i
    for ( j

but it is not dynamic..  because i dont know the n and m values .. it comes from somewhere..
thefirstfbliAuthor Commented:
" invalid type argument of unary * "  is the fault for the line *(*(p+i)+j);

above, there is no ++ after it..
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

p= (double **) malloc(sizeof(double*) * n) ;
for( i=0;i<n;i++ ){
   *(p+i) = (double *)malloc( sizeof(double)*m);
for ( i=0;i<n;i++ ){
 for( j=0;j<m;j++ ){
Shouldn't it be
>Shouldn't it be

Pointers are set up such that (p+1) will always point to the next element after (p). "p++" or "p+1" will increase the pointer's value by the sizeof the element it's pointing to.

.malloc is fine it will allocate required memory
.problem was the statement
 here you are assuming that (p+i) will point to next row of the array which is incorrect.
Here p is **double.

To solve the problem you should declare p correctly this piece of code will solve your problem.

double (*p)[N];//pointer to array of N double data type

 p = (double (*)[N]) malloc(sizeof(double)*M*N);//type cast malloc correctly.

If you're doing one malloc, you're allocating one array.  So, all references have to be in the same array.

If you say double*p = malloc(sizeof(double)*M*N), then you have to refer to elements like p[i*N + j], assuming M is height, N is width,  i is the row and j is the column.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now