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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

still having trouble understanding pass values to functions

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
US
I created two functions, one works the other does not.

function 1 works when I pass a word like "foreign" in the variable foreign it works.
note I just copied the top part of the code so its not complete
How can I call function 2 to make it work?
it does not recognize the word "foreign" and drops into the else. even though I, printf the variable "foreign" and see the word "foreign" on the screen. ARGH
I am also getting warnings at compile time . located at bottom of this post.
function 1 works just fine
Code:
// this is how I am calling it
myword = "four";
printf("four  = \t%s\n",translator(myword,NULL,"foreign"));

// this is the function
translator(char *myword, char* english, char* foreign){ 
/* trnslates words from english to whatever */ 
     int n = 0; 
     const char *words[NBWORDS][2]   = WORDS_LISTS; 
    
     /* if foreign translate the word to a foreign language */
	if(foreign == "foreign"){
		printf("\n This is english to foreign translation");
...

function 2 does not work
it falls into the "else" part
Code:
// this is how I am calling it
translate2(myword,NULL,"foreign")

// this is the function
char* translate2( struct dictionary* d, 
                            const char* const english, 
                            const char* const foreign){
	const char *words[NBWORDS][2]   = WORDS_LISTS; 
//	printf("\nenglish = %s\n",english);  
//	printf("\ndictionary = %s\n",d);     
	if(foreign == "foreign"){
	printf("\n>>>>>>> foreign <<<<<<<<\n");    
	    
	}
	else
	{
		printf("\n>>>>>>> else <<<<<<<<\n");  
		printf("\nforeign = %s\n",foreign);
...


Warnings
translate2 is in the file mytools
line 93 is if(words[n][0] == d){
line 95 is return words[n][1];
Code:
mytools.c: In function 'translate2':
mytools.c:93: warning: comparison of distinct pointer types lacks a cast
mytools.c:95: warning: return discards qualifiers from pointer target type

TIA

DougP
 
Code:
if(foreign == "foreign")
compares the address of foreign with the address of "foreign". Since they are not the same, it always returns false. To compare the content, use strcmp
Code:
if(strcmp(foreign,"foreign") == 0)
 
If this is C

#include <string.h>

If it is C++

#include <cstring>
 
About warnings: there is an error (not a simple warning) in this snippet. You are trying to compare a pointer to structure with a pointer to char. It's a senseless operation.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top