Say I have a multidimensional array, I populated it with for's. Now if I just want the colummn and depth parts to be passed to the function is that possible?
Say I have a multidimensional array, I populated it with for's. Now if I just want the colummn and depth parts to be passed to the function is that possible?
You can pass parts of an array to a function - what you normally would do is either pass one element/value of the array to the function, or pass one pointer to say, the top 100 elements of the function.
You're only passing a pointer, in any case. All the data is not copied over. C assumes that you will be modifying the array's values.
See I tried using pointers but it gives me an error when compiling
> See I tried using pointers but it gives me an error when compiling
See?
We can't see any of your code, so we can't say what you got wrong, or how to fix it.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I'm not writing any code it's more of a question for learning purposes.
So if I do say int array[][4][5];
and I want to just take 4 and 5 I would make a prototype
int Ave(array[j][k]);
then in int main declare j and k as 4 and 5?
See I tried using pointers but it gives me an error when compiling... ?I'm not writing any code it's more of a question for learning purposes.
...I was writing code before, but went another route. I'm just trying to figure out how I would go about it correctly via pseudo example
Ok so for example.
Code:#include <stdio.h> float ave(float stu[][3][4]); float sAve(float stu[j][k]); int main() { int i, j, k; float stu[2][3][4]; for(i=0; i<2; i++){ for(j=0;j<3;j++){ for(k=0; k<4; k++){ if(k==0){ printf("Enter in the grade for student %d in subject %d", j+1, k+1); scanf("%d", &stu[i][j][k]); }else scanf("%d", &stu[i][j][k]); } } } printf("All Averages: %f", ave(stu)); } float ave(float stu[][3][4]) { float sum=0; int i, j, k; for(i=0; i<2; i++){ for(j=0;j<3;j++){ for(k=0; k<4; k++){ sum+=stu[i][j][k]; } } } return (sum/24); } float sAve(float stu[j][k]) { float sum=0; int i, j; for(i=0; i<2; i++){ for(j=0;j<3;j++){ sum+=stu[i][j]; } } return (sum/12); }
Last edited by Sorinx; 11-15-2012 at 04:09 PM.
J and K undeclared not in a function. How do I point correctly?
Well the way I like to visualize a 3-d array is like a cube. So, think of it like a matrix on one side that represents one students grades, and the depth vector represents all of your students. This way you can slice the cube, in a manner of speaking, and get the grades of each student.
A sane way to build such a data structure is:
This way isn't insanely popular, but you have to remember what all the dimensions mean. So to pass in one student, you need a matrix of floats; to pass in the whole class you need a cube, etc. To actually print, it might be easier to code a function that prints one students courses, and then loops over the whole class, calling the function.Code:typedef float grades[2]; typedef grades courses[3]; typedef courses students[4]; students class_room; // definition
Last edited by whiteflags; 11-15-2012 at 04:24 PM.
Yeah I know how a 3 dimensional array is however not supposed to do it that way
It is the same thing. You just have an easier time remembering what each dimension means.
Plus, I didn't quite get what you were asking. The code you were complaining about here was in a separate post:
This is about the most incompetent way to ask this question.
The errors are (as indicated):Code:#include <stdio.h> float ave(float stu[][3][4]); float sAve(float stu[j][k]); ... float sAve(float stu[j][k]) { float sum=0; int i, j; for(i=0; i<2; i++) { for(j=0; j<3; j++) { sum+=stu[i][j]; } } return (sum/12); }
You get these errors because you are specifying the dimensions of a multi-dimensional array. This is restricted to constant integral expressions (i.e. numbers), a variable name here will not pass the compiler.Code:-------------- Build: Debug in foo --------------- mingw32-gcc.exe -Wall -ansi -g -pedantic -w -Wextra -Wall -g -std=gnu99 -D_UNICODE -DUNICODE -c C:\Users\Josh2\Documents\foo\main.c -o obj\Debug\main.o C:\Users\Josh2\Documents\foo\main.c:3: error: 'j' undeclared here (not in a function) C:\Users\Josh2\Documents\foo\main.c:3: error: 'k' undeclared here (not in a function) C:\Users\Josh2\Documents\foo\main.c:48: error: 'j' undeclared here (not in a function) C:\Users\Josh2\Documents\foo\main.c:48: error: 'k' undeclared here (not in a function) Process terminated with status 1 (0 minutes, 0 seconds) 4 errors, 0 warnings
Next time, I expect you to provide the actual error text, or I will just ignore you.
So how would I do it correctly?
Last edited by Sorinx; 11-15-2012 at 05:08 PM.
Here is an example of sending a pointer to a 2D array to a subroutine
Code:#include <stdio.h> #include <stdlib.h> #include <stddef.h> #include <string.h> #define MAX_S 10 void print_s(char (*arr2D_ptr)[][MAX_S]); int main (void) { char arr2D[][MAX_S] = { "string 1", "string 2" }; print_s(&arr2D); return EXIT_SUCCESS; } void print_s(char (*arr2D_ptr)[][MAX_S]) { puts((*arr2D_ptr)[0]); puts((*arr2D_ptr)[1]); }
Fact - Beethoven wrote his first symphony in C
That doesn't help me much because my issue isn't passing the array, it's when I have a 3 dimensional array I only want to pass the 2nd and 3rd dimension to the function