Link to home
Start Free TrialLog in
Avatar of Gambit2025
Gambit2025

asked on

STRCMP pointer problem

Greetings everyone,

I am comparing elements of two pointer char arrays and comparing them using the string function strcmp. Both arrays contain words, one word at each element. My objective is to find out if there is any word present in the first array which matches the list of words in the 2nd. s1 is the first pointer array, s2 is the second.

for(i=0;i<length_of_ptr1;i++)
     {
          for(j=0;j<length_of_ptr2;j++)
          {    
               if(strcmp(s1[i],s2[j])==0)
               {
                    //printf("s1=%s s2=%s",s1,s2);
                    Counter++;
               }
          }
     }

The above code gives me a memory error when I run it( MS Visual C++ IDE). When I change to code to this,

char *a,*b;
for(i=0;i<length_of_ptr1;i++)
     {
                     a=s1[i];
          for(j=0;j<length_of_ptr2;j++)
          {  b=s2[j];    
               if(strcmp(a,b)==0)
               {
                                        Counter++;
               }
          }
     }

This also gives me a memory error. When I change the above code to if(strcmp(&a,&b)==0)
instead of if(strcmp(a,b)==0), it doesnt give me an error but the counter value is equal to the number of words in the 2nd array. What is going wrong? TIA.

PS- I have dynamically allocated memory to all pointers and pointer arrays. No problem there.
ASKER CERTIFIED SOLUTION
Avatar of GaryFx
GaryFx

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Gambit2025
Gambit2025

ASKER

I got it now. Problem solved. Debugging helped. Thanks GaryFx.