Passing Char array

Hi guy's,

So I am basically a beginner. I have used c and c++ for compuatation but beyond that I have done nothing. I have a problem and I have read other forum topics and tutorials but they are just not helping. My problem is basic as I will describe. Any help would be great.

I am attempting to write an object orientated program that compares two words and determines if they are anagrams of each other. My problem is attempting to assign the user defined words to the private members of a class. I am using a set function to do this but I can not pass the char arrays to the set function correctly.

I understand that when passing char arrays we are actually dealing with pointers so I should be simply able to use the dereference operater (*) once in my set function but no matter how I arrange the syntax I get an error which is always along the lines of "invalid conversion from `char*' to `char'" or "incompatible types in assignment of `char' to `char[100]' ". Here's my relevant code;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main(void)
{
    char w1[100], w2[100];
    Anagram a;

    cout<<"Enter the first word: ";
    cin>>w1;

    cout<<"Enter the second word: ";
    cin>>w2;
    
    a.setWord(w1,w2);
    a.printword(); //Function to test the assigning of words
    //a.Comparison(); //Function to determine if words are anagrams.not relevant

    cout<<"\nEND of Program. Press any key to close window...";getch();
    return 0;
}

void Anagram::setWord(char* w1, char* w2)
{
	 word1=*w1; //word1 and word2 are private members of the class "Anagram"
	 word2=*w2;
}


Can anybody tell me what I am doing wrong?

Thanks,
Dave
Last edited on
In your setWord() function, the syntax you are using on the right-hand side of the equal sign will retrieve the first character in the string (if there is one.) Also, you need to check that w1 and w2 are not NULL, or you have a nasty suprise. What are the types of word1 and word2?
I've done a similair program and I can share the source if you want.

As for your problem, I recommend using std::string's here. This way you don't have to worry about pointers, you just have to assign the values of the arguments to setWord to word1 and word2 (which I also recommend you make std::string's).

If you want to keep using char arrays change your function body to this:
1
2
word1 = w1;
word2 = w2;

Last edited on
Thanks Kooth,

word1 and word2 are type char. Ill post my class if it helps. as for
you need to check that w1 and w2 are not NULL
could you elaborate a bit please? In my mind Im thinking they wont be null as the user should input value but you obviously disgaree?
Hi ascii,

I dont want to use strings. This is just an exercise in trying to do it as manually as possible...if that makes sense? as for changing the function body to
1
2
word1 = w1;
word2 = w2;


It returns the error: incompatible types in assignment of `char*' to `char[100]'

Thanks for the help.
Last edited on
:o stupid me. Try using the strcpy() function instead like this:
1
2
strcpy(word1, w1);
strcpy(word2, w2);

If that gives you another error just do:
1
2
strcpy(word1, *w1,);
strcpy(word2, *w2);
Last edited on
Thanks ascii,

I just want to clarify this. I assume you mean to use this in the main? If so, it still doesnt work because, I believe, word1 and word2 are not defined in the main but in the class and if I am to use the object 'a', they are still private so there is no direct access?

I also want to avoid the use of the string library if possible.
No, use this in your setWord function. It will copy the contents of w1 and w2 to word1 and word2. Also, it's not in the standard string library, the function was intended to be used with c-style strings, not std::string's.
Ah ha! yes this works perfectly. Thank you ascii. This has been driving me crazy.

Just wondering though if you know of how to do it with out this function or stepping through each element of the array one at a time?
If you want to do it manually just do this:
1
2
3
4
for(int i = 0; *(w1+i) != '\0'; i++)
	word1[i] = *(w1+i);
for(int i = 0; *(w2+i) != '\0'; i++)
	word2[i] = *(w2+i);
Answering your question about w1 and w2 being NULL:

When you write a function that takes a pointer to anything, the first thing you must do is make sure that it isn't null. If you put your class in a library, and someone else links in your code and they call your setWord() without sending you a valid pointer, it will either be garbage or NULL and if you don't check it, you will get a very nasty core dump if you are on UNIX/Linux, or one of those "Your program has executed an illegal function ..."-type of message.

ASCII is right: You should use the string library. But it's good to know how to do the "C" side of things because you will definitely run into it!
Excellent. Massive thanks to both Kooth and ascii. You's have been amazing.
Last edited on
Topic archived. No new replies allowed.