Solved

2d array-pointer

Posted on 2006-10-30
8
3,661 Views
Last Modified: 2008-06-06
double **p;

.. ... .

... . ..


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

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

            *(*(p+i)+j)++;
}
}



it is not working because of molloc operation. i try to do, sorted numbers which lenght is m*n... like array[m][n]..
0
Comment
Question by:thefirstfbli
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 17837541
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,
Kent
0
 
LVL 1

Author Comment

by:thefirstfbli
ID: 17837702
ok but

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

    *(*(p+i)+j)++;
}

it gives error..

i did this like

arr[100][100];

for ( i
    for ( j
        a[i][j]=inc;
inc++;

but it is not dynamic..  because i dont know the n and m values .. it comes from somewhere..
0
 
LVL 1

Author Comment

by:thefirstfbli
ID: 17837732
" invalid type argument of unary * "  is the fault for the line *(*(p+i)+j);

above, there is no ++ after it..
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 84

Assisted Solution

by:ozo
ozo earned 100 total points
ID: 17838683
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++ ){
    (*(*(p+i)+j))++;
  }
}
0
 
LVL 3

Expert Comment

by:benjami
ID: 17844276
Shouldn't it be
*(*(p+sizeof(double)*i)+sizeof(double)*j)++;
0
 
LVL 7

Expert Comment

by:aib_42
ID: 17847180
>Shouldn't it be
>*(*(p+sizeof(double)*i)+sizeof(double)*j)++;

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.
0
 
LVL 1

Accepted Solution

by:
Ashwini_pandey earned 300 total points
ID: 17904400

.malloc is fine it will allocate required memory
.problem was the statement
  *(*(p+i)+j)++;
 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.



0
 
LVL 22

Assisted Solution

by:NovaDenizen
NovaDenizen earned 100 total points
ID: 17916617
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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question