My answer is not an actual answer but an extension to Jonathan Leffler's answer:
I have read questions like yours very often. In most cases there is a misconception caused by comparing C and C++ with Java or C#:
In Java variables of primitive data types (like int
) hold values while values of class
data types hold references to objects.
"Reference" means that in the following program:
ScheduleData a = b;
a.initials = "ab";
b.initials = "xy";
... the same variable is accessed by the statements a.initials
and b.initials
.
However, C and C++ work differently:
You decide if a variable is a "pointer" (which more or less is a "reference") or if it is a value by using or not using an asterisk (*
) between the data type and the variable name. Example:
ScheduleData a, *b;
int c, *d, *e;
d = e;
In this example the variables a
and c
hold values, the variables b
, d
and e
hold references.
And just like in the Java example modifying the value d
points to will have the same effect as modifying the value e
points to because d
and e
are references to the same integer.
The first solution in Jonathan Leffler's answer proposes using values of the data type ScheduleData
instead of using references.
The second solution shows you how references are used. Avoiding references typically makes programming easier but references may be required if you want to do the equivalent of the following Java program:
ScheduleData x = node[4][2];
ScheduleData y = node[4][2];
node[4][2] = new ScheduleData();
/* x and y will be a reference to the same data structure but node[4][2]
* is a reference to a different data structure */
By the way
The program you have written is a C++ program, not a C program.
Unlke C++, C will not automatically recognize structure names as data types. Instead, you either have to define the data type using typedef
:
typedef struct _s_ScheduleData {
...
} ScheduleData;
... or you have to use the struct
keyword as part of the data type:
struct ScheduleData node[8][6];