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 7057611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:00:27+00:00 2026-05-28T04:00:27+00:00

I am trying to define a matrix like so I have a structure typedef

  • 0

I am trying to define a matrix like so
I have a structure

typedef struct _struct {
  int name;
  int data;
} myDataType;

Afterwards i am defining a matrix

int **myMatrix = calloc(size,sizeof(int*));
for()
   // allocate rows except last index
myMatrix[last_index_in_matrix] = calloc(1,sizeof(myDataType));

The problem is that I cannot access myMatrix[last_index].data it says, also tried with -> (i really don’t know when to use what)

request for member ‘data’ in something not a structure or union

What am i doing wrong ? Should i post actual code ? If this method is not possible can i get a different suggestion ?

UPDATE: I’ll say it again, the matrix is all int, i just want the last row to point to that structure, some of the comments have not taken this into consideration. That is way i declared it the way i did in my example.

  • 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-28T04:00:28+00:00Added an answer on May 28, 2026 at 4:00 am

    Let’s start simple:

    You want to create a matrix of myDataType. If you know the number of rows and columns at compile time, you can simply declare it as

    myDataType matrix[ROWS][COLS];
    

    If you need to allocate it dynamically, you would do something like this:

    myDataType **matrix;
    
    matrix = calloc(rows, sizeof *matrix);
    if (matrix)
    {
      size_t r;
      for (r = 0; r < rows; r++)
      {
        matrix[r] = calloc(cols, sizeof *matrix[r]);
      }
    }
    

    Either way, to access the struct members at the i‘th and j‘th elements, you’d write:

    matrix[i][j].name = ...;
    matrix[i][j].data = ...;
    

    Edit

    Ah, now I get it.

    Don’t do that.

    There’s no guarantee that pointers to struct types have the same size and representation as pointers to int. They do on most common architectures, but it’s not something you can rely on. If they don’t, then you’re going to have runtime problems.

    From a design point of view this just makes me itchy; if you need to associate that struct with a matrix, create a new aggregate type to explicitly do so:

    struct composite
    {
      int **matrix;
      struct myData data;
    };
    

    This will make life easier when you need to free up the matrix as well.

    FWIW, to do what you wanted, you’d need to engage in some casting gymnastics such as

    (struct myData *) myMatrix[last_index] = malloc(sizeof (struct myData));
    

    and

    ((struct myData *) myMatrix[last_index])->data = ...;
    

    but as I said above, if the pointer types are not compatible, the conversion could lead to runtime errors. Don’t do it. Bad juju.

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

Sidebar

Related Questions

I'm trying to create a matrix data structure in C. I have a struct
I am trying to define a WCF contract that returns an interface, something like
I'm trying to define base class, which contains typedef's only. template<typename T> class A
I'm trying to define a new type and have not had much luck finding
I am trying to create a Matrix class using c++. So far, I have
Basically I have been trying to forge an understanding of matrix maths over the
The objective here is to have a pointer that works like a 2D matrix.
Im trying to define a dataTemplate for a business object in my wpf application
I'm trying to define a task that emits (using echo) a message when a
I'm trying to define a table to store student grades for a online report

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.