Apolyton Archive  |  Preserved copy of the Apolyton Civilization Site and its forums as they stood in September 2005. Read-only; nothing here can be posted to or replied to.  |  Forum index |  About this archive |  The 1998–2001 UBB forums
Today on Apolyton WARDELL INTERVIEW PROMO A.C.S. HISTORY CHAPTER 4 GET CIV4 /w FREE PLUS! A.C.S. PHOTO GALLERY GET A.O.M. V1.1
Apolyton Civilization Forums
main| civ2| civ3| civ4| smac| ctp2| ron| moo3| galciv| galciv2| alt| about|
ApolytonPLUS | register | search | faq | new posts | pm (-/-) | upload | members
hall of fame new! | civgroups | civgroups news | interviews | the column | radio | chat | directory | news | store | PLUS
Apolyton Civilization Forums : Powered by vBulletin version 2.0.3 Apolyton Civilization Forums > Miscellaneous > Off-Topic > pointers to multidimensional arrays in C
Show a Printable Version | Email This Page to Someone! | Receive updates to this thread | Report this to Apolyton news!
20.Sep: FOURTH CHAPTER IN `HISTORY OF...` PUBLISHED
13.Sep: NEWS ON THE FLY
06.Sep: APOLYTON DONATES TO HURRICANE RELIEF

bottom of page
   - CIVILIZATION 3 $9.99 - CIVILIZATION 2 from $6.95 - CIVILIZATION 2 from $4.25 - ALPHA CENTAURI + ALIEN CROSSFIRE (laptop collection) from $19.99 - ALPHA CENTAURI PC $9.99 -->
Author
Thread   
Pages (2): [ 1   2   ]
< Last Thread     Next Thread > Post New Thread     Post A Reply
Kuciwalker is offline Kuciwalker
Emperor
of Schmooism
Feb 2001
time: 00:24
  Old Post 14-02-2005 23:48
Edit/Delete Message Reply w/Quote
#1 Report this post to a moderator
pointers to multidimensional arrays in C Enter the AD-FREE zone

I have an assignment to write a program that does a bunch of crap with matrices in C. One thing I have to do is randomize the contents of a matrix. I want to do this by copying random data from memory into the matrix (yes, I know there are other ways, but I'm not using them). So I have to create a pointer to an int[100][100] array, and then use malloc. I've tried declaring this as int temp[100][100]*, but the compiler gives me an error. I don't get an error if I declare it as int * temp[100][100], but I'm not sure if that's a point to a matrix or a matrix of pointers. Moreover, I have to cast malloc to a pointer to an int[100][100], but the compiler doesn't like temp = (int[100][100]*) malloc(40000), either.

KrazyHorse is offline KrazyHorse
King
Macedonia
May 2001
time: 00:24
  Old Post 14-02-2005 23:51 Visit KrazyHorse's homepage!
Edit/Delete Message Reply w/Quote
#2 Report this post to a moderator
Help yourself to an AD-FREE life

Whoha is offline Whoha
King
The TOC is supposed to be classified guys...
Dec 2001
time: 23:24
  Old Post 14-02-2005 23:53
Edit/Delete Message Reply w/Quote
#3 Report this post to a moderator
Remove this text

memory allocation will kick your butt real hard.
use the other ways you bozo.

Kuciwalker is offline Kuciwalker
Emperor
of Schmooism
Feb 2001
time: 00:24
  Old Post 14-02-2005 23:56
Edit/Delete Message Reply w/Quote
#4 Report this post to a moderator
Support Apolyton

NO.

Kuciwalker is offline Kuciwalker
Emperor
of Schmooism
Feb 2001
time: 00:24
  Old Post 14-02-2005 23:56
Edit/Delete Message Reply w/Quote
#5 Report this post to a moderator
Increase the size of your Attachments

quote:
Originally posted by KrazyHorse


I might as well find out how to do it now, even if it's an inefficient and more difficult way of doing things

Richelieu is offline Richelieu
Warlord
Gatineau
Dec 2001
time: 00:24
  Old Post 14-02-2005 23:57
Edit/Delete Message Reply w/Quote
#6 Report this post to a moderator
Full PM-box? Change here!

OMFG! Pointers!!!! NOOOOOOOOOOOOOOOOOO!!!!

Whoha is offline Whoha
King
The TOC is supposed to be classified guys...
Dec 2001
time: 23:24
  Old Post 14-02-2005 23:59
Edit/Delete Message Reply w/Quote
#7 Report this post to a moderator
Got spare money?

quote:
Originally posted by Richelieu
OMFG! Pointers!!!! NOOOOOOOOOOOOOOOOOO!!!!


it is good to learn how to use pointers, but using malloc or new just for the hell of it is wrong.

KrazyHorse is offline KrazyHorse
King
Macedonia
May 2001
time: 00:24
  Old Post 15-02-2005 00:00 Visit KrazyHorse's homepage!
Edit/Delete Message Reply w/Quote
#8 Report this post to a moderator
Support Apolyton, buy Civilization III: Complete

quote:
Originally posted by Kuciwalker


I might as well find out how to do it now, even if it's an inefficient and more difficult way of doing things


I'm not laughing at the way you're doing it. I'm laughing at the fact that you have to do it and I don't.

God, I hated learning to program.

When my **** wouldn't compile I used to randomly insert &s and *s until it did.

Asher is offline Asher
King
Calgary, Alberta
Nov 1999
time: 22:24
  Old Post 15-02-2005 00:01 Visit Asher's homepage!
Edit/Delete Message Reply w/Quote
#9 Report this post to a moderator
Support Apolyton buy from Amazon

...

quote:
One thing I have to do is randomize the contents of a matrix.


code:
#include <stdio.h> #include <sys/types.h> #include <time.h> int main() { time_t t; int i, j; long arr[100][100]; time(&t); srand48((long) t); // set seed to current time for(i=0; i < 100; i++) for(j=0; j < 100; j++) arr[i][j] = lrand48(); return 0; }

Richelieu is offline Richelieu
Warlord
Gatineau
Dec 2001
time: 00:24
  Old Post 15-02-2005 00:02
Edit/Delete Message Reply w/Quote
#10 Report this post to a moderator
Support Apolyton, buy GURPS/ Alpha Centauri

quote:
Originally posted by Whoha
it is good to learn how to use pointers


I did. Now I've forgotten and i don't miss them. Hated them.

Kuciwalker is offline Kuciwalker
Emperor
of Schmooism
Feb 2001
time: 00:24
  Old Post 15-02-2005 00:05
Edit/Delete Message Reply w/Quote
#11 Report this post to a moderator
Enter the AD-FREE zone

quote:
Originally posted by Whoha
it is good to learn how to use pointers, but using malloc or new just for the hell of it is wrong.


There is no "new" in C.

@Asher: I steadfastly refuse to use the random number generator.

KrazyHorse is offline KrazyHorse
King
Macedonia
May 2001
time: 00:24
  Old Post 15-02-2005 00:05 Visit KrazyHorse's homepage!
Edit/Delete Message Reply w/Quote
#12 Report this post to a moderator
Browse Apolyton AD-FREE

Using time seeds is frowned upon in scientific computing. There are several much more elegant solutions which have the additional benefit of being reproducible and which do not run the risk of accidental convergence between runtime of the program and the seeding.

Asher is offline Asher
King
Calgary, Alberta
Nov 1999
time: 22:24
  Old Post 15-02-2005 00:06 Visit Asher's homepage!
Edit/Delete Message Reply w/Quote
#13 Report this post to a moderator
Support Apolyton, buy Civilization: The Boardgame

quote:
Originally posted by KrazyHorse
Using time seeds is frowned upon in scientific computing. There are several much more elegant solutions which have the additional benefit of being reproducible and which do not run the risk of accidental convergence between runtime of the program and the seeding.

Are they as simple to do in stupid applications such as this?

Kuciwalker is offline Kuciwalker
Emperor
of Schmooism
Feb 2001
time: 00:24
  Old Post 15-02-2005 00:06
Edit/Delete Message Reply w/Quote
#14 Report this post to a moderator
Support Apolyton, buy Civilization: The Boardgame

Since it's prompted by user input there, it's not a problem.

Provost Harrison is offline Provost Harrison
Emperor
The farce is strong with this one.
Feb 2000
time: 05:24
  Old Post 15-02-2005 00:06
Edit/Delete Message Reply w/Quote
#15 Report this post to a moderator
Support Apolyton, buy Civilization III: Complete

Welcome to geek central!

KrazyHorse is offline KrazyHorse
King
Macedonia
May 2001
time: 00:24
  Old Post 15-02-2005 00:07 Visit KrazyHorse's homepage!
Edit/Delete Message Reply w/Quote
#16 Report this post to a moderator
Got spare money?

Yes, if you have the library of programs contained in "Numerical Recipes in C" available...



I believe the syntax was ran4(-longint)

Ari Rahikkala is offline Ari Rahikkala
King
Unreported loss of streaming in atomic read operation
Oct 1999
time: 07:24
  Old Post 15-02-2005 00:08 Visit Ari Rahikkala's homepage!
Edit/Delete Message Reply w/Quote
#17 Report this post to a moderator
Support Apolyton buy from Amazon

Pointers to multidimensional arrays? Congratulations, you might just be joining the (small) ranks of the three star programmers if you play your cards right (wrong) .

Whoha is offline Whoha
King
The TOC is supposed to be classified guys...
Dec 2001
time: 23:24
  Old Post 15-02-2005 00:09
Edit/Delete Message Reply w/Quote
#18 Report this post to a moderator
Enter the AD-FREE zone

quote:
Originally posted by Kuciwalker


There is no "new" in C.

@Asher: I steadfastly refuse to use the random number generator.


meh, I'm speaking of memory allocation in general. by the way I think I just did what you are asking how to do, the memory was zeroed though.

Asher is offline Asher
King
Calgary, Alberta
Nov 1999
time: 22:24
  Old Post 15-02-2005 00:10 Visit Asher's homepage!
Edit/Delete Message Reply w/Quote
#19 Report this post to a moderator
Support Apolyton, buy Galactic Civilizations

quote:
Originally posted by Kuciwalker
@Asher: I steadfastly refuse to use the random number generator.

I'm trying to help you steer clear of horrible programming practices.

KrazyHorse is offline KrazyHorse
King
Macedonia
May 2001
time: 00:24
  Old Post 15-02-2005 00:14 Visit KrazyHorse's homepage!
Edit/Delete Message Reply w/Quote
#20 Report this post to a moderator
Suffering from ads?

SO instead of using a portable number generator you're using some bass-ackwards way?

Stupid.

Asher is offline Asher
King
Calgary, Alberta
Nov 1999
time: 22:24
  Old Post 15-02-2005 00:23 Visit Asher's homepage!
Edit/Delete Message Reply w/Quote
#21 Report this post to a moderator
Put an end to popups!

Here's how I'd declare a multi-dimensional array with dynamic allocation and pointers...

code:
void allocateMatrix(int ***pMatrix, int size) { int i, j; *pMatrix = (int **) calloc (size, sizeof(int)); **pMatrix = (int *) calloc (size * size, sizeof(int)); for (i = 0, j = 0; i < size; i++, j += size) /* init array indices */ { *(*pMatrix + i) = (**pMatrix + j); } } int main() { int **pMatrix; allocateMatrix(&pMatrix,100); return 0; }

Last edited by Asher on 15-02-2005 at 00:29

St Leo is offline St Leo
King of Scenario League
Member of the Apolyton Social Democratic Party
Jul 2005
time: 00:24
  Old Post 15-02-2005 00:31 Visit St Leo<br><img src=/forums/images/staff-icon.gif>'s homepage!
Edit/Delete Message Reply w/Quote
#22 Report this post to a moderator
Get a bigger avatar today!

quote:
Originally posted by Kuciwalker
I have an assignment to write a program that does a bunch of crap with matrices in C. One thing I have to do is randomize the contents of a matrix. I want to do this by copying random data from memory into the matrix (yes, I know there are other ways, but I'm not using them). So I have to create a pointer to an int[100][100] array, and then use malloc. I've tried declaring this as int temp[100][100]*, but the compiler gives me an error. I don't get an error if I declare it as int * temp[100][100], but I'm not sure if that's a point to a matrix or a matrix of pointers. Moreover, I have to cast malloc to a pointer to an int[100][100], but the compiler doesn't like temp = (int[100][100]*) malloc(40000), either.


Dude, C arrays aren't objects or types. The array's name is a pointer to the zeroth element in the array. Square brackets are a convenience notation.

Zeroth of all, the following two lines are equivalent:
array[cell] = 1;
*(array + cell) = 1;


When you say array[cell], you are telling the compiler to follow the array pointer, advance cell sizeof()s beyond it, and do whatever.

First of all, the following to lines are equivalent:
array[row][col] = 1;
*(*(array+ row) + col) = 1;


That's right. Multi-dimensional arrays are just a hack job on top of the hack job.

The code that you want is probably:

int **p; // hi, I am a two-dimensional array
p = (int**)malloc(100 * 100 / sizeof(int)); // use me, baby

int ***a; // hi, I am the address of the two dimensional address
a = &p; // functions can now change the array by reference

void doStuff(int ***array, int row, int col) {
array[3][4] = 3;
}

doStuff(a, 100, 100);
// element at [3][4] is now 3
doStuff(&p, 100, 100);
// element at [3][4] is again 3


This is why I ****ing hate C.

Kuciwalker is offline Kuciwalker
Emperor
of Schmooism
Feb 2001
time: 00:24
  Old Post 15-02-2005 01:26
Edit/Delete Message Reply w/Quote
#23 Report this post to a moderator
Support Apolyton, buy Call to Power 2

quote:
Originally posted by KrazyHorse
SO instead of using a portable number generator you're using some bass-ackwards way?

Stupid.


It's equivalent to finding out what'll happen if you hook a reflector up to a nightlight that turns off when light hits a sensor on it...

Kuciwalker is offline Kuciwalker
Emperor
of Schmooism
Feb 2001
time: 00:24
  Old Post 15-02-2005 01:27
Edit/Delete Message Reply w/Quote
#24 Report this post to a moderator
Got spare money?

quote:
Originally posted by Whoha
meh, I'm speaking of memory allocation in general. by the way I think I just did what you are asking how to do, the memory was zeroed though.


malloc doesn't zero memory, according to man malloc.

Ari Rahikkala is offline Ari Rahikkala
King
Unreported loss of streaming in atomic read operation
Oct 1999
time: 07:24
  Old Post 15-02-2005 01:34 Visit Ari Rahikkala's homepage!
Edit/Delete Message Reply w/Quote
#25 Report this post to a moderator
Support Apolyton, buy Call to Power 2

calloc, then .

Kuciwalker is offline Kuciwalker
Emperor
of Schmooism
Feb 2001
time: 00:24
  Old Post 15-02-2005 01:34
Edit/Delete Message Reply w/Quote
#26 Report this post to a moderator
Inflate your Upload Space

I don't want to zero the memory.

Asher is offline Asher
King
Calgary, Alberta
Nov 1999
time: 22:24
  Old Post 15-02-2005 01:36 Visit Asher's homepage!
Edit/Delete Message Reply w/Quote
#27 Report this post to a moderator
Support Apolyton buy from Amazon

quote:
Originally posted by Kuciwalker
I don't want to zero the memory.

What you are doing is fundamentally evil!

Kuciwalker is offline Kuciwalker
Emperor
of Schmooism
Feb 2001
time: 00:24
  Old Post 15-02-2005 01:38
Edit/Delete Message Reply w/Quote
#28 Report this post to a moderator
Remove this text

Then I'm . Big deal

Whoha is offline Whoha
King
The TOC is supposed to be classified guys...
Dec 2001
time: 23:24
  Old Post 15-02-2005 01:41
Edit/Delete Message Reply w/Quote
#29 Report this post to a moderator
Support Apolyton

quote:
Originally posted by Kuciwalker


malloc doesn't zero memory, according to man malloc.


cc was going on implicit function definitions, so perhaps that was it.

quote:
Originally posted by Kuciwalker
I don't want to zero the memory.


Why not though? isn't all zeros a random possibility?

Kuciwalker is offline Kuciwalker
Emperor
of Schmooism
Feb 2001
time: 00:24
  Old Post 15-02-2005 01:45
Edit/Delete Message Reply w/Quote
#30 Report this post to a moderator
Support Apolyton, buy Alpha Centauri

I'm using data in memory for random numbers. And St Leo answered my question.

 
Pages (2): [ 1   2   ]
< Last Thread     Next Thread > Post New Thread     Post A Reply
All times are GMT. The time now is 05:24.
Apolyton Time is 00:24.
    top of page
Rate This Thread:
Forum Jump:
Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is ON
vB code is ON
Smilies are ON
[IMG] code is ON
 




Contact Us - Apolyton Civilization Site - Support Us!

Building a better Apolyton through better information. Click here and take our poll!
Non-US visitors, click here!

Powered by: vBulletin Version 2.0.3
Copyright ©2000, 2001, Jelsoft Enterprises Limited.

Page generated in 0.0777 seconds (92.65% PHP - 7.35% MySQL) with 30 queries
Page Loading Time:

Support Apolyton: Amazon USA | Amazon UK | Amazon DE | Amazon FR |
Support Apolyton and get FREE PLUS, Buy from Chips&Bits: Galactic Civilizations | Galactic Civilizations: Deluxe Edition | Call to Power 2 | Civilization: The Boardgame | GURPS/ Alpha Centauri | Alpha Centauri | Civilization IV | Civilization III: Complete |


Front Page | Civilization IV | Civilization III | Civilization II | Call to Power II | Alpha Centauri | Master of Orion III
Rise of Nations | Galactic Civilizations | Galactic Civilizations II | Misc
Alt.Civs | Civ I | C:CtP I | About | News | Directory | Apolyton Store | Forums | Chat | Columns | Interviews | Newsletter
Scenario League | CSC | Clash of Civs | Spanish Site | CtP Maps | Cradle of Civ | WesW's Ctp1/2 Site | Civ3 Haven

apolyton.net | apolyton.com | civilization2.net | civilization3.net | civilization4.net | civilizationiv.info | calltopower.net | galciv.net | galciv2.net | moo3.net