473,404 Members | 2,137 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,404 software developers and data experts.

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

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
4 2409
scruggsy
147 100+
I read your code wrong. Hang on.
Jul 27 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
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
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
9,208 Expert Mod 8TB
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

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

Similar topics

3
by: Abhas | last post by:
> > Hi, this is Abhas, > > I had made a video library program in C++, but was facing a problem. > > After entering 12 movies, i cannot enter any more movies. > > Something gibberish comes instead....
22
by: Dave Cooke | last post by:
Hi I am very new to C. I am trying to figure out how to initialize a struct in my main program. The struct is declared in anouther header file like this... typedef struct ln { int key; int data;...
19
by: Mark Richards | last post by:
I've been programming for many years, but have only recently taken a deep "C" dive (bad pun, i know) and need a lot of explanation from an expert. My questions center around those mysterious...
8
by: Don | last post by:
I have a third party C++ DLL that I am trying to use from C#. The specific function I am trying to use is declared in C++ as follows: ladybugConvertToMultipleBGRU32( LadybugContext ...
2
by: leo2100 | last post by:
Hi, I need help with this program. The program is supposed to take a text file and identify the words in it, then it should print them and count how many times a word is repeated. At first main...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
28
by: Bill | last post by:
Hello All, I am trying to pass a struct to a function. How would that best be accomplished? Thanks, Bill
3
by: dreiko466 | last post by:
(sorry about my english...) I am a newbie in C (3 month expierience) I have wrote a simple test programm in VS2005, what i do wrong?Please... In this programm i create a double linked list.Then ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.