d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Last Question In C For A While...i Hope > Dynamic Memory Allocation For A 3d Array
12Next
Add Reply New Topic New Poll
Member
Posts: 3,210
Joined: Aug 21 2010
Gold: 152.00
Dec 12 2012 03:13pm
I am having trouble implementing a 3 dimensional array whose memory is dynamically allocated.


I am trying to think of it in terms of pointers but I am missing something conceptually because I don't quite understand how the computer handles pointers to other pointers inside of arrays

Right now here is what I have been working with:

Code
#include <stdio.h>
#include <stdlib.h>

//void testFunc(float ***);

int main() {

int x, y, z;

int *size3;
size3 = malloc(2*sizeof(int));

int *size2;
size2 = malloc(2*sizeof(int));

int *size1;
size1 = malloc(2*sizeof(int));


float **testArray[*size1][*size2][*size3];


for (x = 0; x < 2; x++) {

 for(y = 0; y < 2; y++) {

  for(z = 0; z < 2; z++) {

   printf("Enter in first number:\t");
   scanf("%f", testArray[x][y][z]);

  }

 }

}

for (x = 0; x < 2; x++) {

 for(y = 0; y < 2; y++) {

  for(z = 0; z < 2; z++) {

   printf("%.2f\n", **testArray[x][y][z]);

  }

 }

}


return 0;

}
Member
Posts: 3,546
Joined: Jan 28 2006
Gold: 1,584.00
Dec 12 2012 03:31pm
You allocated memory for size1, size2 and size3 but never fill it with numbers.

Furthermore you dont need to allocate 2*sizeof(int)

malloc(sizeof(int)) is sufficient, since the sizes only contain one integer:

Code
int *size1;
size1 = malloc(sizeof(int));
*size1 = 3; // or what length of the array you desire;-)


Then I can't really follow what you try to do :wacko:

The array declaration should be something like this:

Code
float *testArray;


The definition of the array has to be somewhere:

Code
testArray = new float[*size1][*size2][*size3]


This is a normal dynamic array. There are possibilities to get an array of pointers too.
Maybe you should be more specific :)

This post was edited by dipdipdip on Dec 12 2012 03:32pm
Member
Posts: 3,210
Joined: Aug 21 2010
Gold: 152.00
Dec 12 2012 05:51pm
All right so it's kind of working but when the loop to print runs it prints all 0's.

Code
#include <stdio.h>

#include <stdlib.h>

//void testFunc(float *);

int main() {

int x, y, z;

int *size3;

size3 = malloc(sizeof(int));



int *size2;

size2 = malloc(sizeof(int));



int *size1;

size1 = malloc(sizeof(int));

*size1 = 2;
*size2 = 2;
*size3 = 2;

float *testArray[*size1][*size2][*size3];


for (x = 0; x < *size1; x++) {

 for(y = 0; y < *size2; y++) {

  for(z = 0; z < *size3; z++) {

   printf("Enter in first number:\t");

   scanf("%f", &testArray[x][y][z]);

  }

 }

}



for (x = 0; x < 2; x++) {

 for(y = 0; y < 2; y++) {

  for(z = 0; z < 2; z++) {

   printf("%.2f\n", testArray[x][y][z]);

  }

 }

}

return 0;

}


This post was edited by Nom_Nomz on Dec 12 2012 05:56pm
Member
Posts: 3,210
Joined: Aug 21 2010
Gold: 152.00
Dec 12 2012 06:07pm
I fixed it! had to remove that asterisk from the declaration of testarray my mistake

Damn now i am having trouble on the syntax to pass it to a function >.<

This post was edited by Nom_Nomz on Dec 12 2012 06:28pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Dec 12 2012 06:57pm
Quote (Nom_Nomz @ Dec 12 2012 08:07pm)
I fixed it!  had to remove that asterisk from the declaration of testarray my mistake

Damn now i am having trouble on the syntax to pass  it to a function >.<


how is that hard.

Code
void function(int *array, int *size1, int *size2, int *size3)


pass it lik so
Code
function(testarray, size1, size2, size3);


better yet iirc you can remove the asterisks from the sizeX variables and just pass the integers to the function.

