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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:22:33+00:00 2026-05-23T13:22:33+00:00

I have a function that scans a file and returns the number of the

  • 0

I have a function that scans a file and returns the number of the lines along with the lines in a string array, my function looks like this :

int load_lines(char* _file, char** _array){
    FILE *infile;
    char line_buffer[BUFSIZ];
    char line_number;

    infile = fopen(_file, "r");

    ine_number = 0;
    while (fgets(line_buffer, sizeof(line_buffer), infile)) 
        ++line_number;

    fclose(infile);
    _array = malloc (line_number * sizeof(char*));
    infile = fopen(_file, "r");
    line_number = 0;

    while (fgets(line_buffer, sizeof(line_buffer), infile)) {
        _array[line_number] = malloc(strlen(line_buffer) + 1);
        strcpy(_array[line_number], line_buffer);

        //a printf on _array[line_number] works fine here
        ++line_number;
    }

    return line_number;
}

When I call it like this:

char** _array;
line_number = load_lines(inname, _array);

_array[0];

I get a segmentation fault since the array seems to be not allocated after the return of the function.

  • 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-23T13:22:33+00:00Added an answer on May 23, 2026 at 1:22 pm

    When you pass an argument into a function, the function always works on a copy of that argument.

    So in your case, load_lines is working on a copy of _array. The original _array is not modified:

    char** _array = NULL;
    printf("%p\n", _array); // Prints "0x0000"
    line_number = load_lines(inname, _array);
    printf("%p\n", _array); // Prints "0x0000"
    

    To modify _array, you need to pass a pointer to it:

    int load_lines(char* _file, char*** _array){
        ...
        (*array) = malloc (line_number * sizeof(char*));
        ...
        (*array)[line_number] = malloc(strlen(line_buffer) + 1);
    }
    
    char** _array = NULL;
    line_number = load_lines(inname, &_array);
    

    [However, any time you find yourself needing a triple pointer (i.e. ***), it’s time to reconsider your architecture.]

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

Sidebar

Related Questions

In my Java code I have function that gets file from the client in
I have a function that outputs an XML string: <expensesAC> <cashflow> <month>6</month> <cash>300</cash> <projected>null</projected>
I have a parser function written in my code-behind file, that takes as input
I have a function that I'm using while(true) to repeatedly scan memory addresses of
So I have function that formats a date to coerce to given enum DateType{CURRENT,
I have function Start() that is fired on ready. When I click on .ExampleClick
I have a function that accepts a class (not an instance) and, depending on
I have a function that is being called from different threads in the application.
I have a function that allows users to edit a row in a table
I have a function that is recursively calling itself, and i want to detect

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.