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

Why the place of an variable on the script makes it a pointer or a simple variable.

P: 5
Hi, I am learning c language and want to understand a behavior of the language regarding where to inplement variables.




Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. //Compiler version gcc  6.3.0
  4. /*On declare 4 fonctions*/
  5. int ad (int num1, int num2);
  6. int sub (int num1, int num2);
  7. int mul (int num1, int num2);
  8. int div (int num1, int num2);
  9.  
  10. int main()
  11. {
  12.      int chif, num1, num2;
  13.      int(*op[4])(int num1, int num2);
  14.      op[0]=ad;
  15.      op[1]=sub;
  16.      op[2]=mul;
  17.      op[3]=div;
  18.       int result=op[chif](num1, num2);
  19.  
  20.      printf("Choisissez deux nombres separes par enter\n");
  21.      scanf("%d%d", &num1, &num2);
  22.      printf("choisissez un chiffre entre 0 et 3 pour +-*/ \n");
  23.      scanf("%d", &chif);
  24.  
  25.      printf("le reultat est %d\n", result);
  26.      return 0;
  27. }
  28.  
  29. int ad (int x, int y)
  30. {
  31.      return (x+y);
  32. }
  33. int sub (int x, int y)
  34. {
  35.      return(x-y);
  36. }
  37. int mul (int x, int y)
  38. {
  39.      return(x*y);
  40. }
  41. int div (int x, int y)
  42. {
  43.      return(x/y);
  44. }
  45. /*Outputs
  46. Choisissez deux nombres separes par enter
  47. 1
  48. 2
  49. choisissez un chiffre entre 0 et 3 pour +-*/ 
  50.  
  51. le reultat est -1931796686
  52.  
  53. Process finished.
  54. */
  55.  
  56.  
  57.  
but if I initialize the variable `result` after the last `scanf`, the code works fine. Why is the emplacement where I initialize the variable relevant, if I remember right in JavaScript the place is not relevant inside the function. It seems that the main function in C works differently. Can someone explain why. If the variable `result` is initialized before the input functions `scanf`, it is a pointer, if afterwards, it is a simple int variable.
1 Week Ago #1

✓ answered by dev7060

the output will be `le reultat est -1931796686`. And this shows that result is a pointer.
No, it doesn't. That's some garbage value because of the undefined behavior of the program.
Refer to what I mentioned in the previous post. In line 12, 'chif' is declared as a local variable. In line 18, 'chif' is being used as an index of the pointer array -> resulting in unexpected behavior.

In this case the output is good when I implement the variable `int result` at the line 24
This works because the non-static variable is used after assigning a value.

Share this Question
Share on Google+
4 Replies


100+
P: 135
If the variable `result` is declared before the input functions `scanf`, it is a pointer, if afterwards, it is a simple int variable.
I am not sure what you mean by this. If you declare something a pointer, it will be a pointer. If you declare it as a variable, it will work as a variable.

but if I declare the variable `result` after the last scanf, the code works fine.
Have a look at the control flow. Non-static variables (local variables) are indeterminate. Using/reading them prior to assigning a value results in undefined behavior.
6 Days Ago #2

P: 5
@dev7060, first, tanks so much for answering.

There is a pointer called `int(*op[4])(int num1, int num2)`.

the final step of the program is to output the variable, not pointer, called `op[chif](num1, num2)`, when num1, num2 and chif are reached by the inputs `scanf("%d%d", &num1, &num2)` and `scanf("%d", &chif)`.

Now, for the final step, I can write `printf("le reultat est %d\n", op[chif](num1, num2);` and in this case there is no problem. I can also write `printf("le reultat est %d\n", result);`. In this case the output is good when I initialize the variable `int result` at the line 24 for example, but if I write it above to the both `scanf("%d%d", &num1, &num2);` and `scanf("%d", &chif);`, the output will be `le reultat est -1931796686`. And this shows that result is a pointer.

In summary, why the code reads `result` as a pointer when it is initialized above and reads it as a standard int variable when it is initialized bellow.
6 Days Ago #3

100+
P: 135
the output will be `le reultat est -1931796686`. And this shows that result is a pointer.
No, it doesn't. That's some garbage value because of the undefined behavior of the program.
Refer to what I mentioned in the previous post. In line 12, 'chif' is declared as a local variable. In line 18, 'chif' is being used as an index of the pointer array -> resulting in unexpected behavior.

In this case the output is good when I implement the variable `int result` at the line 24
This works because the non-static variable is used after assigning a value.
6 Days Ago #4

P: 5
I begin to understand. How can I see the control flow? It seems that the control flow works differently in JavaScript for instance, in which the place of the variable is not relevant. I will relearn about local variable.
6 Days Ago #5

Post your reply

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