473,394 Members | 2,052 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,394 software developers and data experts.

Dynamic arrays?

Why can't I write this?: int* array=new int[2]{1,010}; It keeps saying:

8 C:\Dev-Cpp\test6.cpp expected `,' or `;' before '{' token
9 C:\Dev-Cpp\test6.cpp invalid type argument of `unary *'

Any suggestions? Thanks!!!!

Dec 4 '05 #1
2 1720
Protoman wrote:
Why can't I write this?: int* array=new int[2]{1,010}; It keeps saying:

8 C:\Dev-Cpp\test6.cpp expected `,' or `;' before '{' token
9 C:\Dev-Cpp\test6.cpp invalid type argument of `unary *'

Any suggestions? Thanks!!!!


No, that is not C++ syntax.

int array[2] = {1, 010}; // == { 1, 8 }

If you want it to be dynamic -

#include <vector>

int array[2] = {1, 010}; // == { 1, 8 }

std::vector<int> darray( array, array + 2 );
Dec 4 '05 #2

"Protoman" <Pr**********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
| Why can't I write this?: int* array=new int[2]{1,010}; It keeps
saying:
|
| 8 C:\Dev-Cpp\test6.cpp expected `,' or `;' before '{' token
| 9 C:\Dev-Cpp\test6.cpp invalid type argument of `unary *'
|
| Any suggestions? Thanks!!!!
|

Ask yourself the following:

a) What does int* array do exactly?

answer: it *may* hold a pointer to a single integer.
So in the fragment:

int *p = new int(5);
delete p;

p is a pointer to a single integer. Not a container of integers.

b) What does
int array[2] = { 1, 10 };
do exactly?

answer: it creates a container of 2 integers initialized to 1 and 10
respectfully.

c) Since its now clear that a pointer to an integer can't do anything
else but potentially point to an integer, one can use the pointer to
"iterate" through a container of integer elements.

int array[2] = { 1, 10 };
int * p = array[0]; // p points to the first element
++p; // p now points to the second element: array[1]

So you can argue that setting the pointer to the container's first
element will naturally return the base address of the container. Except
that this is a rather dangerous statement since the grand majority of
containers are not organized in sequence in memory. In fact, the only
containers that are organized sequentially are:

primitive arrays
std::vector
a stack

(and thats a very short list of the available containers out there)

Hence, the C++ programmer makes a clear, unambiguous and critical
distinction between a pointer to a given element and a pointer (or
reference) to a given container.

Just cause you hold a pointer to a single car in a parking lot does not
give you the address of the parking lot.
Dec 4 '05 #3

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

Similar topics

3
by: meyousikmann | last post by:
The following code just sets up and fills a dynamic array of integers. #include <cstdlib> int main() { int* intArray = NULL; int count; count = 20;
4
by: Scott Lyons | last post by:
Hey all, Can someone help me figure out how to pass a dynamic array into a function? Its been giving me some trouble, and my textbook of course doesnt cover the issue. Its probably something...
3
by: genc ymeri | last post by:
Hi, What can I use in C# for dynamic arrays ???? I have some records (struts in ..Net) and want to store them in a dynamic "arrays" or object list. I noticed the in C# arrays' length can't be...
60
by: Peter Olcott | last post by:
I need to know how to get the solution mentioned below to work. The solution is from gbayles Jan 29 2001, 12:50 pm, link is provided below: >...
4
by: learnfpga | last post by:
Here is a little code I wrote to add the numbers input by the user.....I was wondering if its possible to have the same functionality without using dynamic arrays.....just curious..... ...
2
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs,...
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
1
by: KioKrofov | last post by:
Hello, I am trying to find out how Dynamic Arrays are actually stored in memory. Really, I want to know how Vectors are stored in memory, but I deduce they are stored the same way. If you...
2
by: headware | last post by:
Do dynamic arrays declared using ReDim have to be freed? I assume that if it's an array of dynamically created objects (e.g. Scripting.Dictionary), each one of those objects will have to be set to...
4
by: Sunny | last post by:
Hi, Is there a way in javascript to create Dynamic arrays or arrays on fly. Something Like: var "ptsgN"+sd = new Array(); Here sd is incrementing by 1. I have lots of data that I am...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.