473,473 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Help with the procedure..

25 New Member
(6)
Write a small procedure that will prompt the user for an integer, then get that value and assign it to a local variable. This local variable is then used to change the value of a passed in parameter. The routine should be called getValue and look like:

void getValue(int *value_to_be_returned)
AND it will be called from the mainline as
getValue(&my_value);
WHERE myvalue is declared inside the mainline.

I've tried to do this but that's sumtin wrong in the code:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. void getValue ( int * value_to_be_returned )
  4.  
  5. {
  6.  
  7. static int returned_value;
  8.  
  9. returned_value = 5;
  10.  
  11. * value_to_be_returned = returned_value;
  12.  
  13. }
  14.  
  15. int main ( int argc, char ** argv )
  16.  
  17. {
  18.  
  19. // Mainline Variable Declarations
  20. FILE * output = stdout;
  21. FILE * input = stdin;
  22.  
  23. int * my_value;
  24. int number;
  25.  
  26. fprintf ( output, "Enter the integer number : " ); fflush ( stdout );
  27. fscanf ( input, "%d", &number );
  28.  
  29. * my_value = number;
  30.  
  31. fprintf ( output, "\nValue that is entered is %d\n\n", &my_value );
  32.  
  33. getValue ( &my_value );
  34.  
  35. fprintf ( output, "Value that is changed is %d\n\n", &my_value );
  36.  
  37. }
  38.  
  39.  
Nov 4 '06 #1
6 1578
horace1
1,510 Recognized Expert Top Contributor
my_value is a int * (pointer to int) not an int so your function call (which passes the address of my_value)
getValue (& my_value );

should be (which passes the address of number)
getValue ( my_value );

you have similar mistakes elsewhere, the following now works
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. void getValue ( int * value_to_be_returned )
  4. {
  5. static int returned_value;
  6.  
  7. returned_value = 5;
  8.  
  9. * value_to_be_returned = returned_value;
  10.  
  11. }
  12.  
  13. int main ( int argc, char ** argv )
  14. {
  15. // Mainline Variable Declarations
  16. FILE * output = stdout;
  17. FILE * input = stdin;
  18.  
  19. int * my_value;
  20. int number;
  21.  
  22. fprintf ( output, "Enter the integer number : " ); fflush ( stdout );
  23. fscanf ( input, "%d", &number );
  24.  
  25. * my_value = number;
  26.  
  27. fprintf ( output, "\nValue that is entered is %d\n\n", *my_value );
  28.  
  29. getValue ( my_value );
  30.  
  31. fprintf ( output, "Value that is changed is %d\n\n", *my_value );
  32.  
  33. }
  34.  
you don't need my_value, you could use number directly
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. void getValue ( int * value_to_be_returned )
  4. {
  5. static int returned_value;
  6.  
  7. returned_value = 5;
  8.  
  9. * value_to_be_returned = returned_value;
  10.  
  11. }
  12.  
  13. int main ( int argc, char ** argv )
  14.  
  15. {
  16.  
  17. // Mainline Variable Declarations
  18. FILE * output = stdout;
  19. FILE * input = stdin;
  20. int number;
  21.  
  22. fprintf ( output, "Enter the integer number : " ); fflush ( stdout );
  23. fscanf ( input, "%d", &number );
  24.  
  25. fprintf ( output, "\nValue that is entered is %d\n\n", number );
  26.  
  27. getValue ( &number );
  28.  
  29. fprintf ( output, "Value that is changed is %d\n\n", number );
  30. }
  31.  
Nov 4 '06 #2
evantri
25 New Member
Your code worked but there is a segmentation fault appeared, what happened?
Nov 4 '06 #3
horace1
1,510 Recognized Expert Top Contributor
Your code worked but there is a segmentation fault appeared, what happened?
both programs run OK on my system (using DEV-C++ with gcc compiler) - do both give segmentation faults when you run them?

