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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T13:28:54+00:00 2026-06-18T13:28:54+00:00

How would I go about dynamically allocating memory to char** list in this function?

  • 0

How would I go about dynamically allocating memory to char** list in this function?

Basically the idea of this program is I have to read in a list of words from a file. I cannot assume max strings or max string length.

I have to do other stuff with the C-strings but that stuff I should be fine with.

Thanks!

void readFileAndReplace(int argc, char** argv)
{
    FILE *myFile;
    char** list;
    char c;
    int wordLine = 0, counter = 0, i;
    int maxNumberOfChars = 0, numberOfLines = 0, numberOfChars = 0;

    myFile = fopen(argv[1], "r");

    if(!myFile)
    {
        printf("No such file or directory\n");
        exit(EXIT_FAILURE);
    }

    while((c = fgetc(myFile)) !=EOF)
    {
        numberOfChars++;
        if(c == '\n')
        {
            if(maxNumberOfChars < numberOfChars)
                maxNumberOfChars += numberOfChars + 1;

            numberOfLines++;
        }
    }

    list = malloc(sizeof(char*)*numberOfLines);

    for(i = 0; i < wordLine ; i++)
        list[i] = malloc(sizeof(char)*maxNumberOfChars);


    while((c = fgetc(myFile)) != EOF)
    {
        if(c == '\n' && counter > 0)
        {
            list[wordLine][counter] = '\0';
            wordLine++;
            counter = 0;
        }
        else if(c != '\n')
        {
            list[wordLine][counter] = c;
            counter++;
        }
    }
}
  • 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-18T13:28:55+00:00Added an answer on June 18, 2026 at 1:28 pm

    Do like this:

    char** list; 
    
    list = malloc(sizeof(char*)*number_of_row);
    for(i=0;i<number_of_row; i++) 
      list[i] = malloc(sizeof(char)*number_of_col);  
    

    Additionally, if you are allocating memory dynamically. you are to free it as work done:

    for(i=0;i<number_of_row; i++) 
      free(list[i] );
    free(list);  
    

    EDIT

    In you revised question:

     int wordLine = 0, counter = 0, i;    
    

    wordLine and counter are 0

    before this code:

    list = malloc(sizeof(char*)*wordLine+1);
    for(i = 0;i < wordLine ; i++)
       list[i] = malloc(sizeof(char)*counter);  
    

    you have to assign value to wordLine and counter variable

    Also memory allocation should be before the following loop(outside):

     while((c = fgetc(myFile)) != EOF){
      :
      :
     }
    

    EDIT:

    New your third version of question. You are reading file two times. So you need to fseek(), rewind() to first char before second loop starts.

    try with:

    fseek(fp, 0, SEEK_SET); // same as rewind()
    rewind(fp);             // same as fseek(fp, 0, SEEK_SET)
    

    also I have doubt in your logic to calculate numberOfLines and maxNumberOfChars. please check that also

    EDIT

    I think your calculation for maxNumberOfChars = 0, numberOfLines = 0 is wrong try like this:

    maxNumberOfChars = 0, numberOfLines = 0, numberOfChars = 0;
    while((c = fgetc(myFile)) !=EOF){
         if(c == '\n'){
             numberOfLines++; 
             if(maxNumberOfChars < numberOfChars)
                 maxNumberOfChars = numberOfChars;
             numberOfChars=0
         }
         numberOfChars++;
    }    
    

    maxNumberOfChars is max number of chars in a line.

    Also change code:

    malloc(sizeof(char)*(maxNumberOfChars + 1));  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would I go about dynamically adjusting the height of a UIScrollView ? Basically
Anyone have any idea how I would go about converting a timestamp in milliseconds
How would I go about dynamically allocating a multi-dimensional array?
How would I go about dynamically adding commas as a user is entering numbers?
I would like 'about' to route to 'abouts/1' I tried this: match 'about' =>
Just a quick question about how you would go about implementing this. I want
I have a question about how to dynamically generate JTrees. Is there a way
I hope many of you would have heard about Flipboard . One of the
I need your help, How would I go about creating a javascript function that
This might be a horrifically stupid question, but how would I go about doing

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.