Portál AbcLinuxu, 10. května 2025 05:27
int pole[x]
musí být x
konstantní výraz (spočitelný za překladu). To, že je C# napůl interpretovaný jazyk je věc jiná :-P
int* p = new int[n];nebo rovnou nepoužijete
std::vector
?
#include <iostream>>
using namespace std;
int main() {
int velikost;
cout << "Zadejte velikost: ";
cin >> velikost;
int pole[velikost];
for(int i = 0; i < velikost; ++i) {
cout << (i+1) << ". hodnota: ";
cin >> pole[i];
}
cout << "Vypis:" << endl;
for(int i = 0; i < velikost; ++i) {
cout << (i+1) << ". hodnota: " << pole[i] << endl;
}
return 0;
}
viz:$ ./pole
Zadejte velikost: 3
1. hodnota: 7
2. hodnota: 5
3. hodnota: 2
Vypis:
1. hodnota: 7
2. hodnota: 5
3. hodnota: 2
gcc version 3.3.5 (Debian 1:3.3.5-13)
-std=c99 -Wall
'. Že bych prozkoumal ty pokoutně stažené drafty norem?
Je ale pravděpodobné, že možnost deklarovat velikost pole výrazem, který není vyhodnotitelný při kompilaci, se objevila až v pozdějších verzích jazyka.1. In addition to optional type qualifiers and the keyword static, the [ and ] may delimit an expression or *. If they delimit an expression (which specifies the size of an array), the expression shall have an integer type. If the expression is a constant expression, it shall have a value greater than zero. The element type shall not be an incomplete or function type. The optional type qualifiers and the keyword static shall appear only in a declaration of a function parameter with an array type, and then only in the outermost array type derivation.
2. Only an ordinary identifier (as defined in 6.2.3) with both block scope or function prototype scope and no linkage shall have a variably modified type. If an identifier is declared to be an object with static storage duration, it shall not have a variable length array type.
int velikost;
int *pole;
velikost = rand() % 1000;
pole = new int[velikost];
// pracuji s polem
delete [] pole;
std::vector(JE_MENSI)vas_typ(JE_VETSI) jmeno_promene pole je potom samonafukovaci pokud pouzijete jmeno_promene.push_back(value); jmeno_promene.insert(value); nebo jako klasicke pole: if (! (index < jmeno_promene.size()) ) jmeno_promene.resize(index+1); jmeno_promene[index] = value; prochazet pole pak pomoci iteratoruvice o STL na www.builder.cz nebo primo sgi.com. je to rychle, elegantni a zpravidla dobre optimalizovane. VB
A ten nekdo zaroven absolutne nema sajn, jak to udelat:)To není ten problém, kdyby to věděl, tak se neptá. Potíž je v tom, že my ani nevíme co chce vlastně udělat.
Popište příznaky problému, nikoli vlastní dohadyV tomto případě platí na 110%.
— Jak se správně ptát
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.