-6

Remember I am talking about multidimensional STRUCTURES and not multidimensional arrays, STRUCTURE VARIABLES, etc. If yes, then what is the syntax? The example will help you clear things out:

struct arr[3]{
    int a, float b,
    int c, float d,
    int e, float f;
}

I just used the syntax that are used for multidimensional arrays with some additional work (like semicolon after the last variable) which is not used as a syntax for multidimensional arrays. AND Don't think this as an actual syntax, here I am just trying to let you understand my question. A multidimensional structure which has two types of data in one row separated by columns. A one which can have different type of data in each rows.

enter image description here

MarianD
  • 13,096
  • 12
  • 42
  • 54
Darshan Gupta
  • 51
  • 1
  • 8
  • 6
    Sorry, I don't understand what you mean. – Marc Glisse Jan 03 '18 at 17:33
  • 1
    I might have called it a pretty good question if were able to understand what you want to achieve. How about create code with example use case of your idea? – R2RT Jan 03 '18 at 17:34
  • Is this a C or C++ question? In C you'd use an array of arrays. In C++ a `std::vector` with a `std::vector` in it. – tadman Jan 03 '18 at 17:34
  • I am sure that I want a multidimensional structure – Darshan Gupta Jan 03 '18 at 17:34
  • But we still don't know what a "multidimensional structure" is :-( – Marc Glisse Jan 03 '18 at 17:35
  • @tadman: as the title says C++, I removed that tag. – Jongware Jan 03 '18 at 17:35
  • This is C++. Or I am fine if this feature available in C – Darshan Gupta Jan 03 '18 at 17:35
  • That is not a valid C++ syntax. There are multidimensional arrays and containers of containers if you prefer. – Ron Jan 03 '18 at 17:35
  • 2
    @DarshanGupta - could you describe the semantics of a "multidimensional structure?" Since no such beast currently exists by the name, maybe if you tell us what it does, we could help you find a solution. – Robᵩ Jan 03 '18 at 17:35
  • One minute let me explain. – Darshan Gupta Jan 03 '18 at 17:35
  • "I am sure that I want a multidimensional structure " - we don't understand what you mean by "multidimensional structure". –  Jan 03 '18 at 17:35
  • 1
    Please have a look at this [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) list and a very well maintained [C++ reference](http://en.cppreference.com/w/). – Ron Jan 03 '18 at 17:36
  • If this question is "can I have a structure with a bunch of properties and then make an array of that so there's two levels to the structure" then the answer is yes. – tadman Jan 03 '18 at 17:36
  • Take an example of multidimensional array in which each row has elements of same datatype. Each column holds the data of same type. Now in the case of multidimensional structure I am asking a structure in which each row has elements of different datatype. However, each column will store same type of data – Darshan Gupta Jan 03 '18 at 17:38
  • Hope this helps – Darshan Gupta Jan 03 '18 at 17:38
  • 3
    Sounds like an array of struct... – Marc Glisse Jan 03 '18 at 17:39
  • *"I am asking a structure in which each row has elements of different datatype. However, each column will store same type of data"* Well, there are no columns in a class or at least its members aren't organized in that way. But you can declare multiple variables with the same type: `struct arr { int a, c, e; float b, d, f; };` – Bob__ Jan 03 '18 at 17:44
  • I believe, he wants a 2d array where the first dimension will be an `integer` and the second a `float`. Something like a `php` array where you don't need to declare the type of the variable, although in `cpp` you have to. – Segmentation Jan 03 '18 at 17:45
  • I don't understand much but I believe you are right @Seg – Darshan Gupta Jan 03 '18 at 17:49
  • I have pasted a link in my question open that image and it might help you guys – Darshan Gupta Jan 03 '18 at 17:50
  • You can make a struct that holds an array of ints and holds an array of chars. – Christian Gibbons Jan 03 '18 at 17:51
  • @DarshanGupta you want a two dimensional array that the first column will have variables of type `int` and the second of type `float`. For example: `array[0][0]` would have an `int` and `array[0][1]` would have a `float`. Am i right? – Segmentation Jan 03 '18 at 17:52
  • Yup but you should not store things like: houseno, roll number of the student, etc. informations together as they look odd, so here you must declare two-three variables @christian – Darshan Gupta Jan 03 '18 at 17:54
  • @Segmentation yes but in the form of two dimensional structure – Darshan Gupta Jan 03 '18 at 17:55
  • Use an array of union instead of struct, and be very disciplined when using this thingy. ;-) – alk Jan 03 '18 at 17:57
  • @alk union shares memory which I don't want. – Darshan Gupta Jan 03 '18 at 17:59
  • I do not understand what you mean by it looking odd to group information together. – Christian Gibbons Jan 03 '18 at 18:01
  • 1
    You should take a look at C++17 `std::variant`. It allows you have an array that the data type varies. – Guillaume Racicot Jan 03 '18 at 18:02
  • What would you name an array which stores info like houseno, barcode_no, employee number? – Darshan Gupta Jan 03 '18 at 18:03
  • Sorry guys I am running out of battery right now and my charger sucks, further it is night here so bye see you soon – Darshan Gupta Jan 03 '18 at 18:04
  • @DarshanGupta I would call it structure. – Segmentation Jan 03 '18 at 18:05
  • 2
    This definitely feels like an XY problem. The best I can guess is that what you really need is an array of structs, but unless you can give us more information about what it is you are actually trying to accomplish, the all we can do is guess at what you are trying to do. – Christian Gibbons Jan 03 '18 at 18:09

1 Answers1

4

An array of structs seems to satisfy your request. The

#include <iostream>
static struct { int a; float b; } arr[2] = {1, 1.5, 2, 2.5};

int main () {
    // Print the value of the first column 
    std::cout << "Column 'a': " <<
        arr[0].a << " " <<
        arr[1].a << "\n";

    // Print the value of the first row
    std::cout << "Row '0': " <<
        arr[0].a << " " <<
        arr[0].b << "\n";
}
Robᵩ
  • 163,533
  • 20
  • 239
  • 308