you get a segmentation fault if you use an invalid address, e.g. if you miss the & when calling getValue
Expand|Select|Wrap|Line Numbers
  1. getValue ( number );
  2.  
getValue() would use the value of number as the address rather than the address of number

did you copy and paste my code or edit your own, if the latter you probably missed something
Nov 4 '06 #4
evantri
25 New Member
I copied your code. I'm using cc compiler. Still gave me seg fault
Nov 4 '06 #5
horace1
1,510 Recognized Expert Top Contributor
I copied your code. I'm using cc compiler. Still gave me seg fault
managed to replicate the problem on a Linux box using cc
In the first program I missed assigning the address of number to my_value. The code is now
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. void getValue ( int * value_to_be_returned )
  4. {
  5. static int returned_value;
  6.  
  7. returned_value = 5;
  8.  
  9. * value_to_be_returned = returned_value;
  10.  
  11. }
  12.  
  13. int main ( int argc, char ** argv )
  14. {
  15. // Mainline Variable Declarations
  16. FILE * output = stdout;
  17. FILE * input = stdin;
  18.  
  19. int * my_value;
  20. int number;
  21.  
  22. fprintf ( output, "Enter the integer number : " ); fflush ( stdout );
  23. fscanf ( input, "%d", &number );
  24.  
  25. my_value=&number;               // ** missing statement
  26. * my_value = number;
  27.  
  28. fprintf ( output, "\nValue that is entered is %d\n\n", *my_value );
  29.  
  30. getValue ( my_value );
  31.  
  32. fprintf ( output, "Value that is changed is %d\n\n", *my_value );
  33.  
  34. }
  35.  
Nov 4 '06 #6
evantri
25 New Member
The code worked now, thanx
Nov 5 '06 #7

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

Similar topics

2
by: berthelot samuel | last post by:
Hi everyone, I am currently trying to write a report based on a View of SQL Server. Basically, I have 3 tables : Hardware, SoftwareInstalled and Software with SoftwareInstalled that keeps track of...
7
by: Bill Kellaway | last post by:
Hi there - this should be fairly simple for someone. Basically I can't figure out how to pass the parameters from ASP to a Stored Procedure on SQL. Here's my code: I just need to help in...
15
by: Philip Mette | last post by:
I am begginner at best so I hope someone that is better can help. I have a stored procedure that updates a view that I wrote using 2 cursors.(Kind of a Inner Loop) I wrote it this way Because I...
1
by: Kumar | last post by:
Hi I am trying to recreate a database under the following environments : From: Solaris with DB2UDB version 7.2 with FP 9 To: Linux with DB2UDB version 7.2 with FP 9 It will be of really a...
4
by: Jeff User | last post by:
Hi I tryed to solve this problem over in the framework.asp group, but still am having trouble. Hope someone here can help. using .net 1.1, VS 2003 and C# I have an asp.DataGrid control with a...
7
by: Siv | last post by:
Hi, I have a stored procedure that I want to execute and then wait in a loop showing a timer whilst it completes and then carry on once I get notification that it has completed. The main reason...
8
by: peteh | last post by:
Hi All; I have what I hope is a simple environmental problem. I have some SQL that creates a VERY simple procedure. When the create is executed from Quest (from a Windows client) - no problem. As...
3
by: Darth Ferret | last post by:
This thing is about to drive me crazy. I have about 50 queries in the AS400 that I need to put on a menu. Once I conquer this I have a bunch more rpg reports that I need to pass a date to. In the...
17
by: Riaaaa | last post by:
Pls check my code for the stored procedure which i created for the companydetails including companyid P.K. Not Null int(4), companyname Not Null varchar (20), address varchar(30) where...
0
by: SOI_0152 | last post by:
Hi all! Happy New Year 2008. Il hope it will bring you love and happyness I'm new on this forum. I wrote a stored procedure on mainframe using DB2 7.1.1 and IBM language c. Everything works...
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
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,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.