I wrote this code as an experiment, before I tried same thing with my project. However, can't get it to compile. See my comments inside the code:
Code:
#define MAX_T 42
#define MAX_CHARS 42
#define SOME_INT 42
struct t_container {
struct t_named *ptrFirst;
};
struct t_named {
char name[MAX_CHARS];
int x;
};
int function(int index, struct t_container *ptrContainer);
int main(){
int i;
struct t_container *new;
new = malloc( sizeof *new );
new->ptrFirst = malloc( MAX_T * sizeof *new->ptrFirst );
for(i=0; i<MAX_T; i++) function(i, new);
return 1;
}
int function(int index, struct t_container *ptrContainer)
{
ptrContainer->ptrFirst[index].name = "Arbitrary string./n";
// error: incompatible types in assignment
ptrContainer->ptrFirst[index].x = SOME_INT;
return 1;
}
Read earlier posts about pointer arithmetic, and about the difference between "->" and ".", but I'm still a bit muddled. Why does ptrContainer->ptrFirst[index].x work and ptrContainer->ptrFirst[index]->x cause an "invalid type" error?