dynamic allocation array/function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SneakyElf@gmail.com

    #1

    dynamic allocation array/function

    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';
    }

  • Gianni Mariani

    #2
    Re: dynamic allocation array/function

    SneakyElf@gmail .com wrote:
    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::getlin e 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.


    Comment

    • James Kanze

      #3
      Re: dynamic allocation array/function

      On Sep 24, 12:52 am, Sneaky...@gmail .com wrote:
      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 "requiremen t" 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 meetsRequiremen ts[ 80 ] = {} ;
      sentence.copy( meetsRequiremen ts, 79 ) ;
      char* result = new[ strlen( meetsRequiremen ts )
      + 1 ] ;
      strcpy( result, meetsRequiremen ts ) ;
      return result ;
      }

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

      --
      James Kanze (GABI Software) email:james.kan ze@gmail.com
      Conseils en informatique orientée objet/
      Beratung in objektorientier ter Datenverarbeitu ng
      9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

      Comment

      • Michal Nazarewicz

        #4
        Re: dynamic allocation array/function

        SneakyElf@gmail .com writes:
        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-


        --
        Best regards, _ _
        .o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
        ..o | Computer Science, Michal "mina86" Nazarewicz (o o)
        ooo +--<mina86*tlen.pl >--<jid:mina86*jab ber.org>--ooO--(_)--Ooo--

        Comment

        Working...