473,383 Members | 1,859 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,383 software developers and data experts.

not able to initialize structure variable inside main whose structure is defined gl

i am trying to run a programme implementing a function with structures in c... which is:
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. struct store
  4.     {
  5.         char name[20];
  6.         float price;    
  7.         int quantity;
  8.     };
  9.  
  10. struct store update (struct store product, float p, int q);
  11.  
  12. float mul(struct store stock_value);
  13.  
  14.  
  15.     main()
  16. {
  17.     int inc_q;
  18.     float inc_p,value;
  19.  
  20.     struct store item = {"xyz", 10.895 ,10}; 
  21.  
  22. /*this is where the problem lies, when i am initializing this store item, the values are not being stored by the members i.e. ater this line the members (1)item.name should be "xyz",(2)item.price should be 10.895, (3)item.quantity should be 10; but except the item.name=xyz other members take a garbage value of their own*/
  23.  
  24.     printf("name    = %s\n price = %d\n quantity =  %d\n\n",item.name,item.price,item.quantity);
  25.  
  26.     printf("enter increment in price(1st) and quantity(2nd) : ");
  27.     scanf("%f %d",&inc_p,&inc_q);
  28.  
  29.     item = update(item,inc_p,inc_q);
  30.  
  31.     printf("updated values are\n\n");
  32.     printf(" name       = %d\n price      = %d\n quantity    = %d",item.name,item.price,item.quantity);
  33.  
  34.     value = mul(item);
  35.  
  36.     printf("\n\n value = %d",value);
  37. }
  38. struct store update(struct store product, float p, int q)
  39. {
  40.     product.price+=p;
  41.     product.quantity+=q;
  42.     return(product);
  43. }
  44. float mul(struct store stock_value)
  45. {
  46.     return(stock_value.price*stock_value.quantity);


some one please help to remove the error in my code.. reply soon
Aug 27 '13 #1
1 1233
weaknessforcats
9,208 Expert Mod 8TB
This code:
Expand|Select|Wrap|Line Numbers
  1. struct store item = {"xyz", 10.895 ,10}; 
  2.  
10.895 is a double whereas the struct needs a float. Either change to double in the struct or code 10.905F. Otherwise you get a warning "conversion double to float possible loss of data".

Also, your update function takes a store argument which means the compiler generates a copy for the function to work with. It is this copy that is updated and deleted when the function completes. You need a store* here instead so you update the original variable and not the copy.

I changed that and everything compiled OK. I did not test anything.
Aug 27 '13 #2

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

Similar topics

7
by: lallous | last post by:
Hello, Why is the static variable inside a member function static for all instances and not static for the current instance of the object? -- Elias
10
by: Fred Ma | last post by:
Are there any reasons that would make it bad for C++ to allow simultaneous declaration and initilization of member data? Current way: ------------ class DerivedClass : BaseClass { { enum {...
17
by: Davor | last post by:
How to define global variable in main()? I'm asking because I have an array in main, whose size is determined by input, so the definition has to be in main ( or in some other funcion ). And I need...
4
by: MattFit | last post by:
I just downloaded the LCC-Win Compiler and wrote the following program: #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv) { printf("Hello World\n"); return 0; }
1
by: Mikko Penkkimäki | last post by:
I have a struct in C#-project and C++-project. I use the struct in C# but it has to be filled in C++. I know that arrays work like this: In C++ code: struct teststruct { char str; float...
7
by: nospam | last post by:
Hello All, Is it possible to place a variable inside of a textbox and have it viewed as the value? Or even read in as a string. Here's the problem... I have a text file I would like to read in...
4
by: Boni | last post by:
Dear all, how can I initialize shared variable? i.e class A private shared B as boolean end class in C++ I would write in the global scope A::B=true What is the proper way in vb
5
by: lovecreatesbea... | last post by:
Does the expression *(int *)&s1 below inside the printf() statement guarantee to refer to the first member of the structure variable s1? I've tried the code and it seems that it works that way. The...
4
Dheeraj Joshi
by: Dheeraj Joshi | last post by:
Hi, I was wondering is there any technique available, so we can access the global variable inside a function if we have a local variable inside the function with the same name as global variable. ...
2
by: Sajini Biju | last post by:
Can I get a structure variable in function for eg:- struct stud{ char name}stud1; void student(char); void main() { char n; scanf("%s",n); strcpy(stud1.name,n);
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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...

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.