Solved

STRCMP pointer problem

Posted on 2003-10-24
2
1,151 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Binary Bomb: Phase 4 func4 3 285
Problem with MFCApp 78 317
Grammars for C C++ and java 1 108
C Programming - If Statement 8 49
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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now