0

Basiclly, I have a struct defined this way:

struct D_Array
{
    int Capacity;
    int Cur_Size;
    struct Student* List;
};

Now, after creating the struct;

struct D_Array* T_Array;
if (T_Array = malloc(sizeof(struct D_Array)) == NULL)
{
    printf("There has been a problem with memory allocation. Exiting the program.");
    exit (21);
}

I have a problem creating the students list inside this struct. I need the list to be an array of pointers, since I should make some kind of ADT program, I've tried to make it something like this:

T_Array->Capacity = 10;
T_Array->Cur_Size = 0;
T_Array->List[10]= (struct Student*) malloc(sizeof (struct Student*));

It doesn't matter how I tried to change it, I'm getting the same error: incompatible types when assigning to type 'struct Student' from type 'struct Student *'|

I've trying to create an array that I will be able to do something like;

T_Array->List[1]=Student_Pointer;

Thanks!

Voxito
  • 33
  • 8

2 Answers2

2

Change the type of the array to struct Student** that will be array of pointers :

struct student * --> a pointer to student. struct student ** --> a pointer to a pointer to student (what you need).

struct D_Array
{
    int Capacity;
    int Cur_Size;
    struct Student** List;
}

and the allocation:

T_Array->List = malloc (sizeof (struct Student*) *  lengthofarray);

and then:

T_Array->List[1]=Student_Pointer;
alk
  • 69,737
  • 10
  • 105
  • 255
Dr.Haimovitz
  • 1,568
  • 12
  • 16
  • 3
    Don't cast the result of `malloc`. In `sizeof (struct student*)x lengthofarray` `x` should be `*`. – n. m. could be an AI Jun 04 '16 at 10:43
  • You have to cast if you want it to compile, what do you mean? – Dr.Haimovitz Jun 04 '16 at 10:45
  • 3
    [Don't cast the result of malloc](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). If you think you need it, you are either using a compiler for a wrong language, or forgetting to `#include` required headers. – n. m. could be an AI Jun 04 '16 at 10:49
  • Thanks, it did solve the problem. I guess the main problem was how I've defined D_Array in the first place, and yes, it also works without casting. – Voxito Jun 04 '16 at 10:49
  • You are wrong, malloc returns void* wich is obviously not student* so you need a cast, maybe there are compiler who will do an implicit casting for you... – Dr.Haimovitz Jun 04 '16 at 10:50
  • 2
    @Dr.Haimovitz void would automatically convert to the the datatype of the variable that it's getting assigned to... Then why take the pain of casting once again when it's done automatically? – Cherubim Jun 04 '16 at 13:38
  • begginers in c needs to know what they are doing, if someone started learning c he will use things that are not must just so he will know what happens in the background, for him an explicit writing will be preferred. – Dr.Haimovitz Jun 04 '16 at 13:41
  • 1
    "*malloc returns void\* wich is obviously not student\* so you need a cast*" not needed, nor recommended in any way in C, C does this implicitly from and to void-pointers. This is different in C++. – alk Jun 04 '16 at 13:42
  • in some compilers the default return type of a function is int, it is still a good idea to explicity write a return type , again for best practicing the language. – Dr.Haimovitz Jun 04 '16 at 13:47
  • someone who is learning c will never remember that function like malloc returns a void* if they wont be casting the return value, and then they will not understand how malloc "knows" their user defined type pointer ( a real question i was asked). – Dr.Haimovitz Jun 04 '16 at 13:50
  • 1
    If you do not include the prototype for `malloc()` on 64bit system its return type will default to `int`, yes. The compiler would detect this and error. If you would have applied an explicit conversion via a cast to `void*` the compiler just warns (or do log nothing all), it definitely won't error. Still `malloc()` would return 32 bits only (what the width of an `int`), and miss the upper 32bits for the `void*` it should return with or without cast. Conclusion: The cast hid the error, the program will fail. This is not good. Do not cast where it isn't required (for exp. when call `accept()`. – alk Jun 04 '16 at 13:58
  • Although i still thinks it is not a good thing to do while learning the language, i have edited the code. – Dr.Haimovitz Jun 04 '16 at 14:13
  • As far as learning is concerned i agree with you @Dr.Haimovitz :) – Cherubim Jun 04 '16 at 15:07
0

struct Student* List; List is a pointer of Student When you do List[10] what youre doing is basicaly *(List+10) that means get Student from the 11th position

If you want an array of pointers you need to do something like

struct Student **List;
and then
List = (Student **)malloc(10*sizeof(Student **))
List would then have memory allocated for 10 pointers of student wich would be size of int for each in the end