0

So apparently, when I compile this program on my own computer it seems to work. However, on another compiler it gives Segmentation Fault Errors. I've used pointers here, and I might have used them wrong, however, logically everything should work without error. If anyone could point out some errors, it would be greatly appreciated.

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;
}
1
  • Fix your code until no more warnings are issued by the compiler. Commented Nov 15, 2013 at 7:41

1 Answer 1

3

You assign integer to pointer and pointer to integer. It is wrong at least:

int l = t;
...
t = l
Sign up to request clarification or add additional context in comments.

7 Comments

+1 This is the biggest hitch in this code, and running it on different implementations where sizeof(int) and sizeof(char*) differ (or are the same) will exhibit the behavior seen. Changing int l to intptr_t l and including <stdint.h> is one potential work-around, but I would have to look more to be confident in that. In either case, the compiler should have warned the OP if configured properly.
I'm simply setting the location that t is pointing to to l as an int? Then I retrieve it later on... What's wrong with that exactly?
@Maaz what do you suppose happens when the physical size of a pointer in memory is larger than an int (such as a 32bit int and a 64bit pointer)?
So it's just a size of the integer that's being the limitation here? For example if I used long int or something, would that fix this issue? Furthermore, using int* l = t fix the error?
Oops sorry, I meant char* l = t
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.