First, I second laserlight's advice.
Originally Posted by
stefanturk
Hey guys, small issue that I'm sure has a quick fix, but I'm stumped. I'm getting this error:
vector.c:20:13: warning: assignment from incompatible pointer type [enabled by default]
vector.c:24:12: warning: assignment from incompatible pointer type [enabled by default]
I really appreciate that you've copied the original errors/warnings but it would be even better if you mark the corresponding lines in your code. That will make it easier for us to locate the lines because since you have only posted an extract of your program the line numbers from the compiler messages and the line numbers in the posted code don't match.
Assuming that you call your function like
Code:
initialize(&crns, &subjects, &courses, &size);
i.e. "subjects" and "courses" are two-dimensional arrays, "crns" is an one-dimensional array, and "size" is an int, I see 4 problematic lines in your code, I guess the compiler warnings are about these two lines:
Code:
**subjects = (char **)malloc(100*sizeof(char*));
**courses = (char **)malloc(100*sizeof(char*));
You have one indirection too much on the left side. "**subjects" is of type "char *" but your malloc returns a "char **".
You need to write these lines as
Code:
*subjects = malloc(100*sizeof(char*));
*courses = malloc(100*sizeof(char*));
or even better
Code:
*subjects = malloc(100*sizeof(**subjects));
*courses = malloc(100*sizeof(**courses));
because that's the idiomatic way to allocate memory for an dynamic array:
Code:
ptr = malloc(size * sizeof(*ptr));
In your case "ptr" is "*subjects" thus "*ptr" is "*(*subjects)" which is the same as "**subjects".
I also think that the following two lines are wrong:
Code:
*(subjects)[i] = (char*)malloc(4*sizeof(char));
*(courses)[i] = (char*)malloc(6*sizeof(char));
Your parentheses aren't correct on the left side. As you have written them the compiler interprets them as
Code:
*(subjects[i])
*(courses[i])
because array subscripting [] has higher precedence than indirection *.
You have to put the asterix inside the parentheses because you first have to dereference "subjects" and "courses" to get a pointer to pointer (i.e. a two-dimensional array):
Code:
(*subjects)[i]
(*courses)[i]
Bye, Andreas