C - re-reset

I have a pointer problem, but I can't figure out what the problem is with this. The project is supposed to read the numbers from the file (works correctly) and then add the information to the dynamically allocated array.

This is a piece of code to read from a file and add to an array: buffer [0] is a special character that marks the next entries for an array of streets.

I've been going through the code and changing things for about a week and still can't figure out where the problem is. Note that the first slot works fine, after I re-output the second time, it throws me an exception from within "AddStreet" telling me the location of the violation record. I'm sure this is really stupid that I missed or didn't understand, so any help would be appreciated.

Input example: # 20 20 30 20 # 40 50 40 60

typedef struct street
{
    int start[2];
    int end[2];
}STREET;

void main()
{
int slength=0;
STREET *streets = NULL;
...
ReadFromFile(&streets, people, buildings);
...
}

void ReadFromFile(STREET **pstreets, PERSON *people, BUILDING *buildings)
{
int slength = 0;
...
if(buffer[0] == stre)
    {
        slength += 1;
        *pstreets = (STREET*) realloc(*pstreets, (slength)*sizeof(STREET));
        fscanf(pf, "%d %d %d %d", &a,&b,&c,&d);
        *pstreets = AddStreet(&(*pstreets), slength-1,a,b,c,d);
    }
...
}

STREET* AddStreet(STREET **streets, int length, int bx, int by, int ex, int ey)
{
    if(ValidStreet(bx, by, ex, ey))
    {
        streets[length]->start[0] = bx;
        streets[length]->start[1] = by;
        streets[length]->end[0] = ex;
        streets[length]->end[1] = ey;
    }
    else
    {
        streets[length]->start[0] = ex;
        streets[length]->start[1] = ey;
        streets[length]->end[0] = bx;
        streets[length]->end[1] = by;
    }

return *streets;
}

      

+3


source to share


1 answer


Your dereference logic for AddStreet is wrong. You are playing length

from pointer to pointer base. You want to dereference the pointer it points to in order to get the actual street object.

If streets

- STREET**

, then this:

streets[length]->start[0]

      

says, "Get me a pointer in an array of pointers starting streets

at an offset length

, and then look up that pointer." But your array is not an array of pointers. In fact, there is only one pointer (and you pass it by the address).

This will do what you want:

STREET* AddStreet(STREET **streets, int length, int bx, int by, int ex, int ey)
{
    if(ValidStreet(bx, by, ex, ey))
    {
        (*streets)[length].start[0] = bx;
        (*streets)[length].start[1] = by;
        (*streets)[length].end[0] = ex;
        (*streets)[length].end[1] = ey;
    }
    else
    {
        (*streets)[length].start[0] = ex;
        (*streets)[length].start[1] = ey;
        (*streets)[length].end[0] = bx;
        (*streets)[length].end[1] = by;
    }

    return *streets;
}

      




To be honest, you shouldn't pass pointer to pointer in the first place.

void AddStreet(STREET* street, int bx, int by, int ex, int ey)
{
    if(ValidStreet(bx, by, ex, ey))
    {
        street->start[0] = bx;
        street->start[1] = by;
        street->end[0] = ex;
        street->end[1] = ey;
    }
    else
    {
        street->start[0] = ex;
        street->start[1] = ey;
        street->end[0] = bx;
        street->end[1] = by;
    }
}

      

called like this:

    fscanf(pf, "%d %d %d %d", &a,&b,&c,&d);
    AddStreet(*pstreets + (slength-1), a,b,c,d);

      

will work and is significantly less error prone.

+3


source







All Articles