Why am I always getting Segmentation faults when displaying my array in C?

user275564

I've tried so much code trying to display my data in a histogram. I've been stuck on this problem for two days now, data crawling the internet and such to find an answer but they usually talk about pointers and ect. while this is much more basic. I've tried an actual histogram that displayed the same error but for the sake of keeping it simple I choose to post this one.

This is the description of the program from The C Programming language: "Write a program to print a histogram of the lengths of the words in its input. " I'm doing it horizontally then I will do it vertically. Although, in this example I am just trying to display the number, obviously.

#include <stdio.h>
#define MAXIMUM 99


main()
{
int i, c, j, lenOfWord[MAXIMUM], charCount;

for (i = 0; i < MAXIMUM; i++)
    lenOfWord[i] = 0;


printf("Starting program, please type then ctrl+d\n");



while((c = getchar()) != EOF) {
    if (c == ' ') {
        //new word
        ++lenOfWord[charCount];
        charCount = 0;
    }
    else
        //inword
        ++charCount;
}

printf("\n Displaying now... \n");


for (j = 0; j <= MAXIMUM; j++)
    printf("%d: %d", j, lenOfWord[j]); 


}

output:

 JohnJackson@linux-72db:~/Desktop> gcc c.c -o c
 JohnJackson@linux-72db:~/Desktop> ./c
 Starting program, please type then ctrl+d
 Hello my name is john jacksonSegmentation fault

*Segmentation fault on the end is what i get when I do EOF (ctrl+D)

Additional information: GCC compiler from terminal in openSUSE linux

Thanks so much, I'm trying to learn C but I've hit alot of road blocks like these, I'm getting frustrated.

Arpit

The actual problem is

++lenOfWord[charCount];
        charCount = 0;

You have not initialized charCount to 0 before using it. Initialize it to 0 first before the loop

charCount = 0;

while((c = getchar()) != EOF) {

And assigning charCount to 0 in loop is of no use. Remove it.

You have to check one more thing. Since the array size is MAXIMUM. So You have to put a check that charCount does not exceed MAXIMUM. Otherwise, it will again give segmentation fault.

Another problem is in the for loop condition. Instead of

for (i = 0; i < MAXIMUM; i++)

change it to

for (i = 0; i < MAXIMUM; i++)

And

Instead of

for (j = 0; j >= MAXIMUM; j++)

to

for (j = 0; j < MAXIMUM; j++)

Otherwise you won't get any output.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Why am I getting Segmentation fault: 11 in this code

分類Dev

I am trying to search up to the right diagonally in my multidimensional array in java. Why am I getting index out of bounds error?

分類Dev

When calling this .pop method to an array, why am I getting an unexpected .length result

分類Dev

Why I am getting Unresolved variable or type approvedPriceDealerCostForm in my HTML?

分類Dev

Why am I getting column not found in my SQL query?

分類Dev

Why am I getting an error when connecting to MySql in C# .Net Core 3

分類Dev

Why am I getting an uninitalized heap memory error when using a pointer (C++)?

分類Dev

Why am I getting a CASE error when in Data Studio?

分類Dev

Why am I getting duplicated data when scraping?

分類Dev

why am i getting this error when reading a .json file?

分類Dev

Why am I gettng this segmentation fault

分類Dev

Why am I getting an undefined?

分類Dev

Why am I getting this KeyError?

分類Dev

C++ getnstr causes odd segmentation faults

分類Dev

Why am i getting this error when i am trying to insert a string into a list?

分類Dev

why am I getting a none value in my method inside of my model with nested if?

分類Dev

Why am I getting undefined from my onClick event on my anchor tag in React?

分類Dev

I keep getting segmentation fault :11 errors and i can't figure out why... c++

分類Dev

Can I have an array that does not have a limit because I am getting my input by scanner?

分類Dev

I am getting Template Does Not Exist at / when I'm running my server in Django

分類Dev

I am getting "java.lang.ArithmeticException: / by zero" when I run my program

分類Dev

Why am I getting a value back from Long.parseUnsignedLong when I shouldn't be

分類Dev

Scheme: When I cons two atoms I am getting a dot in the middle. Why?

分類Dev

Why am I getting a NoClassDefFoundError in the pdfXFA example?

分類Dev

Why am I getting "svn: command not found"

分類Dev

Why am I getting an Inferred Latch Error?

分類Dev

Why am I getting this deprecated warning?! MongoDB

分類Dev

Why am I getting an error with Find in VBA?

分類Dev

Why am I getting an IndexError from a for loop?

Related 関連記事

  1. 1

    Why am I getting Segmentation fault: 11 in this code

  2. 2

    I am trying to search up to the right diagonally in my multidimensional array in java. Why am I getting index out of bounds error?

  3. 3

    When calling this .pop method to an array, why am I getting an unexpected .length result

  4. 4

    Why I am getting Unresolved variable or type approvedPriceDealerCostForm in my HTML?

  5. 5

    Why am I getting column not found in my SQL query?

  6. 6

    Why am I getting an error when connecting to MySql in C# .Net Core 3

  7. 7

    Why am I getting an uninitalized heap memory error when using a pointer (C++)?

  8. 8

    Why am I getting a CASE error when in Data Studio?

  9. 9

    Why am I getting duplicated data when scraping?

  10. 10

    why am i getting this error when reading a .json file?

  11. 11

    Why am I gettng this segmentation fault

  12. 12

    Why am I getting an undefined?

  13. 13

    Why am I getting this KeyError?

  14. 14

    C++ getnstr causes odd segmentation faults

  15. 15

    Why am i getting this error when i am trying to insert a string into a list?

  16. 16

    why am I getting a none value in my method inside of my model with nested if?

  17. 17

    Why am I getting undefined from my onClick event on my anchor tag in React?

  18. 18

    I keep getting segmentation fault :11 errors and i can't figure out why... c++

  19. 19

    Can I have an array that does not have a limit because I am getting my input by scanner?

  20. 20

    I am getting Template Does Not Exist at / when I'm running my server in Django

  21. 21

    I am getting "java.lang.ArithmeticException: / by zero" when I run my program

  22. 22

    Why am I getting a value back from Long.parseUnsignedLong when I shouldn't be

  23. 23

    Scheme: When I cons two atoms I am getting a dot in the middle. Why?

  24. 24

    Why am I getting a NoClassDefFoundError in the pdfXFA example?

  25. 25

    Why am I getting "svn: command not found"

  26. 26

    Why am I getting an Inferred Latch Error?

  27. 27

    Why am I getting this deprecated warning?! MongoDB

  28. 28

    Why am I getting an error with Find in VBA?

  29. 29

    Why am I getting an IndexError from a for loop?

ホットタグ

アーカイブ