Search code examples
cstructsegmentation-faultcoredump

Segmentation fault (Core Dumped)-- Structures and Pointers--C language


I have gotten my code to compile, but after running and entering input, the core dumps. I am aware this this must be from a pointer problem, but I am unaware of where the problem stems. The goal of the code is to take user input of a DNA sequence, then print out how many times each base was entered. Thank you in advance for your responses!

#include <stdio.h>
#include <string.h>
#define N 25

void countBase (char *dnaSequence, int n)           //countBase function declaration
{
    int i;
    char *p;

    struct 
    {
    int aCount;
    int cCount;
    int tCount;
    int gCount;
    }dnaCount;

    p = &dnaSequence[0];
    for (p = 0; i < N; p++)
    {
        if (*p == 'A' || *p =='a')
        {
            dnaCount.aCount++;
        }
        if (*p == 'C' || *p == 'c')
        {
            dnaCount.cCount++;
        }
        if (*p == 'T' || *p == 't')
        {
            dnaCount.tCount++;
        }
        if (*p == 'G' || *p == 'g')
        {
            dnaCount.gCount++;  
        }
    }

    printf("Number of A's : %d\n", dnaCount.aCount);
    printf("Number of C's : %d\n", dnaCount.cCount);
    printf("Number of T's : %d\n", dnaCount.tCount);
    printf("Number of G's : %d\n", dnaCount.gCount);
}   

int main(int argc, char *argv[])
{
    char dnaSequence [N];

    printf("Enter a DNA sequence\n");           //prints prompt

    fgets(dnaSequence, N, stdin);               //retrieves user input

    printf("Sequence: \n%s", dnaSequence);          //prints entered sequence 

    countBase(dnaSequence, N);              //function call

return 0;                           //terminating line
}

Solution

  • You have several issues going on there:

    • A runaway pointer - you do not increment i in the for loop (it turns out that you do not need i at all - read on)
    • You ignore n passed into the function, using N instead - you do not need to pass n either, but if you choose to do it, you should use it in the loop
    • Your counters are not initialized - you need to set counters to zero, or zero out the entire struct.

    Here is how you can fix your code:

    void countBase (char *p) {
        struct {
            int aCount;
            int cCount;
            int tCount;
            int gCount;
        } dnaCount = {0}; // Set all counters to zero
        // Loop will end when you reach the end of null-terminated string
        while (*p) {
            if (*p == 'A' || *p =='a') {
                dnaCount.aCount++;
            } else if (*p == 'C' || *p == 'c') {
                dnaCount.cCount++;
            } else if (*p == 'T' || *p == 't') {
                dnaCount.tCount++;
            } else if (*p == 'G' || *p == 'g') {
                dnaCount.gCount++;  
            }
            p++;
        }    
        printf("Number of A's : %d\n", dnaCount.aCount);
        printf("Number of C's : %d\n", dnaCount.cCount);
        printf("Number of T's : %d\n", dnaCount.tCount);
        printf("Number of G's : %d\n", dnaCount.gCount);
    }