473,509 Members | 10,100 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about arrays..

I'm sorta new to the C++ syntax.

I know that arrays can be delcared like so:
int a[]={1,2,3,4};

but what if I want the array to be a global, and then set the length
and value inside a method?

like in java, you can do:

int a[];
void main()
{
a=new int{1,2,3,4};
}

How do I do that in C++?

Jul 19 '06 #1
11 1183
In article <11*********************@m73g2000cwd.googlegroups. com>,
ne*************@gmail.com says...
I'm sorta new to the C++ syntax.

I know that arrays can be delcared like so:
int a[]={1,2,3,4};

but what if I want the array to be a global, and then set the length
and value inside a method?
You can't do it (not directly anyway). If you want to do that, you're
probably better off using an std::vector instead.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '06 #2
I see... I'll look that up, thanks alot

Jul 19 '06 #3
AB
like in java, you can do:
>
int a[];
void main()
{
a=new int{1,2,3,4};
}

How do I do that in C++?
No direct method, but you can try this..

int* a ;//global declaration

void method()
{
a = new int[size] ;
for(i = 0; i < size; i++)
a[i] = value ;
}

Jul 19 '06 #4

AB wrote:
like in java, you can do:

int a[];
void main()
{
a=new int{1,2,3,4};
}

How do I do that in C++?

No direct method, but you can try this..

int* a ;//global declaration

void method()
{
a = new int[size] ;
for(i = 0; i < size; i++)
a[i] = value ;
}
another way -

int* a;

void method()
{
int b[] = {1, 2, 3, 4};
a = new int [4];
memcpy(a, b, 4);
//think it's better for speed of performance
}//

Jul 19 '06 #5
AB schrieb:
>like in java, you can do:

int a[];
void main()
main returns an int:

int main()
>{
a=new int{1,2,3,4};
}

How do I do that in C++?

No direct method, but you can try this..

int* a ;//global declaration

void method()
{
a = new int[size] ;
for(i = 0; i < size; i++)
a[i] = value ;
}
Don't do it the Java way when you program C++. Where would you delete[]
the pointer?

Better would be a std::vector as Jerry suggests:

#include <vector>

std::vector<inta;

int main()
{
a.push_back(1);
a.push_back(2);
a.push_back(3);
a.push_back(4);

// or:
a.resize(4);
for (int i = 0; i < 4; i++)
a[i] = i+1;
}

--
Thomas
Jul 19 '06 #6
Thomas J. Gritzan wrote:
AB schrieb:
>>like in java, you can do:

int a[];
void main()

main returns an int:

int main()
>>{
a=new int{1,2,3,4};
}

How do I do that in C++?

No direct method, but you can try this..

int* a ;//global declaration

void method()
{
a = new int[size] ;
for(i = 0; i < size; i++)
a[i] = value ;
}

Don't do it the Java way when you program C++. Where would you
delete[] the pointer?

Better would be a std::vector as Jerry suggests:

#include <vector>

std::vector<inta;

int main()
{
a.push_back(1);
a.push_back(2);
a.push_back(3);
a.push_back(4);

// or:
a.resize(4);
for (int i = 0; i < 4; i++)
a[i] = i+1;
}
Why is 'a' *global*? Is there really *the need* for it to be global?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 19 '06 #7
Victor Bazarov schrieb:
Thomas J. Gritzan wrote:
>Don't do it the Java way when you program C++. Where would you
delete[] the pointer?

Better would be a std::vector as Jerry suggests:

#include <vector>

std::vector<inta;

int main()
{
a.push_back(1);
a.push_back(2);
a.push_back(3);
a.push_back(4);

// or:
a.resize(4);
for (int i = 0; i < 4; i++)
a[i] = i+1;
}

Why is 'a' *global*? Is there really *the need* for it to be global?
Because the OP wanted it:
but what if I want the array to be a global, and then set the length
and value inside a method?
I wonder how he got the global in Java to work...

--
Thomas
Jul 19 '06 #8
kiddler posted:
I'm sorta new to the C++ syntax.

I know that arrays can be delcared like so:
int a[]={1,2,3,4};

but what if I want the array to be a global, and then set the length
and value inside a method?

like in java, you can do:

int a[];
void main()
{
a=new int{1,2,3,4};
}

How do I do that in C++?

std::vector would probably be the way to go.

Or if you're feeling creative. . .

