C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Question about arrays..

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
kiddler
Guest





PostPosted: Wed Jul 19, 2006 7:54 am    Post subject: Question about arrays.. Reply with 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?

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





PostPosted: Wed Jul 19, 2006 8:05 am    Post subject: Re: Question about arrays.. Reply with quote



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





PostPosted: Wed Jul 19, 2006 8:36 am    Post subject: Re: Question about arrays.. Reply with quote



I see... I'll look that up, thanks alot
Back to top
chriskr7
Guest





PostPosted: Wed Jul 19, 2006 9:11 am    Post subject: Re: Question about arrays.. Reply with quote

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





PostPosted: Wed Jul 19, 2006 9:11 am    Post subject: Re: Question about arrays.. Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.