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

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

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.
Nov 7 '19 #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.

4 1763
dev7060
626 Expert 512MB
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.
Nov 8 '19 #2
kouty
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.
Nov 8 '19 #3
dev7060
626 Expert 512MB
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.
Nov 8 '19 #4
kouty
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.
Nov 8 '19 #5

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

Similar topics

2
by: John Ramsden | last post by:
I have a perl logging module that sends log messages, via post variables, to a simple PHP web app whose purpose is to write them to a database table. The module is organized in the standard...
4
by: Mark Wilson CPU | last post by:
This must be easy, but I'm missing something... I want to execute a Perl script, and capture ALL its output into a PHP variable. Here are my 2 files: -------------------------------------...
5
by: eagletender | last post by:
I am simply trying to pass a variable to another page. I know my second page can do Request("str") to retrieve that variable, but how do I pass it in the first place? I tried...
20
by: CoolPint | last post by:
While I was reading about const_cast, I got curious and wanted to know if I could modify a constant variable through a pointer which has been "const_cast"ed. Since the pointer would be pointing to...
14
by: Markus Dehmann | last post by:
The following if condition if((stream = fmemopen((void*)str, strlen(str), "r")) == NULL){ // ... } gives me a warning: assignment makes pointer from integer without a cast But only when I...
5
by: Afshar Mohebbi | last post by:
Hi everybody there, I've declared a <xsl:variable name="something" select="999"/> in my xslt and when I want to set another value in it (with same syntax) I get error the variable "something" has...
10
drhowarddrfine
by: drhowarddrfine | last post by:
warning: assignment makes pointer from integer without a cast I get that when I compile a C file where the function is defined as this: char **getvars() and the calling function has this...
11
by: mfglinux | last post by:
Hello to everybody I would like to know how to declare in python a "variable name" that it is in turn a variable In bash shell I would wrote sthg like: for x in `seq 1 3` do M$i=Material(x)...
1
by: hingwah | last post by:
Consider the following code:: void log_function(const char *str); // 3rd party logging function which don't use const std::string & as input class Test { public: std::string...
3
by: Bilbo0a | last post by:
I am trying to save a integer array variable to a file. I am looking for the easier way to do this then going through all items and converting them to a str and then saving them as a text file. ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.