Article View: alt.comp.lang.learn.c-c++
Article #308Re: pass structure by reference / by value
From: Chris ( Val )
Date: Sat, 26 Jan 2002 19:06
Date: Sat, 26 Jan 2002 19:06
149 lines
5047 bytes
5047 bytes
"EL" <lameddie@yahoo.com> wrote in message news:a2um2g$1q1@imsp212.netvigator.com...
| Hi all,
Hi EL, some comments follow...
| I want to pass a structure to my own function and change its value, however
| i couldn't get the expected result. why?
|
| struct mystr
| {
| char id[7], name[21];
// Here you are overwriting the memory you allocated.
// id holds n-1 chars, but you parse it 'n' and leave no
// room for the null terminator '\0'.
// Making the storage const might also be better too.
| };
|
| void appRec(mystr* , int&);
// You can avoid these pointers, see below...
| void main()
// Should be int main(), as set by the standards association...
| {
| mystr s[50];
| int i, j, b, ttl;
// You should always initialise all variables before you try to
// access them, this will avoid undefined results in your code.
// Providing some meaningfull names would not hurt at all either :-).
| char vid[7], bid[7];
|
| strcpy(s[0].id, "1000001");
| strcpy(s[0].name, "RobertHi");
| strcpy(s[1].id, "1000008");
| strcpy(s[1].name, "KawaiiNic");
// Loading records in this way is 'ok' for testing, but would be
// better to read in the records at run time, as you will see in my
// example of your code below.
| ttl = 2;
// This magic number should be eliminated. Generally, you would
// set this up to suit the number of records read in.
| appRec(s, ttl);
| for (i=0; i<ttl ; i++)
// If your compiler supports it, it's a good idea to declare
// your 'i' variable inside the for loop. For example:
for ( int i = 0; i < ttl ; i++ )
| {
| cout << "\nStud ID : " << s[i].id;
| cout << "\tName : " << s[i].name;
| }
|
| }
|
| void appRec(mystr *s1, int &ttl)
| {
| ttl++;
// This is not right. The variable "ttl", comes in at the value '2',
// so you have the records [0], [1] with data, and [2] which is empty
// but waiting for some data to be entered, but you increment it
// before adding any data to that index, so [2] is always empty.
| cout << "\nTotal in function: " << ttl;
| strcpy(s1[ttl].id, "1000005");
| strcpy(s1[ttl].name, "Minotaic");
| int i=0;
| for (i=0; i<ttl; i++)
| {
| cout << "\nStud ID fnt : " << s1[i].id;
| cout << "\tName fnt : " << s1[i].name;
| }
| }
// I don't know why you have this loop in this function, but
// your much better off, if you implement seperate functions
// for seperate tasks. Please see below...
// I have modified your existing code in such a way to give
// you another perspective of achieving your goal. I hope you
// can take something away from it :-).
# include <iostream>
using namespace std; // Generic Declaration...
struct Person
{
static const int MAX = 50;
char Id[MAX];
char Name[MAX];
};
void AddRecord(Person Rec[], size_t& Total)
{
size_t Count = 0;
char Choice = ' ';
do
{
cout << "Enter an id ";
cin >> Rec[Count].Id;
cout << "\nEnter person name ";
cin.ignore();
cin.getline(Rec[Count].Name, 50, '\n');
cout << "Would you like to continue? [Y/N] ";
cin >> Choice;
Count ++;
Total ++;
}
while( Choice != 'n' && Choice != 'N');
}
void ShowRecords(Person Rec[], const size_t& Total)
{
cout << "\nRecords Stored In Database Are... ";
for(size_t i = 0; i < Total; i++)
{
cout << "\nStudent ID " << Rec[i].Id;
cout << "\tPerson Name " << Rec[i].Name;
}
}
int main()
{
const int NrRECORDS = 50;
Person Record[NrRECORDS];
size_t Total = 0;
AddRecord( Record, Total );
ShowRecords( Record, Total );
return 0;
}
Good Luck.
Chris Val
Technical Details
Message-ID:
<d2D48.80545$HW3.101684@newsfeeds.bigpond.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!intgwpad.nntp.telstra.net!news.telstra.net!newsfeeds.bigpond.com!not-for-mailReferences:
<a2um2g$1q1@imsp212.netvigator.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f8d38,d119aab9300bf461 X-Google-Attributes: gidf8d38,public X-Google-Thread: 114592,d119aab9300bf461 X-Google-Attributes: gid114592,public X-Google-Thread: 109fba,d119aab9300bf461 X-Google-Attributes: gid109fba,public X-Google-ArrivalTime: 2002-01-26 11:05:44 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!intgwpad.nntp.telstra.net!news.telstra.net!newsfeeds.bigpond.com!not-for-mail From: "Chris \( Val \)" <chrisval@bigpond.com.au> Newsgroups: alt.comp.lang.learn.c-c++,borland.public.cpp.language,comp.lang.c++ References: <a2um2g$1q1@imsp212.netvigator.com> Subject: Re: pass structure by reference / by value Lines: 155 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4522.1200 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200 Message-ID: <d2D48.80545$HW3.101684@newsfeeds.bigpond.com> Date: Sun, 27 Jan 2002 06:06:30 +1100 NNTP-Posting-Host: 203.54.63.22 X-Trace: newsfeeds.bigpond.com 1012071689 203.54.63.22 (Sun, 27 Jan 2002 06:01:29 EST) NNTP-Posting-Date: Sun, 27 Jan 2002 06:01:29 EST Organization: Telstra BigPond Internet Services (http://www.bigpond.com)