| | |
Hwk: how do I make a 2d array of pointers point to another array
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
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; 32 Days Ago at 2:20 pm.
-7
#2 32 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; 32 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 * append array arrays asterisks bash binarysearch calculate changingto char character cm copyimagefile creafecopyofanytypeoffileinc createprocess() database dynamic execv feet fgets file floatingpointvalidation fork forloop framework function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide include incrementoperators input intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix meter microsoft mqqueue number oddnumber odf opensource openwebfoundation overwrite owf pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing scripting segmentationfault sequential single socket socketprogramming standard strchr string systemcall testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






