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

  • SEARCH
  • Home
  • 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 7549193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:53:23+00:00 2026-05-30T09:53:23+00:00

I am trying to take in array which contains some integer values/string values from

  • 0

I am trying to take in array which contains some integer values/string values from the user, and not sure how long the array is going to be.. If i intitalise like array[500], i know it is a poor solution and poor programming skills.. How do i improve this?

Sample code below:

int main(void){
    int t=0;
    char str[200];
    int  count[20];
    printf("Please enter the number of test cases(between 1 to 20):");
    scanf("%d",&t);
    for ( int i = 1; i<=t;i++)
    {
        printf("Please enter each of %i test case values:",i);
        gets(str);
        //set_create(str);
        printf("\n");
    }
    for(int i = 0;i<t;i++)
    {
        prinf("%i",count[i]);
        printf("\n");
    } 
    return 0;
}

The code above is wrong definitely.. Need some help to improve the code…Thanks

Edited code:

 int main(void){
        int T=0;
        int *count; 
        printf("Please enter the number of test cases(between 1 to 20):");
        scanf("%d",&T);
        count = malloc(T * sizeof *count);
        for ( int i = 1; i<=T;i++)
        {
            printf("Please enter each of %i test case values:",i);
            fgets(count);
            printf("\n");
        }

        return 0;
    }
  • 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-05-30T09:53:24+00:00Added an answer on May 30, 2026 at 9:53 am

    Just use a pointer and malloc / realloc memory as needed. Don’t use gets – it’s unsafe and no longer in the standard. Use fgets instead.

    For example, if you don’t know how many count elements you need:

    int *count;
    
    scanf("%d", &t);
    count = malloc(t * sizeof *count);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a recursive method which may take a array/value as input
I'm trying to create a function that will take a string which could be
I'm trying to load some decimal values from a file but I can't work
I'm trying to create a custom JSP tag that would take an array object
Im trying to create a method that take 2 int array as the input
I am trying to take the string <BR> in VB.NET and convert it to
I'm trying to take 21 bytes of data which uniquely identifies a trade and
I'm trying to take my code to the next level. Following some best practices
Here's what I'm trying to do. I'm having the user input a city, which
I am using an NSArray which contains CGRect values. Is there any way to

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.