Article View: alt.comp.lang.learn.c-c++
Article #306Re: pass structure by reference / by value
From: Roland
Date: Sat, 26 Jan 2002 17:00
Date: Sat, 26 Jan 2002 17:00
66 lines
2709 bytes
2709 bytes
Hi,
I didn't scan through the complete example but what makes me curious is
this:
strcpy(s[0].id, "1000001");
This will copy 8 characters (the 8'th char is the delimiting 0) to the
target.
BUT your target can hold only 7 chars. I expect this will overwrite the
next item in your struct. And subsequently trash something.
Roland
"EL" <lameddie@yahoo.com> wrote in message
news:a2um2g$1q1@imsp212.netvigator.com...
> Hi all,
>
> 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];
> };
>
> void appRec(mystr* , int&);
>
> void main()
> {
> mystr s[50];
> int i, j, b, ttl;
> 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");
>
> ttl = 2;
> appRec(s, ttl);
> for (i=0; i<ttl ; i++)
> {
> cout << "\nStud ID : " << s[i].id;
> cout << "\tName : " << s[i].name;
> }
>
> }
>
> void appRec(mystr *s1, int &ttl)
> {
> ttl++;
> 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;
> }
> }
>
>
Technical Details
Message-ID:
<kgB48.119284$5G5.963381@news.chello.at>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!colt.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsrouter.chello.at!news.chello.at.POSTED!53ab2750!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: 109fba,d119aab9300bf461 X-Google-Attributes: gid109fba,public X-Google-Thread: 114592,d119aab9300bf461 X-Google-Attributes: gid114592,public X-Google-ArrivalTime: 2002-01-26 09:00:09 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!colt.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsrouter.chello.at!news.chello.at.POSTED!53ab2750!not-for-mail From: "Roland" <nospam@chello.at> 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: 68 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: <kgB48.119284$5G5.963381@news.chello.at> Date: Sat, 26 Jan 2002 17:00:00 GMT NNTP-Posting-Host: 212.17.86.171 X-Complaints-To: abuse@news.chello.at X-Trace: news.chello.at 1012064400 212.17.86.171 (Sat, 26 Jan 2002 18:00:00 CET) NNTP-Posting-Date: Sat, 26 Jan 2002 18:00:00 CET Organization: Customers chello Austria