[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

STRCMP pointer problem

Posted on 2003-10-24
2
Medium Priority
?
1,159 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 750 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

650 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