Hi, I am learning c language and want to understand a behavior of the language regarding where to inplement variables. -
#include <stdio.h>
-
-
//Compiler version gcc 6.3.0
-
/*On declare 4 fonctions*/
-
int ad (int num1, int num2);
-
int sub (int num1, int num2);
-
int mul (int num1, int num2);
-
int div (int num1, int num2);
-
-
int main()
-
{
-
int chif, num1, num2;
-
int(*op[4])(int num1, int num2);
-
op[0]=ad;
-
op[1]=sub;
-
op[2]=mul;
-
op[3]=div;
-
int result=op[chif](num1, num2);
-
-
printf("Choisissez deux nombres separes par enter\n");
-
scanf("%d%d", &num1, &num2);
-
printf("choisissez un chiffre entre 0 et 3 pour +-*/ \n");
-
scanf("%d", &chif);
-
-
printf("le reultat est %d\n", result);
-
return 0;
-
}
-
-
int ad (int x, int y)
-
{
-
return (x+y);
-
}
-
int sub (int x, int y)
-
{
-
return(x-y);
-
}
-
int mul (int x, int y)
-
{
-
return(x*y);
-
}
-
int div (int x, int y)
-
{
-
return(x/y);
-
}
-
/*Outputs
-
Choisissez deux nombres separes par enter
-
1
-
2
-
choisissez un chiffre entre 0 et 3 pour +-*/
-
-
le reultat est -1931796686
-
-
Process finished.
-
*/
-
-
-
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.
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
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.
@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.
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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:
-------------------------------------...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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.
...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
| |