| | |
Hwk: how do I make a 2d array of pointers point to another array
Thread Solved |
•
•
Join Date: Nov 2009
Posts: 6
Reputation:
Solved Threads: 0
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:
**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
C Syntax (Toggle Plain Text)
for (i = 0; i < n; i++) for (j = 0; j < n; j++) ptrA[i][j] = a[j][i];
However when I do calculations on array ptrA, it doesnt change the values in array a...
ptrA and a are declared by
C Syntax (Toggle Plain Text)
double a[n][n]; double temp; for (i = 0; i < n; i++){ //intializing [A] to inputs for (j = 0; j < n; j++){ scanf("%lf", &temp); a[i][j] = temp; } } double **ptrA = malloc(n*sizeof(*ptrA)); if (ptrA != NULL){ for (i = 0; i < n; i++) ptrA[i] = malloc(n*sizeof(*ptrA[i])); } else return 1;
Last edited by Tamaki; 19 Days Ago at 2:20 pm.
-6
#2 19 Days Ago
variable ptrA needs to have three stars
C Syntax (Toggle Plain Text)
#define maxrows 2 #define maxcols 5 int main() { double a[maxrows][maxcols]; double ***ptrA = malloc(maxrows * sizeof(double*)); int i,j; for(i = 0; i < maxrows; i++) { ptrA[i] = malloc(maxcols * sizeof(double*)); for(j = 0; j < maxcols; j++) { ptrA[i][j] = &a[i][j]; } } }
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.
![]() |
Similar Threads
- Array of pointers (C++)
- Adding an array to an element of the array (C++)
- Passing 2D Array of Pointers into a function (C++)
- Usage of array of pointers (C++)
- read line of text from file into array (C++)
- Array of pointers to structure (C)
- Array of pointers to objects (C++)
Other Threads in the C Forum
- Previous Thread: c langauge 2nd edition
- Next Thread: need help!!! to fix my program please!!!
| Thread Tools | Search this Thread |
#include * ansi api array arrays asterisks binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() database dynamic execv fflush fgets file floatingpointvalidation fork forloop frequency function getlogicaldrivestrin givemetehcodez grade graphics gtkwinlinux histogram homework i/o inches include infiniteloop input interest intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft mysql oddnumber open opendocumentformat openwebfoundation pdf pointer posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked reversing scanf scheduling segmentationfault send sequential shape single socket socketprogramming stack standard strchr string suggestions test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






