arrays of structures {challenging}urgent

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thebeta
    New Member
    • Oct 2008
    • 4

    arrays of structures {challenging}urgent

    Hi dears.
    actually i am facing a difficulty while assigning values to the structure members


    Code:
     
    #include<iostream.h>
    #include<stdlib.h>
    main()
    {
    struct student
    {
    char name[30];
    int age;
    };
     
    student a[10];
     
    student a[0]={"husnain",23}; //this statement is not working...........
     
    system("pause");
     
    }
  • osfreak
    New Member
    • Oct 2008
    • 22

    #2
    Code:
     
    #include<stdio.h>
    //#include<iostream.h>
    //#include<stdlib.h>
    main()
    {
    struct student
    {
    char name[30];
    int age;
    };
     
    //student a[10];
    //student a[0]={"husnain",23}; 
     
    struct student a[10] = {"husnain",23}; 
     
     
    //system("pause");
    getchar();
     
    }
    Reasons
    1. a is being declared twice
    2. Values inside braces {"husnain",2 3} are variable initializations that can be done only on variable declaration

    Comment

    • thebeta
      New Member
      • Oct 2008
      • 4

      #3
      dear that is not my requirement i have declared student a[10] array which has size of 10.now i want to initialize this can you help me.

      Comment

      • svlsr2000
        Recognized Expert New Member
        • Feb 2007
        • 181

        #4
        just use a[0] = {"human ",9};

        Comment

        • osfreak
          New Member
          • Oct 2008
          • 22

          #5
          Initialising a struct is possible only at declaration,

          so i doubt this does not work

          a[0] = {"human",9};
          ---------------------------------------------------------------------------------------------------

          Here is one possible way, if u really want initialization to happen after declaration....

          Have a constructor for the struct and create variables at runtime,

          Code:
            
          #include<iostream>
          using namespace std;
          //#include<stdlib.h>
          main()
          {
          struct student
          {
           
          student(char * pName, int pAge):age(pAge)
          {
          strcpy(name,pName);
          }
          student()
          {}
          char name[30];
          int age;
          };
           
          struct student *a[10];
           
          a[0]= new student("husnain",23);
           
          //system("pause");
          getchar();
           
          }
          Sorry, I see no other way...

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            OK everyone please remember to use &#91;code] ... [/code] tags round your code unless it is only 1 or 2 lines, it makes it easier to read.

            This

            a[0]= new student("husnai n",23);

            is not good because you have new'd an object without deleting it and caused a memory leak, however this should work

            a[0]= student("husnai n",23);

            but only in C++ not in C.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              OK.

              First let's decide on which language you are using. Based on the #include<iostre am.h>, it looks like you want C++.

              Second, use the correct header. <iostream.h> is pre-ANS C++ and has been obsolete for 10 years. Use <iostream>.

              Third, <stdlib.h> is a C header and getchar() is a C function. You should be using cin>> or cin.get() or cin.getline(), etc. Maybe some research here. Remove the <stdlib.h> header.

              Fourth, C++ uses string rather than char arrays. If you have to use arrays, remove the <iostream.h>, replace it with <stdio.h> and compile as C rather than C++. Otherwise use a string.

              Fifth, strcpy() is C. The C string library is generally not use din C++. A C++ string object knows how to copy itself so strcpy() is not required. All you do is assign the string a value.

              Sixth, C++ does not use arrays of Student. Instead, use a vector<Student> . You will find that all the protective code written to support an array of Student duplicates the code of vector that manages the same array.

              Comment

              • osfreak
                New Member
                • Oct 2008
                • 22

                #8
                Mr.Banfa a doubt,

                student a[10];
                a[0]= student("husnai n",23); //works on an existing object

                while


                student * a[10];
                a[0]= new student("husnai n",23); //Creates a new object


                Am i correct?

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  Actually osfreak I have made a mistake because I had not noticed you had changed the type of the array from student to student *.

                  However both examples you give create new objects, assuming the code is inside a function then

                  The first example creates (and initialises) an array of 10 students in automatic scope (normally on the stack) using the default constructor. It then creates a student using automatic scope using an overloaded constructor taking 2 parameters. It copies this object to the first entry in the array using the (possibly default) assignment operator.

                  When the function exits since all these objects have automatic scope they are automatically deleted (the destructor is called) and the memory freed.

                  The example example creates but doesn't initialise an array of 10 pointers to students in automatic scope. It then explicitly allocates a new student (normally on the heap) using an overloaded constructor taking 2 parameters. It copies the pointer to this object to the first entry in the array (pointer assignment is a basic part of the language).

                  Before the function exits and the array is automatically deleted the program(mer) must ensure that any entries in the array that have been used are deleted.

                  Comment

                  • osfreak
                    New Member
                    • Oct 2008
                    • 22

                    #10
                    Thanks for clarifying it.... :)

                    Comment

                    Working...