0

I've made a new node. How do I refer node->question->question, node->question->options[], node->question->correctans and node->question->difficulty_level

struct node {

    node *prev;

    node *next;

    int count; 

    Question question[];

};
struct Question{

    String question;

    String[] options;

    String correctans;

    int difficulty_level;
}
Praful Bagai
  • 16,684
  • 50
  • 136
  • 267

2 Answers2

0

'->' is a pointer dereference. Since you have an array of objects in your node (and not just pointer to them), the correct way to access those would be 'node.question[x]'. The same applies for your Question struct, i.e. node.question[0].question would yield the question string of the first question saved in this nodes array (if this was valid c code...).

Hope this helps, let me know if anything is unclear to you.

Andreas A.
  • 11
  • 3
  • Actually each node is pointing to an array of objects of structure Questions. How would I do now? Sorry for causing trouble. – Praful Bagai Dec 27 '13 at 19:48
0

I am not certain what you are asking but I think this is what your looking for:

#include <string.h>
#include <iostream>
using namespace std;

struct Question{

    string *question;

    string  *options [4];

    string correctans;

    int difficulty_level;
};

struct node {

    node *prev;

    node *next;

    int count; 

    Question *question[4]; 

}n;

int main(){
    Question *q;
    Question qu;

    q->question = new string("This is a question"); // assign some values
    q->options[0]= new string("The first option");
    q->difficulty_level = 4;

    qu.question = new string("another question");
    qu.options[0]= new string("The second option");
    qu.difficulty_level = 5;

    n.question[0] = q;
    n.question[1] = &qu;
    //print the assigned values
    cout << "Question 1 question: " <<  *n.question[0]->question << endl;
    cout << "Question 1 Option 1: "<<*n.question[0]->options[0] << endl;
    cout << "Question 1 dificulty: "<<n.question[0]->difficulty_level << endl;

    cout << "Question 2 question: " <<  *n.question[1]->question << endl;
    cout << "Question 2 Option 1: "<<*n.question[1]->options[0] << endl;
    cout << "Question 2 dificulty: "<<n.question[1]->difficulty_level << endl;

}
Gibby
  • 458
  • 1
  • 4
  • 14