arrays within a struct

I'm writing a program for class and using an array contained within a struct. The struct is below:

1
2
3
4
5
struct Project_List
{
    Project_Data project_All[LIST_SIZE];   //array
    int num_Projects;   //number of projects listed in the array
};


This way I can access the array and the number of projects in the array with a parameter call to the struct. For example:

 
void Display_All( /* inout */ Project_List &);


Here's my question: If I wanted the array to be called as a constant, thereby preventing change, could I omit the ampersand (&) and have it still work properly? I know that this would also prevent change to the <int> variable, but that doesn't matter because I just need to be able to read it, unless I'm changing the number of filled elements in the array.

As a side note, we haven't even approached vectors, yet, so, please don't mention them. Apparently, it's easier, but it's kind of a joke in class that you can't bring up arrays in a forum without someone bringing up vectors.

Thanks in advance for any help.



ampersand in this context is passing by reference (as opposed to passing by making a copy).
If you want "read-only" feature, you use the const keyword. Often you see the signature of passing around a const reference, const ProjectList& p for example -- a reference used, and p cant be modified.

If you want to do specific things with how your array can be accessed, just make it private and create appropriate methods that return const refs or whatever.

btw, what's the difference between LIST_SIZE and num_Projects?

Stylistic note: write stuff in CamelCase or snake_case (where everything is lowercase or everything is uppercase), but please don't mix capitalization with underscores. Class/struct names, class methods I usually see as CamelCase; locals and private vars as lowercase snake case; enums as uppercase snake case.
Last edited on
1) LIST_SIZE is a constant that determines the size of the array and num_Projects represents how many items are actually contained within the array rather than how large the array is.

2) Thanks for the help.
closed account (E0p9LyTq)
As a side note, we haven't even approached vectors, yet, so, please don't mention them.

OK, I won't mention them. :)

How about using std::array? They store the number of elements as part of the container.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <array>

template <typename T, std::size_t SIZE>
void dispArray(const std::array<T, SIZE>& input);

int main()
{
   std::array<int, 7> arr1 = { 1, 2, 3, 4, 5, 6, 7 };
   std::array<short, 6> arr2 = { 2, 4, 6, 8, 10, 12 };
   std::array<double, 9> arr3 = { 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9 };

   dispArray(arr1);
   dispArray(arr2);
   dispArray(arr3);
}

template <typename T, std::size_t SIZE>
void dispArray(const std::array<T, SIZE>& input)
{
   for (size_t loop = 0; loop < input.size(); loop++)
   {
      std::cout << input[loop] << ' ';
   }

   std::cout << '\n';
}

1 2 3 4 5 6 7
2 4 6 8 10 12
1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9
Last edited on
@FurryGuy We haven't done that, yet either, so, it took me a second to figure out what was going on there. If I can't fix the below problem some other way, I'll try that.

New problem: I put in some stub functions to test some of the program and I came up with an error that I can't quite figure out. Here's the relevant code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const int LIST_SIZE = 100;

struct Date
{
    int month = 0;
    int day = 0;
    int year = 0;
};

struct Project_Data
{
    std::string name = " ";
    std::string description = " ";
    Date start_date;
    Date end_date;
    float time_Require;
    float time_Expend;
    std::string completion;
};

struct Project_List
{
    Project_Data all_Projects[LIST_SIZE];   //array
    int num_Projects;   //number of projects listed in the array
};

//Displays all of the elements in the array
void Display_All(const Project_List &);

int main(){
    Project_List = project;

    Display_All(project);

    return 0;
}

void Display_All(const Project_List & project){

    cout << "Display_All used." << endl;
}


The error message is:
1
2
Line 31    error: expected unqualified-id before '=' token
Line 34    error: 'project' was not declared in this scope


Any help would be greatly appreciated.
closed account (E0p9LyTq)
Line 31: Project_List project; No assignment (=) needed.

We haven't done that, yet either

My earlier post was somewhat snarky.

Yet mashing the code for it helped me to go hunt and learn how to pass the STL std::array container into a function.

So thank you for the "don't talk about vectors" comment. I learned something I didn't know before. :)

Your structures are an attempt to encapsulate a regular array and its size. Same basic idea of std::array.
Last edited on
@FurryGuy 1) Thanks. I don't know I didn't notice that.

2) It's hard to convey snarkiness in a text, so your smile made me think that you realized that the part about vectors being a joke in my class was, in fact, a joke. In other words, there was no apology necessary. However, thanks for the apology and I'm glad you learned something.
closed account (E0p9LyTq)
One of the best ways to learn, keep pushing the boundaries of what you know.

I banged my head a lot with the example I gave earlier. Lots of learning resulted. :)
Topic archived. No new replies allowed.