Dynamic array allocation; 
Author Message
 Dynamic array allocation;

I am just a starter with C++,

here is my test program regarding 2-D array of strings:

#include <iostream.h>

void main(){
        const int ctrW = 2;
        int ctr;
        char array[ctrW][100];

        for(ctr=0; ctr<ctrW; ctr++){
        cout << "enter a sentence";
        cin.getline(array[ctrW], 100);
        }

        for(ctr=0; ctr<ctrW; ctr++) {
                cout << array[ctrW];
        }

Quote:
}

I want to use a dynamic array allocation for the 2-D arrays.
I know the syntax for one dimensional array of strings like this:

cout << "enter the no of stings"
cin >> ctrW;

char *array = new char[ctr];

I want to use this technique.... how about for the two dimensional array
of strings?

PLEASE HELP ME.
THANX.

--
Posted via http://www.*-*-*.com/



Thu, 08 Dec 2005 17:58:05 GMT  
 Dynamic array allocation;

Quote:
> I am just a starter with C++,

And of course you think logical to post to comp.lang.c

What about


and don't forget to read their FAQ before posting!

--

The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-library: http://www.dinkumware.com/htm_cl/index.html
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/



Thu, 08 Dec 2005 18:46:37 GMT  
 Dynamic array allocation;

Quote:

> I am just a starter with C++,

The language is called C, not C++.

Quote:
> here is my test program regarding 2-D array of strings:

> #include <iostream.h>

You mean:

#include <stdio.h>

Quote:

> void main(){

You mean:

int main(void)
{

Quote:
>         const int ctrW = 2;

You mean:

#define ctrW 2

Quote:
>         int ctr;
>         char array[ctrW][100];

>         for(ctr=0; ctr<ctrW; ctr++){
>         cout << "enter a sentence";

You mean:

printf("enter a sentence"); fflush(stdout);

Quote:
>         cin.getline(array[ctrW], 100);

You mean:

fgets(array[ctr], sizeof array[ctr], stdin);

Quote:
>         }

>         for(ctr=0; ctr<ctrW; ctr++) {
>                 cout << array[ctrW];

You mean:

printf("%s", array[ctr]);

Quote:
>         }
> }

> I want to use a dynamic array allocation for the 2-D arrays.

Look up malloc, which is prototyped in <stdlib.h>

Quote:
> I know the syntax for one dimensional array of strings like this:

> cout << "enter the no of stings"
> cin >> ctrW;

> char *array = new char[ctr];

No, that's just a syntax error.

Quote:

> I want to use this technique.... how about for the two dimensional array
> of strings?

The easiest way is to malloc an array of char *, then go through that array
mallocing arrays of char.

Quote:

> PLEASE HELP ME.
> THANX.

No problem.

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Thu, 08 Dec 2005 20:09:59 GMT  
 Dynamic array allocation;

Quote:

> I am just a starter with C++,

> here is my test program regarding 2-D array of strings:

> #include <iostream.h>

That's actually neither C nor (standard) C++.
You should write:
  #include <iostream>
  using namespace std; // for now...

...

Quote:
> I want to use a dynamic array allocation for the 2-D arrays.
> I know the syntax for one dimensional array of strings like this:
...
> I want to use this technique.... how about for the
> two dimensional array of strings?

Note: a 2-dimensional array of char is actually
a 1-dimensional array of string.

If you want dynamic allocation in C++,
you should actually start with:
  #include <vector>
  #include <string>
  std::vector< std::string > myArrayOfStrings;

What C++ book are you using?  For ideas, see:
http://www.accu.org/bookreviews/public/reviews/0sb/beginner_s_c__.htm
 (NB: Accelerated C++ is a good choice IMHO)

hth,
--
 Ivan Vecerina, Dr. med.  <>  http://www.post1.com/~ivec
 Soft Dev Manger, XiTact  <>  http://www.xitact.com
 Brainbench MVP for C++   <>  http://www.brainbench.com



Thu, 08 Dec 2005 21:59:44 GMT  
 Dynamic array allocation;

Oops... Im sorry, Im just new at this forum... anyways thanx

--
Posted via http://dbforums.com



Thu, 08 Dec 2005 21:41:57 GMT  
 Dynamic array allocation;
[posted and mailed]



[I have both posted and emailed this reply]

Quote:
> I am just a starter with C++,


C++ and C are different languages.
Before you do post to comp.lang.c++, here are a few things to note:

[General]
Before posting to *any* newsgroup, check the FAQ and follow the
newsgroup for a while.  The FAQ for comp.lang.c++ can be found through
http://www.parashift.com/c++-faq-lite/ Following the news group need not
be done in real time.  Go to
<http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&group=comp.lang.c++>
(use http://tinyurl.com/eyos rather than trying to use the long URL)

[C++ specific]
Try not to post code that starts off being obviously broken.  This will
do nothing but accumulate flames.  Examples:

Quote:
> #include <iostream.h>

There is no header named <iostream.h> in C++ (or in C).  The header is
named <iostream>

Quote:
> void main(){

In neither C++ nor C is it permissible to claim that main returns
'void'.  main always returns an int.

Quote:
>         cout << "enter a sentence";
>         cin.getline(array[ctrW], 100);

Using identifiers from the std namespace ('cout' and 'cin') without
either properly qualifying them (e.g., std::cout) or incorporating a
using directive in your code will result in (at best) non-standard C++
and probably a compilation failure.

For all these cases, you should be setting your compiler for the best
conformance to the standard for the language and invoking it to give the
fullest possible diagnostics.

Quote:
> PLEASE HELP ME.
> THANX.

All caps is considered shouting and rude.  Cutesy spellings like "THANX"
instead of "Thanks" will mark you, whether justified or not, at least as
being not serious, and perhaps much worse.

--
Martin Ambuhl
now exiled to
Hurricane Bait, Texas



Fri, 09 Dec 2005 01:34:13 GMT  
 Dynamic array allocation;

Quote:

> I am just a starter with C++,

And, apparently, with Usenet. Try over at comp.lang.c++.

-Micah



Fri, 09 Dec 2005 12:49:42 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Help - Dynamic Array Allocation

2. Dynamic Array Allocation

3. 2D Dynamic Array Allocation - Differences?

4. Help with dynamic array allocation

5. Dynamic Array Allocation

6. Problem w/dynamic array allocation

7. Multi-dimensional array using dynamic memory allocation

8. Dynamic Allocation of Multi-Dimensional Array (Simple Question)

9. memory allocation / "dynamic arrays" / portability

10. Dynamic allocation of 2-dimensional array

11. Dynamic Allocation of 2D Arrays

12. Dynamic allocation and reallocation of two dimensional arrays

 

 
Powered by phpBB® Forum Software