Thread: strcpy and array

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    6

    strcpy and array

    basically a func named getString is supposed to ask user to enter a sentence, store sentence in the array, dynamically allocate a char array big enough to hold the sentence plus null terminator. then it should copy the sentence to the dynamically allocated array then return the pointer to the array.
    i'm confused on the return statement and "ptr = getString(str, SIZE);"

    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    char getString(char *, int);
    
    int main()
    {
            
            char *ptr;
            const int SIZE = 81;
            char str[SIZE];
            
            ptr = getString(str, SIZE);     
            
            cout << ptr << endl << endl;
            delete [] ptr;
            ptr = NULL;
    }
    
    
    char getString(char *str, int size)
    { 
            char *ptr;
            int k=size;
            cout << "enter a sentence.\n"; //ask user to enter a sentence
            cin.get(str, size);
            
            //dynamically allocate a char array just large enough to hold the sentence
            //plus the null terminator.
            ptr = new char[k+1];
            
            //copy the sentence to the dynamically allocated array 
            //then return a pointer to the array
            strcpy(ptr, str);
            return char ptr;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The directions say you need to return a pointer. So you need to write that in your function declaration, that getString returns "char *" (not "char"). Then you can write your return statement properly once that's done.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    6
    problem: cannot convert from 'char' to 'char *' >>> ptr = getString(str, SIZE);

    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    char getString(char *, int);
    
    int main()
    {
            
            char *ptr;
            const int SIZE = 81;
            char str[SIZE];
            
            ptr = getString(str, SIZE);     
            
            cout << ptr << endl << endl;
            delete [] ptr;
            ptr = NULL;
    }
    
    
    char getString(char *str, int size)
    { 
            char *ptr;
            int k=size;
            cout << "enter a sentence.\n"; //ask user to enter a sentence
            cin.get(str, size);
            
            //dynamically allocate a char array just large enough to hold the sentence
            //plus the null terminator.
            ptr = new char[k+1];
            
            //copy the sentence to the dynamically allocated array 
            //then return a pointer to the array
            strcpy(ptr, str);
            return *ptr; 
            //return char *ptr;
    
    }
    Last edited by taj777; 04-21-2010 at 08:28 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because you don't want to return *ptr. You want to return ptr. Do so.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM
  2. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM
  3. strcpy() with an array of strings
    By J-Dogg in forum C Programming
    Replies: 3
    Last Post: 03-27-2003, 05:11 AM
  4. Array of pointers + strcpy = access violation?
    By DarkDragon in forum C Programming
    Replies: 2
    Last Post: 09-09-2002, 01:56 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM