944,010 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 574
  • C RSS
Nov 5th, 2009
0

Hwk: how do I make a 2d array of pointers point to another array

Expand Post »
Hi, I'm new here and I just need a little help with pointers and 2D arrays. I'm kind of new to programming so this might be redundant. I'm trying to make a 2D array of pointers (already have done) that points to another 2D array of double values... I have the code:

  1. for (i = 0; i < n; i++)
  2. for (j = 0; j < n; j++)
  3. ptrA[i][j] = a[j][i];
**its transposed for a reason**

However when I do calculations on array ptrA, it doesnt change the values in array a...

ptrA and a are declared by
  1. double a[n][n];
  2. double temp;
  3. for (i = 0; i < n; i++){ //intializing [A] to inputs
  4. for (j = 0; j < n; j++){
  5. scanf("%lf", &temp);
  6. a[i][j] = temp;
  7. }
  8. }
  9.  
  10. double **ptrA = malloc(n*sizeof(*ptrA));
  11. if (ptrA != NULL){
  12. for (i = 0; i < n; i++)
  13. ptrA[i] = malloc(n*sizeof(*ptrA[i]));
  14. } else return 1;
Last edited by Tamaki; Nov 5th, 2009 at 2:20 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Tamaki is offline Offline
6 posts
since Nov 2009
Nov 5th, 2009
-7
Re: Hwk: how do I make a 2d array of pointers point to another array
variable ptrA needs to have three stars
  1. #define maxrows 2
  2. #define maxcols 5
  3.  
  4.  
  5. int main()
  6. {
  7. double a[maxrows][maxcols];
  8. double ***ptrA = malloc(maxrows * sizeof(double*));
  9. int i,j;
  10.  
  11. for(i = 0; i < maxrows; i++)
  12. {
  13. ptrA[i] = malloc(maxcols * sizeof(double*));
  14. for(j = 0; j < maxcols; j++)
  15. {
  16. ptrA[i][j] = &a[i][j];
  17. }
  18. }
  19. }
Last edited by Ancient Dragon; Nov 5th, 2009 at 11:39 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Nov 6th, 2009
0
Re: Hwk: how do I make a 2d array of pointers point to another array
OHHH thank you!
I solved the problem without it anyways... thank you
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Tamaki is offline Offline
6 posts
since Nov 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: c langauge 2nd edition
Next Thread in C Forum Timeline: need help!!! to fix my program please!!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC