473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

arrays of structures {challenging}urgent

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


Expand|Select|Wrap|Line Numbers
  1.  
  2. #include<iostream.h>
  3. #include<stdlib.h>
  4. main()
  5. {
  6. struct student
  7. {
  8. char name[30];
  9. int age;
  10. };
  11.  
  12. student a[10];
  13.  
  14. student a[0]={"husnain",23}; //this statement is not working...........
  15.  
  16. system("pause");
  17.  
  18. }
  19.  
Dec 22 '08 #1
9 1441
osfreak
22
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include<stdio.h>
  3. //#include<iostream.h>
  4. //#include<stdlib.h>
  5. main()
  6. {
  7. struct student
  8. {
  9. char name[30];
  10. int age;
  11. };
  12.  
  13. //student a[10];
  14. //student a[0]={"husnain",23}; 
  15.  
  16. struct student a[10] = {"husnain",23}; 
  17.  
  18.  
  19. //system("pause");
  20. getchar();
  21.  
  22. }
  23.  
  24.  
  25.  
  26.  
Reasons
1. a is being declared twice
2. Values inside braces {"husnain",23} are variable initializations that can be done only on variable declaration
Dec 22 '08 #2
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.
Dec 22 '08 #3
svlsr2000
181 Expert 100+
just use a[0] = {"human ",9};
Dec 22 '08 #4
osfreak
22
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,

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include<iostream>
  3. using namespace std;
  4. //#include<stdlib.h>
  5. main()
  6. {
  7. struct student
  8. {
  9.  
  10. student(char * pName, int pAge):age(pAge)
  11. {
  12. strcpy(name,pName);
  13. }
  14. student()
  15. {}
  16. char name[30];
  17. int age;
  18. };
  19.  
  20. struct student *a[10];
  21.  
  22. a[0]= new student("husnain",23);
  23.  
  24. //system("pause");
  25. getchar();
  26.  
  27. }
  28.  
Sorry, I see no other way...
Dec 22 '08 #5
Banfa
9,065 Expert Mod 8TB
OK everyone please remember to use [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("husnain",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("husnain",23);

but only in C++ not in C.
Dec 22 '08 #6
weaknessforcats
9,208 Expert Mod 8TB
OK.

First let's decide on which language you are using. Based on the #include<iostream.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.
Dec 22 '08 #7
osfreak
22
Mr.Banfa a doubt,

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

while


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


Am i correct?
Dec 23 '08 #8
Banfa
9,065 Expert Mod 8TB
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.
Dec 23 '08 #9
osfreak
22
Thanks for clarifying it.... :)
Dec 24 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

28
by: Tamir Khason | last post by:
Follwing the struct: public struct TpSomeMsgRep { public uint SomeId;
0
by: Mike Stoker | last post by:
I'm trying to use an existing COM object from within .NET and am having problems passing a parameter than used to be of type VARIANT. I have a COM interface, IScanner with the following method:...
3
by: avy31 | last post by:
Hy, I have a problem at which I can not find the solution: I have this: Declare Sub VRB_IO_StdInstel Lib "TWVRB408.DLL" Alias "_VRB_IO_StdInstel@12" (ByVal Mode As Integer, ByVal szIniFile As...
4
by: Jeff Cobelli | last post by:
I am running two sites on the same server, one for live clients and one for testing before we post to live. Each site contains both C# and VB.NET web services that connect to a SQL Server 2000...
1
by: tigerved | last post by:
Hi I am new to this so I dont exactly know the format in which to post the questions ....anyways here goes ...my file looks like this Year,Day,MOnth,latitude,longitude (commas included)...
1
by: AnduSe23 | last post by:
Hi, I implemented a solution for a homework assignment and everytime i run my program it crashes. could someone tell me what i'm doing wrong? I so confused what to pass into the functions when...
8
by: Bob Altman | last post by:
Hi all, I have a structure that includes a constructor. I want to add a bunch of these structures to an STL map (whose index is an int). If I define the map like this: map<int,...
1
by: Ken Fine | last post by:
I have set up Microsoft Search Server 2008 Express. I want to know how I can query against it and return results in a form that can be bound to ASP.NET controls like ListViews. If there's simply...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.