Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4080868
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:59:08+00:00 2026-05-20T17:59:08+00:00

For an assignment at school, we have to use structs to make matrices that

  • 0

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));

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T17:59:09+00:00Added an answer on May 20, 2026 at 5:59 pm
    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.