can I declare an array without declaring it's size in static code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "stdafx.h"
#include <iostream>

using namespace std;

void printuda(int myarray[], int maa);

int main()
{
	int udam = 1;
	cout << "Input amount of elements of array." << endl;
	cin >> udam;
	int uda[udam];

	for(int fff = 0;fff<udam;fff++)
	{
		cout << "input " << fff << " value of array. " << endl;
		cin >> uda[fff];
	};

	printuda(uda, udam);


	cout << endl << " The end of code. " << endl;
	cin.get();
	cin.get();
	return 0;
};

void printuda(int myarray[], int maa)
{
	for(int x = --maa;x>=0;x--,x--)
	{
		cout << x << "   " << myarray[x] << endl;
	};

};


1>------ Build started: Project: testprograms, Configuration: Debug Win32 ------
1>  testprograms.cpp
1>testprograms.cpp(13): error C2057: expected constant expression
1>testprograms.cpp(13): error C2466: cannot allocate an array of constant size 0
1>testprograms.cpp(13): error C2133: 'uda' : unknown size
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


I can't do that or find away around it? line 13
if my question doesn't make sense tell me why if i'm using incorrect terminology or if i should just elaborate more on what my question is
Last edited on
As the compiler reported the size of an array shall be known at compile time. You may do so only in C that satisfies ths C99 standard. In C++ use std::vector instead.
You can't, you can only declare an array with out declaring it's size in dynamic code that utilizes pointers.
Interesting choice of index name/future compilation error.
Hahahaha I just use that in place of x or something for generic variable names sometimes.

Anyway could some one show me an example on how I could achieve declaration of a array's after compile time with out a constant element size
You can dynamically allocate an array in the heap.

1
2
3
4
5
6
int udam1;

cout << "Input amount of elements of array." << endl;
cin >> udam;

int *uda =  new int[udam];
...but that would be a bad idea for a variety of reasons.
That's how you create a vector with a specific number of initial elements:
vector<int> uda(udam);
Topic archived. No new replies allowed.