By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,221 Members | 1,663 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,221 IT Pros & Developers. It's quick & easy.

HELP... hOW TO Pass value to a struct type

P: 3
I BADLY NEED YOUR HELP......

HELP... hOW TO Pass value to a struct type and permanently store the data after youve given the data.The programming language is C.

My problem is that as I exit the function "int reg(struct Member listm[], int msize);", the data Ive entered was not stored in the array.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <string.h>
  4.  
  5. struct Member
  6. {
  7.     char name[50];
  8.     char address[50];
  9.     int age;
  10.     char gender;
  11.     int memID[50];
  12. };
  13.  
  14. struct Video
  15. {
  16.     char title[50];
  17.     char genre[50];
  18.     int borrow[50];
  19.     int videoID[50];
  20. };
  21.  
  22. int reg(struct Member listm[], int msize);
  23. main()
  24. {
  25.     struct Member listm[50];
  26.     struct Video listv[50];
  27.     int choice, again=0;
  28.     int i;
  29.  
  30.     /*
  31.     for (i=0; i<50; i++)
  32.     {
  33.         listm[i].memID[i]=0;
  34.         listv[i].videoID[i]=0;
  35.         listv[i].borrow[i]='A';
  36.     }*/
  37.  
  38.         clrscr();
  39.         printf("\nWELCOME TO ANIME VIDEO RENTAL SHOP");
  40.         printf("\nWhat do you want to do?");
  41.         printf("\n1. Register (if not yet a member)");
  42.                                 scanf("%d", &choice);
  43.                                 again=reg(listm, 50);
  44. getch();
  45. return(0);
  46.  
  47. }
  48.  
  49. int reg(struct Member listm[], int msize)
  50. {
  51.     struct Member *mlist[50];
  52.     static int i;
  53.     mlist[50]=&listm[msize];
  54.  
  55.         printf("\nName: ");
  56.         scanf("%s", &mlist[i]->name);
  57.         fflush(stdin);
  58.  
  59.         printf("\nAddress: ");
  60.         scanf("%s", &mlist[i]->address);
  61.         fflush(stdin);
  62.  
  63.         printf("\nAge: ");
  64.         scanf("%d", &mlist[i]->age);
  65.         fflush(stdin);
  66.  
  67.         i++;
  68.  
  69. return(0);
  70. }
  71.  
Jul 27 '07 #1
Share this Question
Share on Google+
4 Replies


100+
P: 147
I read your code wrong. Hang on.
Jul 27 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
This code:
scanf("%s", &mlist[i]->name);
Looks fishy. Maybe it should be:

Expand|Select|Wrap|Line Numbers
  1.  scanf("%s", &listm[i]->name);
  2.  
An what's this for?
struct Member *mlist[50];
static int i;
mlist[50]=&listm[msize];
Here you declare an array of 50 pointers but only initialize the last pointer.
Then, you use the uninitialized pointers in youre scanf().

It doesn't look like you need the mlist array at all.

Lastly, why a static variable??? The static keyword is largely deprecated in C++.
Jul 27 '07 #3

P: 3
thanks..uhm can you help me how to store a data in an array in a function?that is after i exit the function, it will still be stored?
Jul 28 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
thanks..uhm can you help me how to store a data in an array in a function?that is after i exit the function, it will still be stored?
You can't pass an array to a function. All you can pass is a type or a pointer to a type.

In the case of an array, C++ treats the name of the array as the address of element 0. So, when you pass an array, you are really passing the address of element 0.

When the function works with the array, since it only has the address of the array, any change to the array made by the function will be put in the array.

Therefore:
[quote=jadeivel756]
int reg(struct Member listm[], int msize)
{
etc...
[/quote

any change made to the array using listm are made to the array itsself since listm is just a pointer to element 0.
Jul 28 '07 #5

Post your reply

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