0

I am learning array and just wrote this small program to see how it works. but its crashing with segmentation faul which i understand mean i am writing my variable / function to an memory place not allotted to it. But I cant figure how. Can anyone let me know please? i am calling introArray from my main().

int introArray (void)
{
    int total, ctr;

    printf("enter how many students \n");
    scanf("%d", &total);

    int students[total];
    ctr = 0;

    while ( students[ctr] <= total)
    {
        printf("enter student %d DOB in mmddyy \n", ctr );
        scanf("%d", students[ctr]);
        ctr++;
    }

    return 0;

}

1
  • Which compiler are you using to build this program? Commented Mar 19, 2013 at 22:49

4 Answers 4

2

In your code, there is one implementation logic issue. The total number of students is total and hence, your while loop should be

while(ctr < total)

The data to be read also should scanf("%d", &students[ctr]); There is an ampersand missing

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

2 Comments

thanks ganesh. I made the recommended changes and its still crashing with same segementation fault error. Any idea why?? Also, why is ampersand needed when i am dereferencing with [] ??
@user2152138 If you haven't used ampersand then there is a likelihood that your code will crash. When you dereference an array element as students[ctr], then you are actually accessing the corresponding value. However, your objective is to read a value through scanf and for this you need to pass the address of the element. Hence, you should use &students[ctr].
0

ctr goes beyond total. This way you are going out of bound Change the loop to

while (ctr < total)
{
        printf("enter student %d DOB in mmddyy \n", ctr );
        scanf("%d", &(students[ctr]));
        ctr++;
}

Comments

0

I think

scanf("%d", students[ctr]);

should be

scanf("%d", &students[ctr]);

1 Comment

@Armin Only the second one is right. In the first snippet scanf would try to write at address students[ctr], which is some garbage value (or maybe zero, depending on the compiler). In the second snippet scanf will write at the address of the ctr element of the students array, which is correct.
0

This line

while ( students[ctr] <= total)

is not protection against reading past your array bounds inside the loop. This will stop you reading past the end of your array provided you use ctr as your index

while ( ctr < total)

you need the strict inequality as array indices are zero based.

In addition, your scanf call inside your while loop is wrong - the second argument should be a pointer and currently you pass an integer. It should be

scanf("%d", &students[ctr]);

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.