Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C++ Programming

Need help in my array


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

<sdl@gmail.com> wrote in message

news:1181092218.606289.19590@q75g2000hsh.googlegroups.com...

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.

sdl@gmail.com wrote in
news:1181092218.606289.19590@q75g2000hsh.googlegroups.com:

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>:

        std::vector<int> P(n);

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

"sumedh....." <sumedhsak@gmail.com> wrote in
news:1181110045.600222.47530@i13g2000prf.googlegroups.com:

> 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).
On 2007-06-06 09:11, Andre Kostur wrote:

> "sumedh....." <sumedhsak@gmail.com> wrote in
> news:1181110045.600222.47530@i13g2000prf.googlegroups.com:

>> 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.

--
Erik Wikstrm

On Jun 6, 8:07 am, "sumedh....." <sumedhsak@gmail.com> wrote:

> 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[i] 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:james.ka@gmail.com
Conseils en informatique oriente objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34

On Jun 6, 5:07 am, James Kanze <james.ka@gmail.com> wrote:

Alright  thanks ^_^
Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc