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

Thread Solved

Join Date: Nov 2009
Posts: 6
Reputation: Tamaki is an unknown quantity at this point 
Solved Threads: 0
Tamaki Tamaki is offline Offline
Newbie Poster

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

 
0
  #1
20 Days Ago
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; 20 Days Ago at 2:20 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
-7
  #2
19 Days Ago
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; 19 Days Ago at 11:39 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 6
Reputation: Tamaki is an unknown quantity at this point 
Solved Threads: 0
Tamaki Tamaki is offline Offline
Newbie Poster
 
0
  #3
19 Days Ago
OHHH thank you!
I solved the problem without it anyways... thank you
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC