Question

Note: C is Microsoft C Compiler.

I'm having trouble with the following code.

*Roomsize = (int*)calloc(sizeof(int),sched->numberOfRooms);

roomIndex = 0;
for(roomIndex=0; roomIndex< sched->numberOfRooms; roomIndex++)
{
    fscanf(inputFile,"%d",&lineInput);
    numberOfLinesRead++;
    *Roomsize[roomIndex] = lineInput;
}

This is in a separate C file. I wasn't having this problem until I decided to separate things out to make them more maintainable and I think I'm just getting a little mixed up with pointers.

The calloc works fine.

On the first iteration of the loop, element zero of roomIndex gets set properly.

However the second element (element 1) in the loop, always results in an access violation at runtime.

I run into this problem later in my code too with a 2d array, but I'm figuring it's the exact same problem and this is just the most simple case.

Can anyone help me understand why it seems impossible to set anything but the first element here?

Was it helpful?

Solution

*Roomsize[roomIndex] is the same as *(Roomsize[roomIndex]). You want to say (*Roomsize)[roomIndex].

(I'm assuming that Roomsize is actually a int**. If that's not correct, then the problem may lie elsewhere.)

OTHER TIPS

Your first line, when you allocate Roomsize, looks wrong. If I am correct in assuming Roomsize is an int *, you should just say Roomsize = (int *) calloc... As @Daniel posted, your assignment should also be changed to get rid of the asterisk.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top