3

I've been trying to figure out how to add an array into a struct... a struct of ints for example would look like this:

struct test{
    int a;
    int b;
    int c;
} test = {0,1,2};

but if I want to have an array for example:

struct test{
    int a;
    int b;
    int c;
    int deck[52];
} test;

is this doable? the initialization of the deck (of cards) happens in a different function. when I do it like this, I don't get an error in the struct but I get it when I try to use it... for example if I do this test.deck[i] = 1; it gives me this error:

Error C2143 Syntax Error missing ';' before '.'

if I were to use a , I could write test.a = 1;

Could anyone write a simple struct where a variable in it is an array and then just use it for a simple command?

6
  • Please show a complete example of the failing code. Commented Mar 20, 2012 at 23:10
  • 5
    Why are you giving your variable the same identifier as it's type? Commented Mar 20, 2012 at 23:11
  • 1
    Yes, that is completely doable. I'm not sure why it's causing problems for you. Perhaps you can post more code. Commented Mar 20, 2012 at 23:11
  • The big problem I am having with posting the code is that for this problem to make sense I'd have to put a whole lot of code up here... that's why I asked if someone can just write an extremely basic example of initialization and usage, so I can compare Commented Mar 20, 2012 at 23:13
  • 1
    It works fine for me too, post a small example where the code fails and also try changing the name of your variable. Commented Mar 20, 2012 at 23:15

2 Answers 2

6

If this is C++, no C, drop the test after the struct definition.

The following code works perfectly.

#include <iostream>

using namespace std;

struct Test {
  int a;
  int b;
  int c;
  int deck[52];
};

int main (int argc, char* argv[])
{
    Test t;
    t.deck[1] = 1;
    cout << "t.deck[1]: "<< t.deck[1] << endl;
    exit(0);
}

The problem: In C, you put the test after the definition to create a variable named test. So in C, test is not a type, it is a global variable, the way you wrote that.

This compiles:

#include <iostream>

using namespace std;

struct Test {
  int a;
  int b;
  int c;
  int deck[52];
} test;

int main (int argc, char* argv[])
{
    test.deck[1] = 1;
    cout << "test.deck[1]: "<< test.deck[1] << endl;
    exit(0);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Your second example is exactly what the OP has in the question, isn't it?
The OP didn't post the code in which he uses the struct. And that, I think, may be wrong. Has he declared i before trying to index the vector? Well, he asked for an example of a code that would compile, I gave it to him.
3

The error:

Error C2143 Syntax Error missing ';' before '.'

is due to test being a type name. You need to define an instance:

int main() {
   test mytest;
   mytest.deck[1] = 1;
   return 0;
}

1 Comment

He did create an instance. It just happened to have the same name as the type, which was silly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.