0

I'm Just trying to run a simple program to count the number of spaces, digits and other characters using arrays. Below is my program:

void main(){


    int digit_holders[10]={0};
    int ch;
    int i, white_space=0,other=0;

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

            if(isspace(ch))
                    white_space++;
            else if(isdigit(ch))
                    digit_holders[ch-'0']++;
            else
                    other++;
    }
    digit_holders[12]=20;
    printf("\n White spaces=%d\n Other=%d\n",white_space,other);
    for(i=0;i<=9;i++)
            printf("\ndigit_holders[%d]=%d\n",i,digit_holders[i]);
    printf("\n digit_holder[12]=%d\n",digit_holders[12]);
}

2 Questions:

  1. Why does digit_holders[12] still manage to print the assigned vale despite it being outside the range? Why doesn't it display a segmentation fault ?The same happens when I change the for loop check to i<=11 it manages to print digit_holders[11]=0 ( which it shouldn't) .. however when I replace 11/10 with 1100 i.e digit_holders[1100] in either of the case, the program crashes ( segmentation fault). Why so ?
  2. Is there an easier way to print the elements of this array without using for loop ?

-Thanks!

1
  • No, you will need a loop of some kind to print the content of your array in C. You are assigning the 12th element of an array with only 10 elements declared, its risky to do and will be unstable, but won't necessarily result in a seg fault because if the OS hasn't modified it in the time between write and read your pointer will resolve the value without error. But again, risky. Commented Nov 1, 2014 at 11:08

3 Answers 3

1

There is no range checking in C so it gives it is best shot (I.e enough rope to hang yourself and the rest of the family).

Segmentation fault occurs from the OS. i.e. trying to access memory not assigned to the process.

Sign up to request clarification or add additional context in comments.

Comments

1

As I wrote in the comment:

You will need a loop of some kind to print the content of your array in C.

You are assigning the 13th element of an array with only 10 elements declared, its risky to do and will be unstable, but wont necessarily result in a seg fault because if the OS hasnt modified it in the time between write and read, your pointer will resolve the value without error. But again, risky.

If you had declared an array with 13 elements, all 13 will be reserved in memory and there will be no chance of a seg fault. You are likely to get a seg fault if you interrogate an array outside of its declared limits, more so the further you go away from the range you defined.

Comments

0

It's possible to print "out-of-range" array indices in C because digit_holders[12] is handled as though digit_holders were a pointer with an offset of 12 * sizeof(int) added on to it.

This won't necessarily cause a segmentation fault, since the memory address being read might still be earmarked as being "available" to the program. A good example of this is a struct containing an array:

typedef struct {
    int some_array[12];
    int another_int;
} my_struct;

If you were to read the value of some_array[12], you'd get the contents of another_int instead, since it's stored directly after the last "valid" index of some_array.

As for the printing out of arrays: I'm not aware of any way to print out an array without using a loop in C, though it is possible in some other languages. For example, you could easily print out a list of strings in Python using print(", ".join(["This", "That", "The other"]))

Comments

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.