6 Replies - 1727 Views - Last Post: 20 April 2010 - 01:45 PM Rate Topic: -----

#1 janice777   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 17-March 10

array and strcpy

Posted 20 April 2010 - 10:33 AM

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 having trouble with the strcpy and returning the pointer

#include<iostream>
#include<string>
using namespace std;

char getString(char *, int);

int main()
{
	
	char *ptr;
	const int SIZE = 81;
	char str[SIZE];
	
	getString(str, SIZE);	
	
	cout << ptr << endl << endl;	
	return 0;
}


char getString(char *str, int size)
{ 
	char *ptr;
	int k;
	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;
}


Is This A Good Question/Topic? 0
  • +

Replies To: array and strcpy

#2 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: array and strcpy

Posted 20 April 2010 - 10:44 AM

So, what specifically is your question?
Was This Post Helpful? 0
  • +
  • -

#3 sarmanu   User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: array and strcpy

Posted 20 April 2010 - 10:47 AM

Take a look at the return value of your function. It returns a pointer ("return ptr"), but you function return type is char. So, change the return type to char *:
char *getString(char *str, int size)


Another thing:
int k;
        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];


You are assigning memory for ptr (k + 1 characters), but the thing is that k is not initialized with any value. Did you mean to use:
ptr = new char[size+1];


or simply assign size to k:
int k = size;


Now, moving to main() function, you need to assign the function return value to a pointer to char. So, use:
ptr = getString(str, SIZE);


Lastly, don't forget to free the allocated memory using delete []:
delete [] ptr;


Was This Post Helpful? 0
  • +
  • -

#4 janice777   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 17-March 10

Re: array and strcpy

Posted 20 April 2010 - 10:48 AM

View PostOler1s, on 20 April 2010 - 09:44 AM, said:

So, what specifically is your question?


i keep getting these errors:
warning C4996: 'strcpy': This function or variable may be unsafe.

error C2440: 'return' : cannot convert from 'char *' to 'char'

This post has been edited by janice777: 20 April 2010 - 10:49 AM

Was This Post Helpful? 0
  • +
  • -

#5 janice777   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 17-March 10

Re: array and strcpy

Posted 20 April 2010 - 11:05 AM

hmm...there's still a problem with the return statement and "ptr = getString(str, SIZE);"

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

Was This Post Helpful? 0
  • +
  • -

#6 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: array and strcpy

Posted 20 April 2010 - 11:17 AM

Quote

i keep getting these errors:
The warning about strcpy being unsafe you can ignore for now. It is meaningful, as strcpy will not prevent a buffer overflow. It does seem like you bound everything properly, so you don't have an overflow problem.

The second problem is meaningful however.

char getString No this is wrong. You were correct before. A C string is an array of characters terminated with a NUL character. So you must return a pointer to char. Not a single character.

return char ptr; What is this syntax? Take a look at your books, or online materials. Look at the syntax for returning something. Does this look OK?
Was This Post Helpful? 0
  • +
  • -

#7 janice777   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 17-March 10

Re: array and strcpy

Posted 20 April 2010 - 01:45 PM

alright! thanks guys! =]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1