简体   繁体   中英

Unknown Segmentation Fault Error in C

When I compile my code everything compiles successfully, but when I run my code I receive a "segmentation fault error". What is causing this error? As far as I can tell I've dotted all my i's and crossed all my t's.

-I edited my code to reflect some of the suggested changes but still get the same error- My code is as follows:

#include <stdio.h>
int main(void)
{
    int a[100];
    int n = 0;
    int i = 0;
    int *even_elt_sum_ptr = 0;
    int *odd_elt_sum_ptr = 0;

    printf("Enter size of array: ");
    scanf("%d", &n);

    printf("Enter elements in array: ");
    for(i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }//end of for loop

    sum(a, n, even_elt_sum_ptr, odd_elt_sum_ptr);

}//end of main

int sum(int a[], int n, int *even_elt_sum_ptr, int *odd_elt_sum_ptr)
{
    int i = 0;
    for(i = 0; i < n; i++)
    {
        if (a[i] % 2 == 0)
        {
            *even_elt_sum_ptr += a[i];
        }//end of if statement
        else
        {
            *odd_elt_sum_ptr += a[i];
        }//end of else statement

    }//end of for loop
    printf("%d\n", *even_elt_sum_ptr);
    printf("%d\n", *odd_elt_sum_ptr);

}//end of sum function

You declared the sum -function to take an array of integers, named a .

However, when you call the function, you are only passing a single integer, and an invalid one at that!

int a[100];         // Declare an array: valid indicies are [0] to [99]
sum(a[100], .... ); // Try to pass the single integer, a[100], which is not valid!

int sum(int a[], ... ) // The function expects an ARRAY, not a single, undefined value.

Instead of this line:

sum(a[100], n, *even_elt_sum_ptr, *odd_elt_sum_ptr);

you should write this line:

sum(a, n, even_elt_sum_ptr, odd_elt_sum_ptr);

a is already an array (you wrote int a[100] ). By writing a[100] as an argument, you mean "Consider a as an array, take the 101th element an give it as the argument", so you give an int not an int array).

Also, the 3rd and 4th arguments are already pointers (declared as int *... ), by putting a star when passing them as arguments, you actually dereference them, ie take the int the pointers point to.

These error should be indicated by your compiler (actually, your compiler should indicate warnings, but in C, if you don't exactly know what you are doing, you should consider warnings the same as errors).

For starters you have to declare the function sum before its usage in main .

The function sum is declared as having the return type int but returns nothing.

The array

int values[10];

is not used in the program. And moreover this statement

values[10] = values[n];

does not make sense. It seems you are trying to redefine the array in this statement however arrays do not have the assignment operator and may not be redefined.

These declarations

int *even_elt_sum_ptr = 0;
int *odd_elt_sum_ptr = 0;

also do not make sense. You should declare at least objects of the type int

int even_elt_sum = 0;
int odd_elt_sum = 0;

and pass them to the function sum by reference as for example

sum(a, n, &even_elt_sum, &odd_elt_sum);

It seems what you mean is the following

#include <stdio.h>

#define N   100

void sum( const int a[], size_t n, long long int *even_elt_sum_ptr, long long int *odd_elt_sum_ptr);

int main(void)
{
    int a[N];
    long long int even_elt_sum;
    long long int odd_elt_sum;

    size_t n = N;

    printf( "Enter size of array (no more than %zu): ", ( size_t )N );
    scanf( "%zu", &n);

    if ( n == 0 || N < n ) n = N;

    printf( "Enter %zu elements in array: ", n );
    for ( size_t i = 0; i < n; i++ )
    {
        scanf("%d", &a[i]);
    }//end of for loop

    sum( a, n, &even_elt_sum, &odd_elt_sum );

    printf( "%lld\n", even_elt_sum );
    printf( "%lld\n", odd_elt_sum );
}//end of main method for problem 4

void sum( const int a[], size_t n, long long int *even_elt_sum_ptr, long long int *odd_elt_sum_ptr )
{
    *even_elt_sum_ptr = 0;
    *odd_elt_sum_ptr = 0;

    for ( size_t i = 0; i < n; i++ )
    {
        if ( a[i] % 2 == 0 )
        {
            *even_elt_sum_ptr += a[i];
        }//end of if statement
        else
        {
            *odd_elt_sum_ptr += a[i];
        }//end of else statement
    }//end of for loop
}//end of sum function

The program output might look like

Enter size of array (no more than 100 ): 10
Enter 10 elements in array: 0 1 2 3 4 5 6 7 8 9
20
25

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM