tags:

views:

100

answers:

4

The code says at many places "invalid indirection".Please help.

   int main()
    {

        char *s1,*s2,*position;
        printf("Enter string:\n");
        gets(s1);
        printf("Enter word to find:\n");
        gets(s2);
        *position=ststr(*s1,*s1);
        if(*position)
        printf("word is found at %c loc\n",*position);
        else
        printf("word not found");
        getch();
        return 0;

    }

char *strstr(char *s1,char *s2)
{
    int flag=1;
    char i,j;
    for(i=0; ;i++)
    {
        if(*s1[i]==*s2[0])
        for(j=i;*s2;j++)
        {
            if(*s1[j]!=*s2[j])
            flag=0;
        }

    }
    if(flag)
    return i;
    else
    return 0;
}
+2  A: 
if(*s1[i]==*s2[0])

is such an example where my gcc complains:

error: invalid type argument of ‘unary *’ (have ‘int’)

if s1 is a pointer to char, s1[i] is a char. So you can't dereference it any more (with the *), i.e. s1[i] does not point to anything any more.

Try

if(s1[i]==s2[0])

instead.


You should also change the return value of strstr: you return an integer where you declare to return a pointer to a character. So try returning s1+i instead.


This here:

for(j=i;*s2;j++)

probably does not what you want. You're not advancing the pointer s2 anywhere in the loop, in fact you're just testing whether s2[0] (which is the same as *s2) is zero for each iteration. If s2 isn't the empty string, this loop will never terminate.

Andre Holzner
I dint got the second part.I guess i did right.
fahad
Andre Holzner
+1  A: 
         if(*s1[j]!=*s2[j]) 
  • *s1 means "the character where s1 is pointing".
  • s1[j] means "*(s1+j)" or "the character j positions after where s1 is pointing"

You have to use one or the other; not both.

James Curran
+1  A: 

One of the problems I'm noticing is whenever you do *s1[j]. The asterisk is dereferencing the array, and so is the [] notation.

s[i] really means *(s + i), so you don't have to dereference it again. The way you have it would read **(s + i), and since it's a single pointer you can't do that.

KLee1
"The apostrophe is dereferencing" => "The asterix..."
James Curran
"The asterix" => "The asterisk"
ptomato
wow... probably should not answer questions first thing in the morning...
KLee1
+2  A: 

First, s1 and s2 in main have not been initialized to point anywhere meaningful. Either declare them as static arrays, or allocate memory to them at runtime using malloc() or calloc():

#define SIZE 20 // or some number big enough to hold your input
...
char s1[SIZE], s2[SIZE], *position; // s1 and s2 declared statically

Second, NEVER NEVER NEVER NEVER NEVER use gets(); it will introduce a point of failure in your program. Use fgets() instead:

if (fgets(s1, sizeof s1, stdin) != NULL)
  // process s1
else
  // check for EOF or error on read

EDIT

And like everyone else has pointed out, your comparison in the strstr() function needs to be either

*s1 == *s2

or

s1[i] == s2[i]

but first you need to deal with allocating your buffers in main properly.

John Bode