Dynamic allocation of memory

B

bintom

I want to dynamically allocate to memory to n names, each of 21
characters. I have written the followin code, but it gives the
following error message:

cannot convert `char (*)[21]' to `char**' in initialization

The full program is given below: Thanks in anticipation,

Bintom

#include <iostream.h>

int main()
{ int size, i;

cout << "How many names? ";
cin >> size;

char *cptr = new char[size][21];

for(i=0; i<size; i++)
{ cout << "Enter name " << i+1 << ": ";
cin.getline(*(cptr+i), 21);
}
for(i=0; i<size; i++)
cout << *(cptr+1) << "\n" ;

return 0;
}
 
A

Alf P. Steinbach

* bintom, on 17.06.2010 05:54:
I want to dynamically allocate to memory to n names, each of 21
characters. I have written the followin code, but it gives the
following error message:

cannot convert `char (*)[21]' to `char**' in initialization

The full program is given below: Thanks in anticipation,

Bintom

#include<iostream.h>

int main()
{ int size, i;

cout<< "How many names? ";
cin>> size;

char *cptr = new char[size][21];

for(i=0; i<size; i++)
{ cout<< "Enter name "<< i+1<< ": ";
cin.getline(*(cptr+i), 21);
}
for(i=0; i<size; i++)
cout<< *(cptr+1)<< "\n" ;

return 0;
}

Off the cuff:


<code>
#include <string>
#include <vector>

int main()
{
using namespace std;
vector< string > names;

for( ;; )
{
string name;
cout << "Enter name, or just press return: "; getline( cin, name );
if( name == "" ) { break; }
else { names.push_back( name ); }
}

int const n = int( names.size() );
cout << "You entered " << n << " names:" << endl;
for( int i = 0; i < n; ++i )
{
cout << " " << names << endl;
}
}


It may not be apparent that this solves your question, but really the problem
stems from using low level raw pointers and stuff. Just use the standard
library. It's safer, easier to use, and results in more clear code.

By the way, <iostream.h> is a pre-standard header that a modern compiler may not
provide.


Cheers & hth.,

- Alf
 
I

Ian Collins

I want to dynamically allocate to memory to n names, each of 21
characters. I have written the followin code, but it gives the
following error message:

cannot convert `char (*)[21]' to `char**' in initialization

The line

char *cptr = new char[size][21];

is allocating size lots of 21 element arrays.

Make life simple and use std::string.
 
S

Sousuke

I want to dynamically allocate to memory to n names, each of 21
characters. I have written the followin code, but it gives the
following error message:

cannot convert `char (*)[21]' to `char**' in initialization

The full program is given below: Thanks in anticipation,

Bintom

#include <iostream.h>

int main()
{ int size, i;

  cout << "How many names? ";
  cin >> size;

  char *cptr = new char[size][21];

As the compiler's error message says, the expression "new char[size]
[21]" has type "char(*)[21]", so you have to declare "cptr" to have
that type:

char (*cptr)[21] = new char[size][21];

Yes, the syntax is nasty. Also, it limits the length of a name. And
it's prone to memory leaks (at least in larger programs).

The solution, unless this is homework, is to use a
std::vector<std::string>.
 
B

bintom

Thanks Sousuke,

Your advice has done the trick. But, could you explain what the
statement:

char (*cptr)[21] = new char[size][21];

does? Is that a typecast to pointer to char?

Thanks in advance,
Bintom
 
B

bintom

I'm not using te vector class becos our Board of Studies in India
still follows programming in the pre-Standard C++ formats only.
 
J

Juha Nieminen

bintom said:
I'm not using te vector class becos our Board of Studies in India
still follows programming in the pre-Standard C++ formats only.

No offense, but I find that a bit ironic given that there is no
"standardized" pre-standard C++ (because, after all, the first C++
standard was what first standardized the language). Hence trying to
stick to such vague language definition seems like a completely moot
point. What is and isn't supported will depend completely on the
pre-standard C++ compiler you are using. Basically, you wouldn't be
programming in C++ at all; instead, you would be programming in a C++
dialect defined by a certain (very old) compiler.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top