0

Take a simple program where i want to count the number of vowels in a string, this i've easily implemented using string.h. But when i try that with char*:

#include <stdio.h>

int main(void)
{

    char *c = NULL;
    printf("Enter your sentence - ");
    scanf("%c", c);
    int y = 0;
    int x = 0;
    while (*c != '\0')
    {
        if (*c == 'a' || *c == 'e' || *c == 'i' || *c == 'o' || *c == 'u' || *c == 'A' || *c == 'E' || *c == 'I' || *c == 'O' || *c == 'U')
        {
            x++;
        }
        else
        {
            y++;
        }
    }
    printf("Vowels = %i Consonents = %i\n", x, y);
}

It compiles ok, but gives the following error when i try to execute it: Enter your sentence - Segmentation fault

I know it means that it means i am trying to illegally access some memory, but i can't figure out where. Can someone please point me to the right direction?

1

A couple of things about your program, the variable c is a pointer to a string, but it has no memory assigned, if you want to use c as it is, you must use the malloc function. The variable c is not updated inside the while loop, in effect c is a pointer and we can use pointer arithmetic to advance along the string. The scanf () function, as it is written, will only read one character, and not a complete string,also keep in mind that scanf will only read up to the first blank space it finds, so your program would look something like this:

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

int main(void)
{
    char *c = malloc(88);
    char *temp = c;
    printf("Enter your sentence - ");
    scanf("%s", c);
    int y = 0;
    int x = 0;
    while (*c != '\0')
    {
        if (*c == 'a' || *c == 'e' || *c == 'i' || *c == 'o' || *c == 'u' || *c == 'A' || *c == 'E' || *c == 'I' || *c == 'O' || *c == 'U')
        {
            x++;
        }
        else
        {
            y++;
        }
        c++;
    }
    printf("Vowels = %i Consonents = %i\n", x, y);
    free(temp);
    return 0;
}

NOTE: As with gets, it is very dangerous to use scanf to read strings, since scanf does not take into account the length of what is read and will allow the user to write more characters than the defined array (or the space allocated by malloc) in the program can admit. As a result, scanf will write the characters that no longer fit in the defined array in other memory portions that may contain other data that our program is using. This will lead to abnormal behaviors, memory leaks, etc.

EDIT: There is indeed a memory leak, but we should not try free(c), because c points to zero at the end of the program, we should not try free() a pointer that points to zero (NULL) to we will have a problem with the program. Since you ask it, what to do?, the solution is simple, we will use a temporary pointer that points to the same memory address as c, and at the end of the program we free it, you can see the modified program.

6
  • It's not running! The program is stuck when i try to run it. i tried adding free(c); at the end of the program, but it still won't run! Is this because of some memory leak? What to do now? – Musical_Ant Apr 11 '20 at 6:50
  • The program compiles well in my IDE, as for the memory leak, you can see my EDIT – MARS Apr 11 '20 at 12:46
  • Sorry, but it's not running on the linux command line (terminal) too! did you use the cs50 ide? – Musical_Ant Apr 11 '20 at 18:28
  • effectively use the IDE CS50. Can you tell me what error you receive, what command to use to compile, etc ...? – MARS Apr 11 '20 at 18:36
  • i used the make command, and it doesn't give any errors! when i run it using ./ and enter any input, it is simply stuck! Nothing happens. After a while i have to hit ctrl + c. – Musical_Ant Apr 11 '20 at 19:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .