Reputation: 77
Lets say they input r = (i == 16)
, how can I take this and then run that for the output?
This is the error I am getting.
G:\Code\Tutorial 3\main.c|104|error: assignment to expression with array type
Code
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main()
{
int answer;
char operation[20];
int i = 16;
int j = 32;
int k = 8;
bool r;
printf("Input an operation:\n");
fgets(operation, sizeof(operation), stdin);
operation = answer;
if (answer == 1)
printf("The operation you entered was true.");
else
printf("The operation you entered was false.");
}
Upvotes: 0
Views: 125
Reputation: 15032
With this expression operation = answer;
you are trying to assign an (uninitialised) int
value to a pointer to an char
array.
While this expression is completely invalid on its own, note that you always should initialize variables before using them in any way. Otherwise they have just a random "garbarge" value in it, which you use later in the program and can cause much more errors.
That is what causing the error message you are pointing to.
Beside this, your program has much more issues to concern about.
What you want to do actually with this program?
What is the purpose of answer
? Why answer
doesn´t get a value assignd to?
And why do you want to assign no certain value of answer
to the array?
Furthermore,
if (answer == 1)
printf("The operation you entered was true.");
else
printf("The operation you entered was false.");
is also crazy, because answer
doesn´t get a value before, so you can´t make a condition with it.
Upvotes: 0
Reputation: 146
As other users said, you could be programming previously accepted operation. And then, if you want to execute special commands, you could use the exec
family inside created childs with fork
.
Upvotes: 0
Reputation: 3797
If I understand you correctly, you want to allow the user to enter a string which contains a piece of C code. In your example the input string is r = (i == 16)
.
Then, you want to execute whatever piece of C code the user entered. If the entered C code contains any variables (r
and i
in your example) you want to use the values of actual variables in your program.
Because C is a compiled language this is extremely difficult (I would say nearly impossible) to achieve. You would have to write a compiler to compile the entered C code to machine code. And even then, it would be extremely difficult to give the generated machine code access to the context (e.g. variables) in your running program.
No offense intended, but that kind of exercise is well beyond the level you are at. If this comes from an assignment, this is not what your teacher wants you to do.
PS 1: If the input is not an arbitrary piece of C code, but just an expression with a limited set of operators, the task becomes simpler.
PS 2: In more dynamical languages such as Python, this is much much easier to achieve (look, for example, at the Python eval
statement). If you have flexibility in the choice of your programming language, using something like Python could be a better approach.
Upvotes: 4
Reputation: 2583
Short answer: you can't. C is a compiled language, and unlike interpreted languages it can't evaluate dynamically set strings as commands.
What you can do is create some subset of allowed operations and evaluate them manually. Something like that:
if (operation[0] == 'i' and operation[1] == '=' and operation[2] == '=')
{
answer = (i == atoi(&operation[3]));
} else if (...)
...
Upvotes: 2