🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: alt.comp.lang.learn.c-c++
4 messages
4 total messages Started by EL Sat, 26 Jan 2002 16:46
pass structure by reference / by value
#305
Author: EL
Date: Sat, 26 Jan 2002 16:46
46 lines
1887 bytes
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;
  }
}
Re: pass structure by reference / by value
#306
Author: Roland
Date: Sat, 26 Jan 2002 17:00
66 lines
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;
>   }
> }
>
>
Re: pass structure by reference / by value
#307
Author: Berend Meijer
Date: Sat, 26 Jan 2002 18:28
72 lines
2701 bytes
You are incrementing ttl in the wrong place. Try this.

Berend


void appRec(mystr *s1, int &ttl)
{
  cout << "\nTotal in function: " << ttl;
  strcpy(s1[ttl].id, "1000005");
  strcpy(s1[ttl].name, "Minotaic");
  ttl++;
  int i=0;
  for (i=0; i<ttl; i++)
  {
   cout << "\nStud ID fnt : " << s1[i].id;
   cout << "\tName    fnt : " << s1[i].name;
  }
}

--

"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;
>   }
> }
>
>
Re: pass structure by reference / by value
#308
Author: Chris ( Val )
Date: Sat, 26 Jan 2002 19:06
149 lines
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
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads