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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:28:41+00:00 2026-06-13T11:28:41+00:00

is it possible to ‘dynamically’ allocate file pointers in C? What I mean is

  • 0

is it possible to ‘dynamically’ allocate file pointers in C?
What I mean is this :

FILE **fptr;
fptr = (FILE **)calloc(n, sizeof(FILE*));

where n is an integer value.
I need an array of pointer values, but I don’t know how many before I get a user-input, so I can’t hard-code it in.
Any help would be wonderful!

  • 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-13T11:28:42+00:00Added an answer on June 13, 2026 at 11:28 am

    You’re trying to implement what’s sometimes called a flexible array (or flex array), that is, an array that changes size dynamically over the life of the program.) Such an entity doesn’t exist among in C’s native type system, so you have to implement it yourself. In the following, I’ll assume that T is the type of element in the array, since the idea doesn’t have anything to do with any specific type of content. (In your case, T is FILE *.)

    More or less, you want a struct that looks like this:

    struct flexarray {
        T *array;
        int size;
    }
    

    and a family of functions to initialize and manipulate this structure. First, let’s look at the basic accessors:

    T  fa_get(struct flexarray *fa, int i) { return fa->array[i]; }
    void fa_set(struct flexarray *fa, int i, T p) { fa->array[i] = p; }
    int fa_size(struct flexarray *fa) { return fa->size; }
    

    Note that in the interests of brevity these functions don’t do any error checking. In real life, you should add bounds-checking to fa_get and fa_set. These functions assume that the flexarray is already initialized, but don’t show how to do that:

    void fa_init(struct flexarray *fa) {
        fa->array = NULL;
        fa->size = 0;
    }
    

    Note that this starts out the flexarray as empty. It’s common to make such an initializer create an array of a fixed minimum size, but starting at size zero makes sure you exercise your array growth code (shown below) and costs almost nothing in most practical circumstances.

    And finally, how do you make a flexarray bigger? It’s actually very simple:

    void fa_grow(struct flexarray *fa) {
        int newsize = (fa->size + 1) * 2;
        T *newarray = malloc(newsize * sizeof(T));
        if (!newarray) {
            // handle error
            return;
        }
        memcpy(newaray, fa->array, fa->size * sizeof(T));
        free(fa->array);
        fa->array = newarray;
        fa->size = newsize;
    }
    

    Note that the new elements in the flexarray are uninitialized, so you should arrange to store something to each new index i before fetching from it.

    Growing flexarrays by some constant multiplier each time is generally speaking a good idea. If instead you increase it’s size by a constant increment, you spend quadratic time copying elements of the array around.

    I haven’t showed the code to shrink an array, but it’s very similar to the growth code,

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

Sidebar

Related Questions

Possible Duplicate: php == vs === operator Reference - What does this symbol mean
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: When does Endianness become a factor? reading this tuto on endianess, i
Possible Duplicate: Realloc is not resizing array of pointers Can anyone tell me where
Possible Duplicate: Turning on camera flash LED in Android? I need to turn on
Possible Duplicate: how to delete a file? My application first reads the fields in
Possible Duplicate: What is the fastest XML parser in PHP? I still need to
Possible Duplicate: oracle PL/SQL: sort rows I run this query: Select a.product, sum( case
Possible Duplicate: array_splice() for associative arrays How to add an array value to 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.