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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:51:53+00:00 2026-06-15T06:51:53+00:00

In the following program, file_ptr is NULL, yet it is being initialized properly. Why?

  • 0

In the following program, file_ptr is NULL, yet it is being initialized properly. Why?

#include <stdio.h>
#include <stdlib.h>


void Fopen(const char * restrict filePath, const char * restrict mode, FILE * restrict filePtr);

int main(int argc, char ** argv)
{
    FILE * file_ptr = NULL;
    Fopen("dummy.dat", "r", file_ptr);
    printf("File pointer is %p\n", file_ptr);

    exit(0);
}

void Fopen(const char * restrict filePath, const char * restrict mode, FILE * restrict filePtr)
{
    if ( (filePtr = fopen(filePath, mode)) != NULL)
            printf("file pointer is %p\n", filePtr);
}

On Command Line:

[jim@cola c++]$ ./readFile
file pointer is 0x740010
File pointer is (nil)

The only explanation is that a copy of FILE * is being passed to Fopen. How can i pass pointer by ref?

  • 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-15T06:51:54+00:00Added an answer on June 15, 2026 at 6:51 am

    Your Fopen function changes the value of its local filePtr and then throws that value away. Nothing in your code can change the value of file_ptr in the caller. You just pass a NULL to the function.

    You want this:

    FILE* Fopen(const char * restrict filePath, const char * restrict mode)
    {
        FILE* filePtr;
        if ( (filePtr = fopen(filePath, mode)) != NULL)
                printf("file pointer is %p\n", filePtr);
        return filePtr;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I write the following program: file 1: #include <stdio.h> int global; void print_global1()
I wrote the following program: #include <stdio.h> int main() { char *a = Abcdefg;
Following program: #include <stdlib.h> #include <stdio.h> #include <sys/stat.h> #include <sys/types.h> int main() { fclose(
I have the following program: #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXLEN 100
Is the following program a valid C program? #include <stdio.h> int main() { fwrite(x,
I have the following program, int iIndex=0; char cPort[5]={\0}; char cFileChar; fopen_s(&fFile,c:\\Config\\FileName.txt,r); if(fFile !=0)
I tried to compile following program (main.cu) with the nvcc (CUDA 5.0 RC): #include
So I have the following program: int main(){ char* one = computer; char two[]
i have following program to calculate size of file #include <iostream> #include <fstream> #include
I wrote the following program using VS2008: #include <fstream> int main() { std::wofstream fout(myfile);

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.