0

I was just wondering. Say you will need to store these values:

Hours Minutes

inside an array, is the following implementation logically possible?

struct node
{
int hour;
int minutes;
};


int main()
{
int numOfLanding, minGap, hour, minutes;
cin>>numOfLanding;
cin>>minGap;
cout<<endl;

struct node *arr[numOfLanding];

for (int i=0; i<numOfLanding; i++)
{
    cin>>hour;
    cin>>minutes;
    arr[i]->hour=hour;
    arr[i]->minutes=minutes;
}

I am still trying very hard to understand struct node logic. Any help would be much appreciated!

3
  • 2
    It looks like you may be following a C guide or tutorial. The C++ way would be to use std::vector<node> arr;. Commented Apr 30, 2018 at 17:14
  • 1
    You are declaring an array of pointers, not an array of nodes, which is one problem. Another is that arrays are expected to have a constant size, which doesn't work when it is received from user input. Skip a few chapters ahead in your C++ tutorial to get to the container classes, they can be used to create variable-sized "arrays". Commented Apr 30, 2018 at 17:14
  • You probably want to use std::vector<node>. Commented Apr 30, 2018 at 17:14

2 Answers 2

3

What you are asking is: is it possible to make an array of a size that is not known at compile time, but only at runtime? Yes, you can, but the way you have chosen is not standard C++: declaring an array like

struct node *arr[numOfLanding];

means using a "variable-length array", which is not (and has never been) part of the C++ standard. It was part of C, however, in C99, but then the committee decided to make it optional in C11. It is anyway possible to find a C++ compiler that supports this feature as an extension: gcc, for example. But if you use it, keep in mind that your code is not portable.

The standard way of doing it in C++ is to use new[]:

node* arr = new node[numOfLanding];

(note that using the keyword struct every time is what you would do in C; in C++ it is not required)

At this point, you access each element using the ., not the ->:

arr[i].hour=hour;
arr[i].minutes=minutes;

After you are finished using the array you have to delete it, by using:

delete[] arr;

Anyway, this style is old, and nowadays considered bad. The preferred approach is to use a container that automatically deals with the size for you, and that will manage memory so that you don't need to worry about new[] and delete[]. The best container for this is the std::vector. To use it, first you have to #include <vector>, and then you can use it like this:

std::vector<node> arr(numOfLanding);

for (auto& curr_node : arr) {
    cin>>hour;
    cin>>minutes;
    curr_node.hour=hour;
    curr_node.minutes=minutes;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Wow I didn't expect such an elaborate answer! Thank you for enlightening me with current practices :) And the vector implementation, just blew my mind. What does the declaration vector<node> mean? I've never seen a for loop being used in that way, would you mind explaining what it means?
The for loop used is called range-for loop. It is supported in C++11 and higher. Take a look at en.cppreference.com/w/cpp/language/range-for.
@NicholasChen I suppose you haven't seen templates yet. Well, in short, you can have a vector of int (and then you write vector<int>), of double (vector<double>), and so on. For node, you use vector<node>. You have to specify the type of data stored in the vector, and you do that by writing it between angle brackets. The for loop, as R Sahu told you, is called range-for and it should be easier to manage than the classic for loop, since you don't have to worry about the index and counting the elements.
1

Yes, you can have an array of nodes.

However, if you insist on an array, and you don't know the capacity at compile time, you'll have to allocate it at run-time:

struct node
{
  int hour;
  int minutes;
};


int main()
{
  int numOfLanding, minGap, hour, minutes;
  cin>>numOfLanding;
  cin>>minGap;
  cout<<endl;

  node *arr = new node[numOfLanding];

  for (int i=0; i<numOfLanding; i++)
  {
      cin>>hour;
      cin>>minutes;
      arr[i].hour=hour;
      arr[i].minutes=minutes;
  }

  // Remember to delete the array.
  delete[] arr;
  return EXIT_SUCCESS;
}

A safer alternative is to use std::vector<node>.

Note: Since it is an array, use the '.' for access, not ->.

4 Comments

This is a better answer.
Thank you for your answer! So to conclude, for array of nodes, to access it, we have to use '.' instead of '->' for it to work? Why is using vectors the safer alternative as well? I also noticed you put an EXIT_SUCCESS at the end, does it have the same value as return 0?
A std::vector will dynamically grow as you append items. An array is fixed in size.
The EXIT_SUCCESS is one of two standard values returned by main. The other is EXIT_FAILURE. You can look them up to see their values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.