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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:54:07+00:00 2026-05-18T08:54:07+00:00

I know it could be done using malloc , but I do not know

  • 0

I know it could be done using malloc, but I do not know how to use it yet.

For example, I wanted the user to input several numbers using an infinite loop with a sentinel to put a stop into it (i.e. -1), but since I do not know yet how many he/she will input, I have to declare an array with no initial size, but I’m also aware that it won’t work like this int arr[]; at compile time since it has to have a definite number of elements.

Declaring it with an exaggerated size like int arr[1000]; would work but it feels dumb (and waste memory since it would allocate that 1000 integer bytes into the memory) and I would like to know a more elegant way to do this.

  • 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-18T08:54:07+00:00Added an answer on May 18, 2026 at 8:54 am

    This can be done by using a pointer, and allocating memory on the heap using malloc.
    Note that there is no way to later ask how big that memory block is. You have to keep track of the array size yourself.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char** argv)
    {
      /* declare a pointer to an integer */
      int *data; 
      /* we also have to keep track of how big our array is - I use 50 as an example*/
      const int datacount = 50;
      data = malloc(sizeof(int) * datacount); /* allocate memory for 50 int's */
      if (!data) { /* If data == 0 after the call to malloc, allocation failed for some reason */
        perror("Error allocating memory");
        abort();
      }
      /* at this point, we know that data points to a valid block of memory.
         Remember, however, that this memory is not initialized in any way -- it contains garbage.
         Let's start by clearing it. */
      memset(data, 0, sizeof(int)*datacount);
      /* now our array contains all zeroes. */
      data[0] = 1;
      data[2] = 15;
      data[49] = 66; /* the last element in our array, since we start counting from 0 */
      /* Loop through the array, printing out the values (mostly zeroes, but even so) */
      for(int i = 0; i < datacount; ++i) {
        printf("Element %d: %d\n", i, data[i]);
      }
    }
    

    That’s it. What follows is a more involved explanation of why this works 🙂

    I don’t know how well you know C pointers, but array access in C (like array[2]) is actually a shorthand for accessing memory via a pointer. To access the memory pointed to by data, you write *data. This is known as dereferencing the pointer. Since data is of type int *, then *data is of type int. Now to an important piece of information: (data + 2) means "add the byte size of 2 ints to the adress pointed to by data".

    An array in C is just a sequence of values in adjacent memory. array[1] is just next to array[0]. So when we allocate a big block of memory and want to use it as an array, we need an easy way of getting the direct adress to every element inside. Luckily, C lets us use the array notation on pointers as well. data[0] means the same thing as *(data+0), namely "access the memory pointed to by data". data[2] means *(data+2), and accesses the third int in the memory block.

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

Sidebar

Related Questions

Yes, I know you could use regular objects as associative arrays in JavaScript, but
I know I could write scripts and create jobs to run them, but at
I know I could have an attribute but that's more work than I want
Does anyone know of an algorithm that I could use to find an interesting
Does anyone know of an application (hosted or otherwise) that I could use to
(not discussing the table design, its a done deal!) I know how to do
I know you could make a helper pretty easily given the data. So, if
Can you cast a List<int> to List<string> somehow? I know I could loop through
Do you know where I could find some useful third party (free) code snippets
Our dev shop currently uses Visual SourceSafe. We all know how that could end

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.