Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8888609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:05:06+00:00 2026-06-14T22:05:06+00:00

Possible Duplicate: How to initialize an array in C initializing an array of ints

  • 0

Possible Duplicate:
How to initialize an array in C
initializing an array of ints

I wonder over the fastest/simplest way to initialize an int array to only contain -1 values. The array I need is 90 ints long so the straightforward way should be to initialize it like this:

int array[90]={-1, -1, -1, ...};

but I only want to use the array once so I want to be able to use it dynamically and be able to free it after using it in the program, so Im more looking for a fast way like calloc, but instead of zeros, -1 of course.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:05:07+00:00Added an answer on June 14, 2026 at 10:05 pm

    If you are using gcc then use designated initializer

    int array[90] = { [ 0 ... 89 ] = -1}
    
    int array[90],i;
    for(i = 0; i < 90 ; arr[i++] = -1);
    

    To do this dynamically , you will have to allocate using malloc then you only free the memory, otherwise freeing the memory which is not allocated by malloc , calloc or realloc is undefined behavior.

    Use this:

    int *array;
    array=malloc(sizeof(int)*n);
    for(i=0;i<n;array[i++]=-1);
    // After use free this
    free(array);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C++ Comma Operator I am initializing an array with int main() {
Possible Duplicate: Any shortcut to initialize all array elements to zero? How to Initializing
Possible Duplicate: Initializing private static members Why I can't initialize non-const static member or
Possible Duplicate: initialize a const array in a class initializer in C++ If I
Possible Duplicate: Recommended way to initialize srand? I am extracting frames from AVI. I
Possible Duplicate: How do I initialize a member array with an initializer_list? You can
Possible Duplicate: Can I list-initialize a vector of move-only type? I use gcc 4.6.1
Possible Duplicate: How to initialize a two-dimensional array in Python? I have always written
Possible Duplicate: C++: Easiest way to initialize an STL vector with hardcoded elements I'm
Possible Duplicate: Recommended way to initialize srand? I have the following problem when using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.