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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:40:29+00:00 2026-05-27T10:40:29+00:00

I understand how to create a struct that can be used to access an

  • 0

I understand how to create a struct that can be used to access an array, for example:

#include <stdio.h>

typedef struct {
  int i;
  int j;
  int k;
} MyStruct;

int main()
{
  MyStruct *ms;
  int array[9];
  int i;

  for(i=0;i<9;++i) array[i] = i;

  ms = (MyStruct *)array;

  for(i=0;i<3;++i) printf("%d %d %d %d\n",i,ms[i].i,ms[i].j,ms[i].k);
  return 0;
}

which allows me to access array[0] as ms[1].i and array[1] as ms[1].j etc.. However, how could I do the same thing if I wanted to use an array in the struct that is known at runtime but not at compile time? Something like:

#include <stdio.h>

typedef struct {
  int *myArgs;
} MyStruct;

int main()
{
  MyStruct *ms;
  int array[9];
  int i;

  for(i=0;i<9;++i) array[i] = i;

  ms = (MyStruct *)array;

  for(i=0;i<3;++i) printf("%d %d %d %d\n",i,ms[i].myArgs[0],ms[i].myArgs[1],ms[i].myArgs[2]);
  return 0;
}

which in that example, I would know at runtime that myArgs is length 3. But it’s also possible myArgs is length 9, in which case ms should be length 1.

Edit:

I’m storing a solution set for a PDE solver that has a number of fixed-at-compile-time knowns and then an unknown number of extras. For example, my array will have X, Y, Z but then it might have 3 or 5 or 10 more things after that. So, I wanted to provide a structure that gives access to the fields for easy readability later, for example solution.x, solution.y, solution.z, solution.chemicalSpecies[0], solution.chemicalSpecies[1], etc.. The number of species is entirely unknown at compile time.

I’m using an outside library that stores the number of unknowns as an array, and I can get the array back in the form [k][j][i][numUnknowns] and it would be nice to give access like solution[k][j][i].something.

  • 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-27T10:40:29+00:00Added an answer on May 27, 2026 at 10:40 am

    No.

    In your first case, the size of your structure is the size of 3 int variables. When you cast the pointer of your array to the pointer of your structure, each 3 int elements will match with the one MyStruct element, what does the magic in your code (that is theoretically non-portable).

    In your second case, the size of the structure is the size of 1 pointer to int. You must be aware that pointers have some similarities with arrays regarding usage, but they are not the same thing. Pointers are just one variable with a single value that points to a memory address. The contents of that address can then be used as an array.

    In C, structs and arrays sizes are dealt at compile time, so a struct’s size cannot change at runtime, thus it will do no good to your purpuse to have a pointer inside your struct, since pointer sizes are also fixed.

    Maybe a good solution to you would be to use an abstract data type, like:

    typedef struct {
      int m; /* number of lines */
      int n; /* number of columns */
      int *data; /* pointer to the actual data */
    } Matrix;
    

    Then you could create ancillary functions for using the struct:

    void matrix_new(Matrix *m, int rows, int columns);
    int matrix_get(Matrix *m, int i, int j);
    void matrix_set(Matrix *m, int i, int j, int value);
    void matrix_free(Matix *m);
    

    If that is too clumsy, you could even use a mixed approach (not too “abstract” type):

    int matrix_get(Matrix *m, int i, int j)
    {
      return m->data[i * m->n + j];
    }
    
    int main()
    {
      Matrix m;
      int array[9];
      int i;
    
      for(i=0;i<9;++i) array[i] = i;
    
      m.m = m.n = 3;
      m.data = array;
    
      for(i=0;i<3;++i)
        printf("%d %d %d %d\n", i,
                                matrix_get(&m, i, 0),
                                matrix_get(&m, i, 1),
                                matrix_get(&m, i, 2));
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand that you can now create MVC-specific user controls, but will my existing
I understand that I can call ToString().IndexOf(...), but I don't want to create an
For reasons that only the developers can understand, Firefox will create and open .url
I (think I) understand that you can only retrieve the size of an array
I understand that we need to create MXML file to define a view. Suppose
I'm trying to understand if it's possible to create a set of variables that
I understand that Firefox addins can be created in Javascript and Chrome. How do
I'm having a brain cramp: struct MyStruct { int x; ... inline int getX1()
I want to create a function that can take different types of iterators which
When doing metaprogramming using C++ templates, is there a method that can be used,

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.