 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
kiddler Guest
|
Posted: Wed Jul 19, 2006 7:54 am Post subject: 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++? |
|
| Back to top |
|
 |
Jerry Coffin Guest
|
Posted: Wed Jul 19, 2006 8:05 am Post subject: Re: Question about arrays.. |
|
|
In article <1153277653.837594.33480 (AT) m73g2000cwd (DOT) googlegroups.com>,
nevarine.legend (AT) gmail (DOT) com says...
| Quote: | 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. |
|
| Back to top |
|
 |
kiddler Guest
|
Posted: Wed Jul 19, 2006 8:36 am Post subject: Re: Question about arrays.. |
|
|
I see... I'll look that up, thanks alot |
|
| Back to top |
|
 |
chriskr7 Guest
|
Posted: Wed Jul 19, 2006 9:11 am Post subject: Re: Question about arrays.. |
|
|
AB wrote:
| Quote: | 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
}// |
|
| Back to top |
|
 |
AB Guest
|
Posted: Wed Jul 19, 2006 9:11 am Post subject: Re: Question about arrays.. |
|
|
| Quote: | 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 ;
} |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|