Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pass a string to a function

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
US
I have this function and am trying to take a phrase such as
"hhh bb ww zzzzz" abd translate it. the Problem is I think on line strncat(newPhrase,s1,1);
even the program compiles without problems it won't run, it crashes.
How do I take whats passed in "phrase" and put it into a var so I can parse it?
Code:
char* the_phrase(const char* const phrase){ 
/* See requirements for details on role and implementation
*/       
         
       /*	intialize Vars */
		int k;
		char s1[] = "a";
		char s2[100]; /* needed to make the strncat work */
		char * newword;
		char * newPhrase;  	/* var for final TTT Phrase */
		for (k = 0; phrase[k] != '\0'; k++){
				if(phrase[k] == ' '){
					/*printf("%c\n Found a space");*/
							
										/* add this new word to the newPhrase var */
					
					strncat(newPhrase,s1,1);
				}
				else{
					
					s2[k]=phrase[k];
					/*printf("%c\n",s2[k]);*/
					/* Build word one letter at a time */

				}
		}
 		
 		
		return newPhrase;
}

DougP
 
the_phrase's parameters tell the compiler that the phrase is constant. The variable "phrase" cannot be modified in any case. The value of phrase and the address it points to cannot be modified if declared as "const char* const phrase".

Right off the bat that seemed wrong to me and I did a quick google on "const char* const" and this article ' explained all about const(s)...

Personally I would just remove the consts references and leave it as a pointer since this will achieve your desired affect.
 
let us know how you fixed it, so everyone can see the solution.
 
And like an idiot I just realized that he's not using "phrase" in the concatenation method so my post is irrelevant.
 
I have'nt yet. as I still don't know how to pass whats in phrase to do anything with it? that is the underlying issue.


DougP
 
well as your code reads: You populate newPhrase with nothing but ' ' (spaces). then you return the newly build "newPhrase" which is some length of nothing but spaces.
sk2 holds all of the characters that arent spaces(built but not used). what is the desired result of this function?
 
sorry.. newphrase will be populated with nothing but "A"s whenever a space is found. so it should return a char* populated with "A"s
 
The declaration "char* newPhrase;" defines newPhrase pointer variable with undefined value. Now you are trying to "concatenate" something with undefined (erratical) memory place contents. The newPhrase pointer in your function refers to NOWHERE!

And finally you return this undefined pointer as your function value...
 
Code:
char* newphrase = {0};  //zero out the memory

then after that, it would depend on what youre actually trying to do with the function. the original post says translate the phrase. translate how? what are the desired results? obviously to create and return a new string, but with what contents? As it stands(once "newphrase" is zero'ed), newphrase will return a string of "a"s for the number of spaces found within the string sent in.

s2, at the end of the function will contain the original string with no spaces.
 
SDowd said:
char* newphrase = {0};
Doesn't that only work with arrays like this?
Code:
char newphrase[80] = {0};

It would be a lot easier to just use the std::string class instead of char* strings.
 
i agree it would be easier to use CString.
And as for the " = {0}" compiles and runs for me.

VS 2010 express
Code:
char*  someCharArray = {0};
printf("someCharArray(%s)\n", someCharArray);

with output of:

Code:
"someCharArray = ((NULL))"
 
The code
Code:
char* newphrase = {0};
is C and C++ idiom of
Code:
char* newphrase = { LETS_LOOK_AT_NOWHERE };
;)

The only way to return a pointer to a new memory chunk: dynamically allocate this memory object, for example:
Code:
const int MaxNewPhraseSize = 1024;
char* newphrase = new char[MaxNewPhraseSize];
then don't forget to free this pointer by
Code:
p = the_phrase(...);
...
delete [] p

Better follow to cpjust's good advice ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top