473,387 Members | 3,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

Jun 6 '07 #1
7 1207

<sd****@gmail.comwrote in message
news:11*********************@q75g2000hsh.googlegro ups.com...
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<intP;

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.
Jun 6 '07 #2
sd****@gmail.com wrote in
news:11*********************@q75g2000hsh.googlegro ups.com:
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>:

std::vector<intP(n);
//cannot allocate an
array of constant size 0, 'P': unknown size.
}

Thanks

Jun 6 '07 #3
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

Jun 6 '07 #4
"sumedh....." <su**********@gmail.comwrote in
news:11*********************@i13g2000prf.googlegro ups.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).
Jun 6 '07 #5
On 2007-06-06 09:11, Andre Kostur wrote:
"sumedh....." <su**********@gmail.comwrote in
news:11*********************@i13g2000prf.googlegro ups.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 Wikström
Jun 6 '07 #6
On Jun 6, 8:07 am, "sumedh....." <sumedhsak...@gmail.comwrote:
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:ja*********@gmail.com
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

Jun 6 '07 #7
On Jun 6, 5:07 am, James Kanze <james.ka...@gmail.comwrote:
On Jun 6, 8:07 am, "sumedh....." <sumedhsak...@gmail.comwrote:
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 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 ^_^

Jun 7 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Nathan Rose | last post by:
Here's my problem. I am reading from a text file using this: if (!file_exists($file)) { echo "Don't exist\n"; return false; } $fd = @fopen($file, 'r'); if (!is_resource($fd))
4
by: KellyH | last post by:
Hi, I hope someone can point me in the right direction. I'll get it out of the way: Yes, I am a college student. No, I am not looking for anyone to do my homework, just looking for help. I have...
1
by: Daniel | last post by:
By using this code I made an array from a hashed array to regain a table content and layout as received from a mysql database... my $sth = $dbh->prepare("SELECT * FROM `Groepen';");...
3
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
48
by: Chad Z. Hower aka Kudzu | last post by:
A few of you may recognize me from the recent posts I have made about Indy <http://www.indyproject.org/indy.html> Those of you coming to .net from the Delphi world know truly how unique and...
2
by: Thomas Connolly | last post by:
Anyone know if there is a C# equivallent to: enum { LIFFE_SIZE_AUTOMARKETREF = 15 }; typedef char LiffeAutoMarketReference ; Thanks,
23
by: vinod.bhavnani | last post by:
Hello all, I need desperate help Here is the problem: My problem today is with multidimensional arrays. Lets say i have an array A this is a 4 dimensional static array.
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
5
by: SpreadTooThin | last post by:
If you are deriving a new class from another class, that you must (I assume) know the initializer of the other class. So in myClass import array class myClass(arrary.array): def...
20
by: Martin Jørgensen | last post by:
Hi, I'm reading a number of double values from a file. It's a 2D-array: 1 2 3 4 5 6 7 ------------- 1 3.2 2 0 2.1 3 9.3 4
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.