0

显然,当我在自己的计算机上编译这个程序时,它似乎可以工作。但是,在另一个编译器上,它会给出分段错误错误。我在这里使用了指针,我可能用错了,但是,从逻辑上讲,一切都应该正常工作而不会出错。如果有人能指出一些错误,将不胜感激。

contains.c

#include <string.h>

int contains(char *s, char *t)
{
    if (!t)
        return 0;

    if (!*t)
        return 0;

    if (s)
    {
        char *k;
        int n = 0;
        int l = t;
        int len = strlen(t);
        int counter = 0;

        printf("%d", len);

        for(k=s; *k; k++)
        {

            printf("S: %c ", *k);
            printf("T: %c ", *t);
            if (*k == *t)
            {
                printf("Si: %c ", *k);
                printf("Ti: %c ", *t);
                t++;
                counter++;
            }
            else
            {
                counter = 0;
                t = l;
                if (*k == *t)
                {
                    t++;
                    counter++;
                    printf("Si: %c ", *k);
                    printf("Ti: %c ", *t);
                }

            }

            printf("%d\n", len);
            printf("%d", counter);
            if (counter >= len)
            {
                    n++;
                    t = l;
                    counter = 0;
                    if (*k == *t)
                    {
                        t++;
                        counter++;

                        printf("Si: %c ", *k);
                        printf("Ti: %c ", *t);
                    }
            }
        }

        return n;
    }
    else
        return 0;



}

main.c

#include <stdio.h>

int contains(char *s, char *t);

int main(void)
{
  printf ("%d\n", contains ("I wanna shoot something!", "thing"));
  printf ("%d\n", contains ("Let's get in range!", "ge"));
  printf ("%d\n", contains ("Wanna see the fireworks?", "wanna"));
  printf ("%d\n", contains ("Look at the pretty explosions!", " "));
  printf ("%d\n", contains ("Kaboom!", ""));

  printf ("%d\n", contains ((char *) 0, "aaa"));
  printf ("%d\n", contains ("aaa", (char *) 0));
  printf ("%d\n", contains ((char *) 0, (char *) 0));

  return 0;
}
4

1 回答 1

3

您将整数分配给指针并将指针分配给整数。至少这是错误的:

int l = t;
...
t = l
于 2013-11-15T07:32:34.220 回答