int main(){
int x=10;
int valor[x];
}
It’s the same thing as doing
int main(){
int valor[10];
}
Or using define
// #define x ValorPredefinido
#define x 10 /** EXEMPLO, caso saiba que será esse valor **/
int main(){
int valor[x];
}
From the codes below we do not know how much will be needed at the outset.
int main(){
int x;
printf("Qual o tamanho do vetor?");
scanf("%d", &x); /** caso seja um valor que o usuário terá de colocar**/
int valor[x];
}
Another option is to use dynamic memory.
int main(){
int *valor;
}
Go allocating memory as you need more space.
- What you can’t do is
int valor[x]
without having a value, in the examples
up when it comes to that line x
will have a value
Any specific difficulties? Know how to use
scanf()
?– Maniero
scanf("%d", &x);
and then defines theint valor[x]
– Fábio Morais
@Fábiomoral and in case I set using define, how would I choose a value for it? to facilitate code maintenance?
– Vitor Gonçalves
(edited the question for better viewing)
– Vitor Gonçalves
What you did didn’t make sense,
#define NUMERO 10
– Fábio Morais
On an average , I want you to calculate 10 numbers , I have to tell the vector that I want 10 right? , I tried to play in the scanf but I’m not getting:
– Vitor Gonçalves
I think the question boils down to "how do I read a C number?" or do I not understand the question ?
– Isac
@Isac no, before that, I want to define a quantity that the user chooses for the vector , I tried to use the scanf and play for the vector and could not.
– Vitor Gonçalves
So you already create a vector with a specific size
int valor[NUMERO];
If you can read a number, just put the variable you read where it isNUMERO
. Maybe it’s confusion about the vector creation syntax not ? That maybe you haven’t fully realized yet– Isac