1

I first initialize 0 to counter[10], and it is OK. However, somewhere I want to re-initialize with 0 again but fail.

The error message is

[Warning] extended initializer lists only available with -std=c++11 or -std=gnu++11

#include <stdio.h>
#include <stdbool.h>

int main(void) {

  int digit, counter[10] = {0}; //Declare variable "digit" to store individual number to be compared and declare an array for the input numbers
  bool rep_digits; //Declare a boolean variable to determine whether the input numbers has repetitive numbers or none
  int n; //Declare variable "n" to store the input numbers

  while (true) { //This while loop serves as a loop for the user input
    
    printf("\nEnter a number (capped at  on this compiler): ");
    scanf("%d", &n);
    if (n == 0) { //The user input loop terminates when the user input a 0
    break;
    }
    
    printf("Repeated digits(s): \n");

    while (n > 0) { //If the condition is true, execute the arguments inside
        digit = n % 10; //Obtain the remainder of the input number
        counter[digit]++; //Increase the counter for the obtained remainder in the array
        if (counter[digit] == 2) { //If the counter of that particular remainder is equal (and only equal) to 2, print out the number
            printf("%2d\n", digit); 
            rep_digits = true;  //Set the boolean variable to true if there is a repetitive number in the input
        }
        n /= 10; //Divide the input number by 10 to determine the next number either repetitive or not
    }
        
    counter[10] = {0}; // re-initialize to 0
        
    if (rep_digits == false) { //If the boolean variable stays false then display this message
        printf("No repeated digits\n");
    } 
  }

  return 0;
}
1
  • You can't do that. If you want to set all the values to 0 you could use a loop, or you could use memset to set all the bytes to 0. You could also move the declaration with the initialization inside the while loop Commented Nov 11, 2020 at 5:07

2 Answers 2

2

counter[10] = {0}; writing beyond the array size causes undefined behavior.

suppose you have array size as int counter[10], you should write only from counter[0] till counter[9]

if you want to initialize all the array elements to 0, then you can do it two ways.

  1. int counter[10] = {0}; \\this works only at the same place where you declare
  2. memset(counter,0,sizeof(counter)); \\ this can be done at any other place in the program

In your program replace counter[10] = {0}; with memset(counter,0,sizeof(counter));

it should work just fine.

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

5 Comments

@RetiredNinja, thanks dint have a proper look at the program before, updated the answer properly by giving explanation
Thank you for the response! I was looking for an alternative way to re-initialize my array elements to 0 without it going through declaration again. You also pointed out my dumb mistake that after declaration, the array "counter[10] = {0}" where 10 is actually the index not the size of the array anymore.
@BrandonJ, up voting is the only way of saying thank you here :)
I tried to vote ur answer and verify it, but I am still new to stackoverflow, it says I need more reputation
I think below the down vote option there is an option to accept as answer you can click on that
2

Instead of using int counter[10] = {0}

Use memset int counter[10]; memset(counter,0,sizeof(counter));

The above memset line fills every value of counter array to 0.

1 Comment

Worth noting that memset works on bytes, not ints, so replace that 0 with a 1 and the result will not be an array of values set to 1.

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.