Rahul
Rahul

Reputation: 81

Code crashes at the printf statement

The code runs until it reaches the statement:

printf("%d", sumOccur(input));

The code:

#include <stdio.h>
#include <stdlib.h>
int sumOccur(int A[]);

int main(){
    int input[6] = {1,1,1,2,2,3};
    printf("%d", sumOccur(input));
    return 0;
}

int sumOccur(int A[]) {
int sum, i;
  while(A[i]!='\0'){
    sum += A[i];
    i++;
  }
  return sum;
}

If I have made any silly mistakes please oblige.

Upvotes: 0

Views: 105

Answers (5)

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53026

You are iterating while A[i] != '\0' but there is no '\0' in the array and also you never initialize sum which is unlikely the cause for a crash but it could be.

You need to pass the number of elements in the array, like this

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

int sumOccur(size_t count, const int *A);
int sumOccurCHQrlieWay(const int *A, size_t count);

int main()
{
    int input[] = {1, 1, 1, 2, 2, 3};
    printf("%d", sumOccur(sizeof(input) / sizeof(*input), input));
    return 0;
}

int sumOccur(size_t count, const int *A) 
{
    int sum;
    sum = 0;
    for (size_t i = 0 ; i < count ; ++i)
        sum += A[i];
    return sum;
}

int sumOccurCHQrlieWay(const int *A, size_t count)
{
    return sumOccur(count, A);
}

Upvotes: 0

machine_1
machine_1

Reputation: 4454

Note that you are dealing with an int array,which means it normally won't contain '\0' character.To iterate over the array you need to specify number of elements.Here is the correct way :

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

int sumOccur(int A[],size_t number_of_elemets);

int main(){

    int input[6] = {1,1,1,2,2,3};

    //Get the number of elements
    size_t n = sizeof(input) / sizeof(int);

    printf("%d", sumOccur(input,n));

    return 0;
}

int sumOccur(int A[],size_t number_of_elements) {

    int sum = 0;

    size_t i = 0;

    while( i < number_of_elements )
    {
        sum += A[i];
        i++;
    }
    return sum;
}

Upvotes: 0

ameyCU
ameyCU

Reputation: 16607

In your function int sumOccur you have two problems-

1. sum and i are not initialized just declared. Initialize both to 0 .

2. Also while(A[i]!='\0') ain't going to work as expected as your array doesn't have that value in it.

Upvotes: 3

Marc B
Marc B

Reputation: 360872

It's not the printf() crashing. It's sumOccur(). Your array has no \0 value in it, so your while() never terminates and you end up in a near-infinite loop and run off the end of the array.

The array is an array of numbers, not a string, so there is no reason whatsoever to think there there would be a null-terminator on the values. null terminators are for strings, not arrays of numbers.

Upvotes: 3

chqrlie
chqrlie

Reputation: 145297

Your code invokes undefined behaviour: you access A[6] and subsequent inexistent entries in sumOccur trying to find a final 0 in the array, but you do not put one in the definition of input in the main function.

-------- cut here if you are not interested in gory implementation details --------

The array is allocated on the stack, very near the top since it is instantiated in the main function. Reading beyond the end until you find a 0 likely tries to read beyond the end of the stack pages and causes a segmentation fault.

Upvotes: 0

Related Questions