Need help in my array

S

sdlt85

This is part of my program but for some reason it is not taken the
value of c in the array P[n]. Can some one help me please.

int main()
{
int x, c, p;

cout<<"Enter the value for x: ";
cin>>x;
cout<<"How many coefficients? ";
cin>>c;

const int n = c-1;
int P[n]; // here are the errors: expected constant
expression,
//cannot allocate an
array of constant size 0, 'P': unknown size.
}

Thanks
 
J

Jon Slaughter

This is part of my program but for some reason it is not taken the
value of c in the array P[n]. Can some one help me please.

int main()
{
int x, c, p;

cout<<"Enter the value for x: ";
cin>>x;
cout<<"How many coefficients? ";
cin>>c;

const int n = c-1;
int P[n]; // here are the errors: expected constant
expression,
//cannot allocate an
array of constant size 0, 'P': unknown size.
}

You cannot allocate a static array with a dynamic size.


The compiler doesn't know what n is until after you run the program... so it
cannot allocate the space before hand... it doesn't know thatyou want a
dynamic array.

you need something more special:

int P[] = new int[c-1];

Then it should work.

Better yet you might want to look at STL vector or list... they are similar
but implemented in classes and have some other functionality

you can do that by

#include <vector>

vector<int> P;

and you don't have to worry about the size as it will deal with it
automatically.

to add elements though you need to use something like P.push_back(342);

Look up details on it for more info.
 
A

Andre Kostur

(e-mail address removed) wrote in

This is part of my program but for some reason it is not taken the
value of c in the array P[n]. Can some one help me please.

int main()
{
int x, c, p;

cout<<"Enter the value for x: ";
cin>>x;
cout<<"How many coefficients? ";
cin>>c;

const int n = c-1;
int P[n]; // here are the errors: expected
constant
expression,

Yep. Arrays must have a compile-time constant size. Yours does not. c
isn't known at compile time, thus c-1 isn't known, thus n isn't known.

You may wish to consider using std::vector<int>:
 
S

sumedh.....

yes,
Array is nothing but a pointer.. that means: int p[4]; can be
represented as *(p+i)
so another way of declaring it is
int *p=new int[n]; //and n doesnot need to be a constant..

thanks,
Sumedh
 
A

Andre Kostur

yes,
Array is nothing but a pointer.. that means: int p[4]; can be
represented as *(p+i)
so another way of declaring it is
int *p=new int[n]; //and n doesnot need to be a constant..

Not the same thing. The two allocations aren't necessarily from the same
location. In some implementations "int p[4]" only requires a stack pointer
to be moved. new int[4] requires a visit to a dynamic memory allocator
(generally slower).
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

yes,
Array is nothing but a pointer.. that means: int p[4]; can be
represented as *(p+i)
so another way of declaring it is
int *p=new int[n]; //and n doesnot need to be a constant..

Not the same thing. The two allocations aren't necessarily from the same
location. In some implementations "int p[4]" only requires a stack pointer
to be moved. new int[4] requires a visit to a dynamic memory allocator
(generally slower).

And it requires a manual deallocation, which makes it a bit more error
prone.
 
J

James Kanze

Array is nothing but a pointer..

Nonsense. Arrays and pointers are two completely different
things.
that means: int p[4]; can be
represented as *(p+i)

I think you mean that the expression p is the same as *(p+i).
This is a misfeature of C, and doesn't hold if you use C++ style
arrays (i.e. std::vector). And it only holds in expressions; it
has nothing to do with the type of any objects.
so another way of declaring it is
int *p=new int[n]; //and n doesnot need to be a constant..

There is never any reason to use array new in C++. The correct
way to declare an array is:
std::vector< int > array( n ) ;
About the only exception is when you need static initialization,
to avoid order of initialization issues.
 
S

sdlt85

Array is nothing but a pointer..

Nonsense. Arrays and pointers are two completely different
things.
that means: int p[4]; can be
represented as *(p+i)

I think you mean that the expression p is the same as *(p+i).
This is a misfeature of C, and doesn't hold if you use C++ style
arrays (i.e. std::vector). And it only holds in expressions; it
has nothing to do with the type of any objects.
so another way of declaring it is
int *p=new int[n]; //and n doesnot need to be a constant..

There is never any reason to use array new in C++. The correct
way to declare an array is:
std::vector< int > array( n ) ;
About the only exception is when you need static initialization,
to avoid order of initialization issues.

--
James Kanze (GABI Software) email:[email protected]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


Alright thanks ^_^
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,509
Members
48,193
Latest member
DannyRober

Latest Threads

Top