473,785 Members | 2,321 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

3 New Member
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 2426
scruggsy
147 New Member
I read your code wrong. Hang on.
Jul 27 '07 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
jadeivel756
3 New Member
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 Recognized Expert Moderator Expert
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=jadeivel7 56]
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
2709
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. > > Can somebody please tell whats wrong?? > > This is the code : : #include<fstream.h> #include<conio.h>
22
2059
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; struct ln *next; } listNode, *listNodePtr; just to test in my main method I tried to initialize the
19
2084
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 pointer beasties. The following code is excerpted from digitemp, a program that reads 1-wire devices. Digitemp is the work of Brian C. Lane. As I walk through this code, I have a number of questions. I'm hoping someone with experience in...
8
2785
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 context, const LadybugImage* pimage, unsigned char* arpszDestBuffers, LadybugImageInfo* pImageInfo );
2
1817
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 called the function wordcount, and then the function did everything including printing out the results. That worked. Now I want to make the function return an array of pointers to struct palabra so the calling function can manage the data as it...
12
3888
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 looked sensible, but it didn't work right. Here is a simple example of what I'm trying to accomplish: // I have a hardware peripheral that I'm trying to access // that has two ports. Each port has 10 sequential // registers. Create a...
28
4717
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
2977
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 pass its first block pointer inside the structure Array to Array ->first and the last block pointer inside the structure Array to Array ->last.So i can manipulate the double linked list as a dynamic array. The cells of this dynamic array are...
0
9489
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10162
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10100
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8988
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5396
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4061
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.