dynamic allocation array/function

S

SneakyElf

Hi all,
so i need to write a function called getString() that has a local char
array of 80 elements. function should ask the user to enter a
sentence, and store the sentence in the array. then function should
dynamically allocate a char array just large enough to hold the
sentence, plus the null terminator. the function then will copy the
sentence to the dynamically allocated array, and return a pointer to
the array.

Here's what I have, but I get all kinds of errors.

help? :)

void getString (char str1 [], char str2 []) {

int length = 80;
char myStr [length];
cout << "Please enter a sentence: " << endl;
cin. getline (myStr, length);

int index = 0;
char *newArray;
newArray = new char [length];

while (myStr [index] != '\0') {
newArray [index] = myStr [index];
index ++;
}

newArray [index] = '\0';
}
 
G

Gianni Mariani

Hi all,
so i need to write a function called getString() that has a local char
array of 80 elements. function should ask the user to enter a
sentence, and store the sentence in the array. then function should
dynamically allocate a char array just large enough to hold the
sentence, plus the null terminator. the function then will copy the
sentence to the dynamically allocated array, and return a pointer to
the array.

Here's what I have, but I get all kinds of errors.

help? :)

void getString (char str1 [], char str2 []) {

int length = 80;
char myStr [length];
^^^^^^^^^^^^^^^^^^^^^^^^^^^

This type of allocation is non-standard. The number of elements needs
to be constant for "standard C++".

const int length( 80 );
char myStr [length];


cout << "Please enter a sentence: " << endl;
cin. getline (myStr, length);

I'm not sure if istrean::getline null terminates. You need to check.
int index = 0;
char *newArray;
newArray = new char [length];

while (myStr [index] != '\0') {
newArray [index] = myStr [index];
index ++;
}

newArray [index] = '\0';

The above is the same as strcpy no ?



All this is much easier with std::string of course.
 
J

James Kanze

so i need to write a function called getString() that has a local char
array of 80 elements.

Why? That's about the most stupid requirement I can imagine.
In fact, almost by definition, a "requirement" doesn't specify
what is local to the function. (It might, possibly, specify
that the function cannot use more than N bytes of local
variables, if the target machine had a very limited stack.) And
I certainly wouldn't use such an array in real code.
function should ask the user to enter a
sentence, and store the sentence in the array. then function should
dynamically allocate a char array just large enough to hold the
sentence, plus the null terminator. the function then will copy the
sentence to the dynamically allocated array, and return a pointer to
the array.

char*
getSentence()
{
std::string sentence ;
std::cout << "Input sentence: " ;
std::cin >> sentence ;
char meetsRequirements[ 80 ] = {} ;
sentence.copy( meetsRequirements, 79 ) ;
char* result = new[ strlen( meetsRequirements )
+ 1 ] ;
strcpy( result, meetsRequirements ) ;
return result ;
}

It's all a bit stupid, though. I fail to see the point of it.
 
M

Michal Nazarewicz

so i need to write a function called getString() that has a local char
array of 80 elements. function should ask the user to enter a
sentence, and store the sentence in the array. then function should
dynamically allocate a char array just large enough to hold the
sentence, plus the null terminator. the function then will copy the
sentence to the dynamically allocated array, and return a pointer to
the array.
void getString (char str1 [], char str2 []) {

Your function returns nothing instead of desired pointer. What are
those arguments for? You probably meant:

char *getString() {
int length = 80;

As it was pointed already it needs to be "const int" or better skip it
char myStr [length];

and declare array as "char myStr[80];".
cout << "Please enter a sentence: " << endl;
cin. getline (myStr, length);

What if the line was more then 79 character long? What happens to the
rest of the line? You should ignore it or something.
int index = 0;
char *newArray;
newArray = new char [length];

while (myStr [index] != '\0') {
newArray [index] = myStr [index];
index ++;
}
newArray [index] = '\0';

Better use strcpy. Or since you should calculate string's length before
allocating memory you may even use memcpy (or C++ equivalent),
ie. replace code starting with "int index = 0;" with:

size_t num = strlen(myStr) + 1;
char *newArray = new char[num];
memcpy(newArray, myStr, num);

newArray is just thrown away. You are missing a "return newArray;"
before closing bracket.

Surly someone will kill me for this code but what the hell...

#v+
char *getString() {
char tmp[80], *ch;

puts("Please enter a sentence: ");
fgets(tmp, sizeof tmp, stdin);

for (ch = tmp; *ch && ch!='\n' ++ch);

if (!*ch) { /* Ignore the rest of the line */
for (int c; (c = getchar())!=EOF && c!='\n'; );
} else {
*ch = 0;
}

size_t num = ch - tmp + 1;
memcpy(ch = new char[num], tmp, num);
return ch;
}
#v-
 

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,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top