0

What the program is supposed to do:

Gets a DNA code from the user

Gets 10 3-letter words

If the combination of any 2 3-letter corresponds with the DNA code program prints it.

I hope I managed to explain it well.

I don't know why it crashes, but I guess it is about the double pointer thing I tried to do. Or the strcmp thing.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    char * dna;
    char ** sample;
    int i,j,len;

    dna = (char*) malloc(sizeof(char)*20);

    gets(dna);
    sample = (char **) malloc(sizeof(char*)*10);
    for(i=0; i<5; i++)
    {
        sample[i] = (char *) malloc(sizeof(char)*3);
    }

    for(i=0; i<5; i++)
    {
        gets(sample[i]);
    }

    for(i=0; i<5; i++)
    {
        for(j=0; j<5; j++)
        {
            strcat(sample[i],sample[j]);
            if(strcmp(sample[i], dna)==0)
            {
                puts(sample[i]);
                return 0;
            }
        }
    }

    for(i=0;i<5;i++)
    {
        free(sample[i]);
    }

    free(sample);
    free(dna);
    return 0;
}
Mike
  • 4,041
  • 6
  • 20
  • 37
Enes Arda
  • 3
  • 2
  • 2
    "I don't know why it chrashes" -- consider using a debugger may be – asio_guy Oct 04 '18 at 06:07
  • What is the input you are giving to the program? – Rishikesh Raje Oct 04 '18 at 06:07
  • `strcat(sample[i],sample[j]);` How many characters do you expect `sample[i]` to hold? – Gerhardh Oct 04 '18 at 06:08
  • You don't provide memory for 10 words. And the memory you provide is only for 1 letter words. – Gerhardh Oct 04 '18 at 06:10
  • i make every sample[i] a string arrray and make it hold 3 letters – Enes Arda Oct 04 '18 at 06:16
  • sample = (char **) malloc(sizeof(char*)*10); -- i provide memory for 10 words – Enes Arda Oct 04 '18 at 06:19
  • Note that [`gets()` is too dangerous to be used — ever!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) – Jonathan Leffler Oct 04 '18 at 06:20
  • No; the `sample` array elements can only hold 2 characters plus the terminating null because you allocate `sample[i] = (char *) malloc(sizeof(char)*3);`. You can't safely concatenate the strings (`strcat()`) unless both source and destination have 1-character strings. You're not paying enough attention to memory. C doesn't pay attention to how you use memory either — and does what you tell it assuming you know what you're doing. That, I regret to say, makes it hard for beginners who don't know what they're doing. But you need to allocate more space, and you need to forget that `gets()` exists. – Jonathan Leffler Oct 04 '18 at 06:24

1 Answers1

1

your memory calculations are wrong, memory allocated for sample[i] and sample[j] are exactly the same, yet you are trying to concatenate to sample[i] which is not the correct thing

strcat(sample[i],sample[j]);

On a completely different note from the manual of gets

Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
asio_guy
  • 3,667
  • 2
  • 19
  • 35