template<class T>
class Array {
private:

T *pNC;
unsigned lenNC;

public:

T * const p;

unsigned const &len;
Array() : p(pNC), len(lenNC) {}

void Allocate(unsigned const quantity)
{
lenNC = quantity;

pNC = new T[len];
}

void Deallocate()
{
delete [] pNC;
}
};
Array<inta;
int main()
{
a.Allocate(4);

a.p[0] = 1; a.p[1] = 2; a.p[2] = 3; a.p[3] = 4;
...
a.Deallocate();
}

--

Frederick Gotham
Jul 19 '06 #9
Frederick Gotham wrote:
kiddler posted:
>I'm sorta new to the C++ syntax.

I know that arrays can be delcared like so:
int a[]={1,2,3,4};

but what if I want the array to be a global, and then set the length
and value inside a method?

like in java, you can do:

int a[];
void main()
{
a=new int{1,2,3,4};
}

How do I do that in C++?


std::vector would probably be the way to go.

Or if you're feeling creative. . .

template<class T>
class Array {
private:

T *pNC;
unsigned lenNC;

public:

T * const p;

unsigned const &len;
Array() : p(pNC), len(lenNC) {}
Neither 'pNC' nor 'lenNC' are initialised. Initialising other members
with their values (indeterminate, BTW), is very dangerous.
>
void Allocate(unsigned const quantity)
{
lenNC = quantity;

pNC = new T[len];
This doesn't seem to do anything with 'p'. What is 'p' for, then?
}

void Deallocate()
{
delete [] pNC;
}
};
Array<inta;
int main()
{
a.Allocate(4);

a.p[0] = 1; a.p[1] = 2; a.p[2] = 3; a.p[3] = 4;
Undefined behaviour. You're dereferencing an uninitialised pointer.
>

...
a.Deallocate();
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 19 '06 #10
Victor Bazarov schrieb:
Frederick Gotham wrote:
>Or if you're feeling creative. . .

template<class T>
class Array {
private:

T *pNC;
unsigned lenNC;

public:

T * const p;

unsigned const &len;
Array() : p(pNC), len(lenNC) {}

Neither 'pNC' nor 'lenNC' are initialised. Initialising other members
with their values (indeterminate, BTW), is very dangerous.
> void Allocate(unsigned const quantity)
{
lenNC = quantity;

pNC = new T[len];

This doesn't seem to do anything with 'p'. What is 'p' for, then?
Maybe p is supposed to be a reference like len.
Doesn't change the fact that it's bad advice, and that there is no
destructor.
> }

void Deallocate()
{
delete [] pNC;
}
};
--
Thomas
Jul 19 '06 #11
Thomas J. Gritzan posted:
Maybe p is supposed to be a reference like len.
Doesn't change the fact that it's bad advice, and that there is no
destructor.

Yes, the code was unchecked, and I posted in haste.

"p" was supposed to be a reference.

--

Frederick Gotham
Jul 19 '06 #12

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

Similar topics

7
11757
by: csx | last post by:
Hi everyone! two quick questions relating to arrays. Q1, Is it possible to re-assign array elements? int array = {{2,4}, {4,5}}; array = {2,3}
19
1734
by: Method Man | last post by:
I understand that arrays and structs can't be passed by value into and out of functions since they can be arbitrarily big. My question is: Why are types allowed to be passed by value? Couldn't my...
35
2682
by: David Cleaver | last post by:
Hello all, I was wondering if there were some sort of limitations on the "if" statement? I'm writing a program which needs to check a bunch of conditions all at the same time (basically). And...
5
1441
by: Wajih-ur-Rehman | last post by:
The question is about C++ (since its the C family, i posted it on this newsgroup) Lets say i declare an array int a = {1,2,3,4}; int *p = a; //This is allowed because "a" returns the address of...
3
2550
by: James dean | last post by:
I have created algorithms in C# unsafe code and have fixed the arrays in memory for optimum performance. I use multidimensional arrays rather than jagged arrays. The algorithms i use usually read a...
21
3168
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
24
3403
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
7
6410
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
2
4891
by: phpCodeHead | last post by:
Hello fellow codemeisters! I am needing to parse through two lists of serial numbers for parts being received into an inventory database. First, is the list of serial numbers already in the...
31
1867
by: mdh | last post by:
I am still having a problem understanding K&RII on p 112. I have looked at the FAQs --which I am sure answer it in a way that I have missed, so here goes. A 2-dim array, (per K&R) is really a...
0
7137
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
7347
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,...
0
7416
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7073
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
7506
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...
1
5062
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4732
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3218
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
779
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.