This post was edited by AbDuCt on Dec 12 2012 06:58pm
Member
Posts: 3,210
Joined: Aug 21 2010
Gold: 152.00
Dec 12 2012 07:15pm
that won't work because the array being passed is not o type float * but rather of the type it is initialized as the compiler spits out all kinds o problems when I try the way you posted because that was what I was trying to do earlier
Member
Posts: 3,210
Joined: Aug 21 2010
Gold: 152.00
Dec 12 2012 07:32pm
this is where I am at the moment

sorry for all the space it does something weird when i post it on forums

Code
#include <stdio.h>

#include <stdlib.h>



void testFunc(float *);



int main() {



int x, y, z;



int *size3;

size3 = malloc(sizeof(int));



int *size2;

size2 = malloc(sizeof(int));



int *size1;

size1 = malloc(sizeof(int));

*size1 = 11;
*size2 = 4;
*size3 = 5;


float testArray[*size1][*size2][*size3];


for (x = 0; x < *size1; x++) {



 for(y = 0; y < *size2; y++) {



  for(z = 0; z < *size3; z++) {



   printf("Enter in first number:\t");

   scanf("%f", &testArray[x][y][z]);
   printf("You've entered in %.2f \n", testArray[x][y][z]);



  }


 }



}



for (x = 0; x < 2; x++) {



 for(y = 0; y < 2; y++) {



  for(z = 0; z < 2; z++) {



   printf("%.2f\n", testArray[x][y][z]);



  }



 }



}

testFunc(testArray);



return 0;



}

void testFunc(float *testArrayF) {



int x, y, z;

for (x = 0; x < 2; x++) {



 for(y = 0; y < 2; y++) {



  for(z = 0; z < 2; z++) {



   printf("%.2f\n", testArrayF[x][y][z]);



  }



 }



}

}
Member
Posts: 3,546
Joined: Jan 28 2006
Gold: 1,584.00
Dec 13 2012 01:12am
So does this work as you intended?!
Member
Posts: 3,210
Joined: Aug 21 2010
Gold: 152.00
Dec 13 2012 09:20am
It worked up until I had to pass the array to a function, but now I realize the instructor wants the memory allocated for the set size not the set size itself allocated which now I am having problems again the fp won't read into the array anymore.

#include <stdio.h>

#include <stdlib.h>



#define SIZE_1 11

#define SIZE_2 4

#define SIZE_3 5


void avgStuClassGrade(float* [SIZE_1][SIZE_2][SIZE_3]);

void avgGradeClass(float [SIZE_1][SIZE_2][SIZE_3]);

void avgGradeStuAllClass(float [SIZE_1][SIZE_2][SIZE_3]);



int main() {

int v, x, y, z;

FILE *fp;

fp = fopen("ExamGrades.txt", "r");

float* grades[SIZE_1][SIZE_2][SIZE_3];

for (x = 0; x < 11; x++) {

for(y = 0; y < 4; y++) {

for(z = 0; z < 5; z++) {

grades[x][y][z] = malloc(sizeof(float));

}



}



}


//do {
for (x = 0; x < 11; x++) {

for(y = 0; y < 4; y++) {

for(z = 0; z < 5; z++) {

fscanf(fp, "%f", grades[x][y][z]);
printf("This is the first grade on the list: %.2f\n", grades[x][y][z]);

}

}

}

//}while ((v = fgetc(fp)) != EOF);

fclose(fp);

//avgStuClassGrade(grades);

//avgGradeClass(grades);

//avgGradeStuAllClass(grades);

return 0;
}


/*

void avgStuClassGrade(float* gradesF[SIZE_1][SIZE_2][SIZE_3]) {

float sum = 0;

float avg = 0;

int x, y, z;

for (x = 0; x < 11; x++) {

for(y = 0; y < 4; y++) {

for(z = 0; z < 5; z++) {

if (z == 4) {

sum = sum + *gradesF[x][y][z];

avg = sum / z;

printf("The average for student %d in subject %d is %f.\n", x, y, avg);

sum = 0;

}

else {

sum = sum + *gradesF[x][y][z];

}

}

}

}

}

*/
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Dec 13 2012 11:58am
code brackets man...
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll