Solved

STRCMP pointer problem

Posted on 2003-10-24
2
1,154 Views
Last Modified: 2008-02-26
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.
0
Comment
Question by:Gambit2025
2 Comments
 
LVL 6

Accepted Solution

by:
GaryFx earned 250 total points
ID: 9614562
Assuming you're running this in debug mode, you ought to be able to identify exactly which line prompts the memory error, and what the values of s1[i] and s2[j] are at that time.

If you need more help, please post the code where you declare and build s1 and s2.  The fact that the allocation of the arrays doesn't produce a run time exception doesn't prove there are no problems there.

Gary
0
 

Author Comment

by:Gambit2025
ID: 9614595
I got it now. Problem solved. Debugging helped. Thanks GaryFx.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question