Question

For an assignment at school, we have to use structs to make matrices that can store a infinite amount of points for an infinite amount of matrices. (theoretical infinite)

For the assignment I decided to use calloc and realloc. How the sizes for the matrix go is: It doubles in size every time its limit is hit for its points (so it starts at 1, then goes to 2, then 4 and so on). It also doubles in size every time a matrix is added as well.

This is where my issue lies. After the initial matrix is added, and it goes to add the second matrix name and points, it gives me the following:

B???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

B is the portion of it that I want (as I use strcmp later on), but the ? marks are not supposed to be there. (obviously)

I am not sure why it is exactly doing this. Since the code is modular it isn't very easy to get portions of it to show exactly how it is going about this.

Note: I can access the points of the matrix via its method of: MyMatrix[1].points[0].x_cord; (this is just an example)

Sample code that produces problem:

STRUCTS:

 struct matrice {
    char M_name[256];
    int num_points[128];
    int set_points[128];
    int hasValues[1];
    struct matrice_points * points;
} * MyMatrix;
struct matrice_points {
    int set[1];
    double cord_x;
    double cord_y;
};

Setup Matrix Function:

void setupMatrix(){

    MyMatrix = calloc(1, sizeof(*MyMatrix));
    numMatrix = 1;

}

Grow Matrix Function:

void growMatrix(){

    MyMatrix = realloc(MyMatrix, numMatrix * 2 * sizeof(*MyMatrix));
    numMatrix = numMatrix * 2;

}

Add Matrix Function which outputs this problem after growing the matrix once.

void addMatrix(char Name, int Location){

    int exists = 0;
    int existsLocation = 0;
    for (int i = 0; i < numMatrix; i++){
        if (strcmp(MyMatrix[i].M_name, &Name) == 0){
            exists = 1;
            existsLocation = i;
        }
    }

    *MyMatrix[Location].M_name = Name;
    printf("Stored Name: %s\n", MyMatrix[Location].M_name);
    *MyMatrix[Location].num_points = 1;
    *MyMatrix[Location].set_points = 0;
    *MyMatrix[Location].hasValues = 1;
    MyMatrix[Location].points = calloc(1, sizeof(*MyMatrix[Location].points));

}
Was it helpful?

Solution

void addMatrix(char Name, int Location)

char Name represents a single char, i.e. a integer-type quantity. char is just a number, it's not a string at all.

When you do this:

strcmp(..., &Name)

you're assuming that the location where that one character is stored represents a valid C string. This is wrong, there is no reason why this should be the case. If you want to pass a C string to this function, you will need to declare it like this:

void addMatrix(char *Name, int Location)

Then you need to copy that C string into the appropriate place in your matrix structure. It should look like:

strncpy(... .M_name, Name, max_number_of_chars_you_can_store_in_M_Name);

Also these field definitions are strange in your struct:

int num_points[128];
int set_points[128];
int hasValues[1];

This means that your struct will contain an array of 128 ints called num_points, another array of 128 ints calls set_points, and an array of one int (strange) called hasValues. If you only need to store the count of total points and set points, and a flag indicating whether values are stored, the definition should be:

int num_points;
int set_points;
int hasValues;

and correct the assignments in your addMatrix function.

If you do need those arrays, then your assignments as they are are wrong also.

Please turn on all warnings in your compiler.

OTHER TIPS

Try adding '\0' to the end of your data.

*MyMatrix[Location].M_name = Name;

You're copying a single character here, not a string. If you want a string, Name should be defined as char *, and you should be using strcpy.

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