0

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.

4
  • 1
    Your immediate problem is that i is unitialized when you use it to index the array. But the code has more problems ... Commented Oct 5, 2015 at 15:24
  • comparing an int with a null character doesn't seem a very sensible thing to do in any case. Commented Oct 5, 2015 at 15:41
  • @TomTanner It's probable that the OP saw a similar implementation of strlen() or something, I don't know why if you come from a high level programming language you always want to avoid passing the length of the array. Commented Oct 5, 2015 at 15:42
  • I reversed the OP's last edit, please do not fix the code in place in the question, it makes the answers irrelevant and the question pointless. Commented Oct 5, 2015 at 17:21

5 Answers 5

3

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.

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

Comments

3

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.

3 Comments

The real cause was hiding in plain sight! 2. alone did not seem sufficient to warrant a crash.
@chqrlie Yeah , that's true.
@pradyumna rahul: Another illustrative case of misusing compiler warnings. Use all the help the compiler can give you for free: gcc -Wall -W -Werror or clang -Weverything would have pointed error 1., albeit probably not error 2.
0

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.

Comments

0

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;
}

Comments

0

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);
}

6 Comments

Your implementation of sumOccur is risky: it uselessly assumes count > 0. It is good practice to implement utility functions with as few assumptions as possible.
Yes absolutely, but the way I call it in main() that's very unlikely going to happen, but you are right.
@chqrlie not anymore, I did notice that!
Passing the size before the array is questionable : it is inconsistent with most similar APIs from the C library.
@chqrlie That is an irrelevant argument. Why is it bad? And what similar APIs?
|

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.