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 February 21st, 2005, 11:33 PM
JediArmadillo JediArmadillo is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 2 JediArmadillo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 25 sec
Reputation Power: 0
dynamically allocate both dimensions of a 2d array

I need to know how to dynamically allocate both dimensions of a 2d array such as array[row][col]
but I do know know how to correctly do this as well as how to store info into spaces. so far I have tried...

main(){
char *array;
int row, col, rowInput, colInput;

printf("blah blah rows"\n);
scanf("%d", &rowInput);
printf("blah blah cols"\n);
scanf("%d", &colInput);

array = calloc(row*col*sizeof(char*));

for(row=0; row<rowInput; row++){
for(col=0; col<colInput; col++){
array[row][col] = '0;'
}
}
return 0;
}

Reply With Quote
  #2  
Old February 21st, 2005, 11:58 PM
BoolBooB BoolBooB is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 36 BoolBooB User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 35 m 42 sec
Reputation Power: 4
Look at my reply here:
http://forums.devarticles.com/t11075/s.html

The C function allocateMatrix() will dynamically allocate memory for a two dimensional array of ints. In this example both dimensions are the same, but the code could easily be modifed to allow a different dimension for rows and collumns. The code could also be easily modifed to create an array of floats, chars, etc. There's also a sniplet from main that shows you how to declare the pointers in main and how to call allocateMatrix().

Let me know where to send the bill.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > dynamically allocate both dimensions of a 2d array


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 3 hosted by Hostway
Stay green...Green IT