Solved

STRCMP pointer problem

Posted on 2003-10-24
2
1,155 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C dll call freezes 5 112
Why does module make constant click noise ? 9 141
Problem to save 10 174
What's the Difference Between a VI, the Command Prompt and a Shell 7 134
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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

756 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