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 7850125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:45:18+00:00 2026-06-02T18:45:18+00:00

how can i define an array in c which works like vector? This array

  • 0

how can i define an array in c which works like vector? This array should take any amount of values. It can take 0 values or 10 values or 100 values.

The code below works but gives me a runtime error that stack was corrupted.

int i = 0;
    int* aPtr = &i;
    int* head = aPtr;
    for(i=0;i<6;i++){
        (*aPtr)=i;
        aPtr++;
    }

Similarly how can i use char* str to take any amount of characters followed by null character in end to make a string?

Practice for interviews 🙂

  • 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-02T18:45:20+00:00Added an answer on June 2, 2026 at 6:45 pm

    There are many ways to do this in C, depending on your requirements, but you said “any number of values” (which usually means as many as will fit in memory). That’s commonly done using realloc to grow the size of an array dynamically. You’ll need to keep some bookkeeping information too on the size of the array as it grows.

    void
    store (vector_t * v, int idx, int value)
    {
      if (v->size < idx) {
         v->size = idx * 2;
         v->data = realloc(v->data, v->size);
      }
      v->data[idx] = value;
    }
    

    This being tagged “homework”, I’ve left some details to fill in such as the definition of vector_t.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When key name starts with digit, in javascript we can define array-like object like
How can I define a array of boost matrices as a member variable? None
I need an array to hold member-function-pointers of different classes. How can I define
I can define default value in domain by this way : class ProcessingPriority {
In HTML/CSS you can define a style which can be applied to many types
When I create a custom component, I define a property which is array that
I am using a line like this: Array stringArray = Array.CreateInstance(typeof(String), 2, 2); to
on several places in same controller i have code like: $publications = $this->paginate('Publication', $conditions);
As you can't have an array in a define or a class constant I
I'd like to ask how can I do some code before I build the

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.