Your stumbling block is more about handling the struct as a parameter. A struct is simply a data type in C. It is passed as a parameter just as any other data type (int
, long
, char
, etc...) and follows the same rules.
The convenience of a typedef
allows you to specify the parameter as Pyramid var
rather than having to use the full struct Pyramid var
as you would without it. In fact, you don't even need the _Pyramid
name for the struct when using a typedef
, e.g.
typedef struct { /* you can simply typedef a struct */
double stone [N][N];
} pyramid_t;
Above, the typedef
simply aliases an unnamed struct containing an array of doubles to the pyramid_t
name, which you can use in declaring instances of the struct, e.g.
/* explicit way to initilize 2D array as 1st member of struct */
pyramid_t a = {{{10, 4, 2, 5, 1, 0, 0, 0},
{ 3, 9, 1, 2, 1, 0, 0, 0},
{-7, -5, 1, -2, -1, 0, 0, 0},
{-3, -5, 0, -1, 0, 0, 0, 0},
{-2, 1, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0}}};
Whether you include _Pyramid
or not is up to you. (without it, you simply lose the ability to refer to struct _Pyramid
as a declared type).
Your declaration of int data[N*N];
is a red-herring, a distraction of no consequence that isn't relevant to what you are attempting to do. You initialize an instance of your struct in main()
. You simply need to pass the struct (or better, a pointer to your struct) to your showme
[1] function in order to have the values available for printing there.
For example, including the typedef
to specify you are passing the struct as a parameter (where the function receives a copy of the struct) you output the values using the dot
'.'
operator as you have in your function, e.g.
/* function type is void if it returns no value */
void showme (pyramid_t p)
{
int i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++)
printf("%3g ", p.stone[i][j]);
putchar('\n'); /* use putchar() to output single char */
}
}
You would then call your function in main()
as:
showme (a); /* passing a copy of the struct to showme() */
(note: the function receives a copy of the struct -- so try changing some of the array values and then print the contents again back in main()
... any changes made are lost)
A more efficient way of passing the struct would be to pass a pointer to the struct, so that all that is being passed is the address of where the structure is stored in memory. Not only does that avoid making a copy of the struct, but by passing the address, any changes you make to the structure values are then preserved (because you are directly changing the values of the original struct in memory -- not simply making changes to a copy). When you pass a pointer to a struct (or anytime you are accessing values of a struct through a pointer), you use the arrow operator
(->
) to access the struct members. For example, you could have just as easily have declared showme()
to take a pointer to pyramid_t
instead of a struct, e.g.
/* function type is void if it returns no value */
void showme (pyramid_t *p)
{
int i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++)
printf("%3g ", p->stone[i][j]);
putchar('\n'); /* use putchar() to output single char */
}
}
To pass a pointer to your struct in main()
, you simply use the unary '&'
operator (address of) to pass the address of a
, e.g.
showme (&a); /* pass pointer to prevent duplicating struct */
Now if you made changes to the array values in the function, the changes would be visible back in main()
because you changed the values where they were stored in memory rather than operating on a copy passed to the function.
Look at both examples and understand the differences both in declaring the showme()
function and how it is called in main()
. The first example passing a copy of the struct and using the dot-operator to access the member would be:
#include <stdio.h>
#include <stdlib.h>
#define N 8 /* good job - if you need a constant, #define one (or more) */
typedef struct { /* you can simply typedef a struct */
double stone [N][N];
} pyramid_t;
/* function type is void if it returns no value */
void showme (pyramid_t p)
{
int i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++)
printf("%3g ", p.stone[i][j]);
putchar('\n'); /* use putchar() to output single char */
}
}
int main (void) { /* unless using argc, argv, use void */
/* explicit way to initilize 2D array as 1st member of struct */
pyramid_t a = {{{10, 4, 2, 5, 1, 0, 0, 0},
{ 3, 9, 1, 2, 1, 0, 0, 0},
{-7, -5, 1, -2, -1, 0, 0, 0},
{-3, -5, 0, -1, 0, 0, 0, 0},
{-2, 1, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0}}};
showme (a); /* passing a copy of the struct to showme() */
return 0; /* main() is type 'int' and returns a value */
}
To pass a pointer containing the address of the original struct, you would simply change the declaration of showme()
and the operator used to access the struct members:
/* function type is void if it returns no value */
void showme (pyramid_t *p)
{
...
printf("%3g ", p->stone[i][j]);
...
}
And, as explained above, you would simply call showme()
with the address of a
, e.g.
showme (&a); /* pass pointer to prevent duplicating struct */
In either case the output would be the same, e.g.
Example Use/Output
> bin\pyramid-struct.exe
10 4 2 5 1 0 0 0
3 9 1 2 1 0 0 0
-7 -5 1 -2 -1 0 0 0
-3 -5 0 -1 0 0 0 0
-2 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Look things over and understand the differences, and let me know if you have further questions.
footnotes:
[1] While not an error, C style generally avoids the use of MixedCase
or camelCase
variables and the use of all-uppercase names (reserving all uppercase names for constants or macros), instead using variables names of all lowercase. While this is an issue of style, and completely up to you, it can lead to the wrong first-impressions in some settings.