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

Help with the procedure..

P: 25
(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
Share this Question
Share on Google+
6 Replies


Expert 100+
P: 1,510
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

P: 25
Your code worked but there is a segmentation fault appeared, what happened?
Nov 4 '06 #3

Expert 100+
P: 1,510
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

P: 25
I copied your code. I'm using cc compiler. Still gave me seg fault
Nov 4 '06 #5

Expert 100+
P: 1,510
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

P: 25
The code worked now, thanx
Nov 5 '06 #7

Post your reply

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