C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 15th, 2004, 11:48 AM
Xquisite Xquisite is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 1 Xquisite User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Arrow Creating an Array on the fly

Hellow,

I am new to the C enviroment. I need to create a row matrix (array)
on the fly. That is when I pass on the number of columns n into the
program through argv[], I should be able to obtain the row matrix
consists of n columns like [x1 x2 x3 x4 ------xn] as an output file.
I would like to keep these as variables in terms of x, instead of
actual numbers. So these x's are in fact integers.

I would manipulate this matrix later on by finding its transpose and
multiplying with another matrix to get equation in terms of x1, x2 etc.

Can any of you throw some idea!!!

Reply With Quote
  #2  
Old July 15th, 2004, 03:50 PM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
Once you convert the argv value into an integer you can use it to create a dynamic array using dynamic memory allocation functions such as malloc and calloc.

For example if you have the number of items in x -

int *array = calloc (x, sizeof (int));

-KM-

Reply With Quote
  #3  
Old August 10th, 2004, 01:12 AM
arash_partow arash_partow is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 5 arash_partow User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hello,

actually a more correct way of doing it is like this:

int *array = NULL;

if ( (array = calloc (x, sizeof (int))) == NULL)
{
/* allocation failed! */
exit(1);
}


and in c++ its done like this:

if ( (array = new int[x]) == NULL)
{
/* allocation failed! */
exit(1);
}


Arash Partow
__________________________________________________
Be one who knows what they don't know,
Instead of being one who knows not what they don't know,
Thinking they know everything about all things.
http://www.partow.net

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Creating an Array on the fly


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway