Archived

This topic is now archived and is closed to further replies.

Metal Typhoon

using new to allocate a mult array

Recommended Posts

quote:
Original post by Evil Bill
Yeah, * is a pointer, and ** is a pointer to a pointer.
If you want a 3D array, you''d need 2 loops, and char*** Strings, yeah.




wowow i didnt understand this now.

i hade char *Strings;
i did the same as u said it didnt work.. but
char* Strings; worked... why is that ? any diference between char* string and char *string; ??? and what would u call this char*** ?

Share this post


Link to post
Share on other sites
It doesn''t matter where you put the *, char* Strings is the same as char *Strings and char * Strings, it must have been a bug elsewhere.
I refer to char*''s as ''strings'', so i''d call a char** an array of strings, and a char*** a 2D array of strings. Alternatively: "a pointer to a pointer to a pointer to a character". But "a 2D array of strings" is much easier

Share this post


Link to post
Share on other sites
quote:
Original post by Evil Bill
It doesn't matter where you put the *, char* Strings is the same as char *Strings and char * Strings, it must have been a bug elsewhere.
I refer to char*'s as 'strings', so i'd call a char** an array of strings, and a char*** a 2D array of strings. Alternatively: "a pointer to a pointer to a pointer to a character". But "a 2D array of strings" is much easier




ehheeh true ... hey check this out.. this is what i'm tying to do.


    
cin >> String;

for (int Room = 0; Room < String; Room++)
{Strings[Room] = new char [64];}

for (int Loop = 0; Loop < String; Loop++)
{
cout <<"String # "<<Loop<<" : ";
cin.getline (Strings[Loop][0],64);
}


is this right ??



[edited by - Metal Typhoon on December 24, 2002 9:13:58 PM]

Share this post


Link to post
Share on other sites
Room < String and Loop < String is wrong. You'll want a constant there: e.g. Room < 3.

Edit: What exactly are you trying to do? read in a number and allocate a ?x?x64 array?
If so:

  
char String[50];
int nCount;

cin >> String;
nCount = atoi(String);
Strings = new char**[nCount];
for(int i=0; i<nCount; i++)
{
Strings[i] = new char*[nCount];
for(int j=0; j<nCount; j++)
Strings[i][j] = new char[64];
}

for(int Loop=0; Loop<String; Loop++)
{
cout << "String # "<< Loop <<" : ";
cin.getline(Strings[Loop][0],64);
}

or something similar...


[edited by - Evil Bill on December 24, 2002 9:22:52 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Evil Bill
Room < String and Loop < String is wrong. You''ll want a constant there: e.g. Room < 3.





it worked fine... cuz i''m imputeing on int String...
it''s fine.. now the other loop i dont know what''s wrong.. should i jsut do... cin.getline (String[Loop],64) ??

Share this post


Link to post
Share on other sites
quote:
Original post by Evil Bill
If String is defined as a char**, cin.getline(String[Loop],64) will be fine, yeah.


If String is defined as a char**, cin.getline(String[Loop],64) will be fine, yeah.

it''s not defined as that but ill turn into that when i do Strings = new char [64] on the loop for the 3 arrays ... would that make a problem ?

Share this post


Link to post
Share on other sites
what i'm trying to do is.. reada number and then allocate space for arrays of strings... just plaing 2d arrays here....


    
#include <iostream>
#include <fstream>
#include <string>
#include <dos.h>

using namespace std;

int main ()
{
int Render;
int String;

char **Strings;

float Life;
float Fade;

cout <<"| Spray 1.0 Beta |"<<endl;
cout <<"| -------------- |"<<endl<<endl;

cout <<" Settings @ Primary "<<endl;
cout <<"\tLife : ";
cin >>Life;
cout <<"\tFade : ";
cin >>Fade;
cout <<"\tRender : ";
cin >>Render;
cout <<"\tString : ";
cin >>String;

Strings = new char *[String];

for (int Room = 0; Room < String; Room++)
{Strings[Room] = new char [64];}

for (int Loop = 0; Loop < String; Loop++)
{
cout <<"\t String # "<<Loop + 1<<" : ";
cin.getline (Strings[Loop],64);

}

return 0;
}


and this is the whole program... up to now


[edited by - Metal Typhoon on December 24, 2002 9:39:46 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Evil Bill
Edit: Oops, yeah, your code should work fine.



[edited by - Evil Bill on December 24, 2002 9:42:04 PM]


but i''m getting a strange error Evil Bill... not on the compiler.. when the pro goes... String # 1 : String # 2 : _ <- the cursor blinks there.. i never get the chance to type in String 1 .... id ont know why

Share this post


Link to post
Share on other sites
quote:
Original post by vtwin
Is there a reason Metal Typhoon deleted his other thread, which was going absolutely nowhere?

Oh, haha. I just answered my own question.


swift URL | browse my bookmarks [server may not always be on]

Sensimed | Info/download



yes there is a reason. cuz i dont want to waist time on your "answers" have a good day evil santa

Share this post


Link to post
Share on other sites
quote:
Original post by Metal Typhoon
have a good day evil santa


Rotfl.


That''s great.
Sorry, MT, but that''s exactly how it struck me.


And I didn''t know you could delete threads that you started. Cool.

Twilight Dragon

Share this post


Link to post
Share on other sites
quote:
Original post by TDragon
[quote]Original post by Metal Typhoon
have a good day evil santa


Rotfl.


That''s great.
Sorry, MT, but that''s exactly how it struck me.


And I didn''t know you could delete threads that you started. Cool.

Twilight Dragon
good good now u know u can delete it

Share this post


Link to post
Share on other sites
You can also represent multi-dimensional arrays as a 1-dimensional array. Here''s how to do it with a 2-dimensional array:

#define ARRAYCOLS 256
#define ARRAYROWS 64

int myarray[ARRAYCOLS * ARRAYROWS];

/* You can then address item on row R and column C like so: */
myarray[(R * ARRAYCOLS) + C] = 55;

- Andy Oxfeld

Share this post


Link to post
Share on other sites