Passing pointer of a pointer in C

willywill997

I am currently studying how to work with pointers in C, and I have several questions if you dont mind.

I am trying to process information from files, therefore - to maintain modularity - I want to "send my files" into several helper methods.

My questions are:

  1. I know that if I create these file pointers inside a method - they are kept inside the stack. Is it problematic (In a sense that it may end in unwanted behaviours) to send pointers from the stack memory to other methods?

  2. I know that if i'll send the pointers of the files to other methods, it will actually create a copy of them, which means that changing them inside the method would not do anything at all.

Thats why im asking about "pointers to pointers", Ive read (but didn't quite get it) that I could send the helper methods pointers to the file pointers - and in that way, I would work on my actual files, rather than some local copy.

Lets say that mainFunction is my main method (it is not in the "real" main method) which I use for processing files

void helperFunction1(FILE* file1, FILE* file2)
{
        fclose(outputFile);
        fclose(inputFile);
}

void helperFunction2(FILE* file1, FILE* file2)
{
     **write some stuff into both files**
}

void mainFunction()
{
    FILE* inputFile = fopen(filePath, "r");
    FILE* outputFile = fopen(OUTPUT_FILE_NAME, "w");
    helperFunction2(inputFile, outputFile);
    helperFunction1(inputFile, outputFile);
}

Would the "real" inputFile and outputFile get closed from calling to helpFunction1?

Would the "real" inputFile and outputFile get modified (get some stuff written into them) from calling to helpFunction2?

I would love to get some insight / answers for my questions (I hope they are valid and not too pushy) and also I would love to get a brief explanation on how to work with "pointers to pointers" in order to modify data, rather than modifying some copies of it.

John Bode

TL/DR - For what you're trying to do, helperFunction1 and helperFunction2 are declared correctly, you just need to use the correct names in helperFunction1.

Longer version

If you want your function to modify the pointer object itself, then you must pass a pointer to that pointer. Here's a contrived example where we want to update a FILE * object:

void open( FILE **ptr, const char *name, const char *mode )
{
  *ptr = fopen( name, mode );
}

int main( void )
{
  FILE *in;   // in stores the address of a FILE object
  FILE *out;  // out stores the address of a FILE object

  open( &in, "input.txt", "r" );   // we are changing the values of in and out
  open( &out, "output.txt", "w" ); // so we must pass pointers to those objects
  ...
}

We want open to modify the objects in and out, so we pass pointers to those objects.

Compare that to something like this:

void write( FILE *ptr, const char *text )
{
  fwrite( ptr, "%s", text );
}

In this case, we're not trying to change the file pointer itself, we're just trying to write to the FILE stream it points to, so we don't need to worry about a pointer to a pointer in this case. Another contrived example:

int main( void )
{
  FILE *out;
  ...
  write( out, "some text" ); // we are not changing the value of out,
  ...                        // so we do not pass a pointer to it
}

For any type T, if we want a function to modify an object of that type, we must pass a pointer to that object:

void foo( T *ptr )
{
  *ptr = new_T_value(); // write a new value to the thing ptr points to
}

int main( void )
{
  T var;
  foo( &var ); // write a new value to var
}

This works exactly the same way for pointer types - replace T with P *:

void foo( P * *ptr ) // or just P **ptr
{
  *ptr = new_Pstar_value(); // write a new *pointer* value to the thing ptr points to
}

int main( void )
{
  P * var;
  foo( &var ); // write a new value to var
}

Again, this is only true if you want to modify the value of var.

You can go with even higher levels of indirection - replace P with Q *:

void foo( Q * * *ptr ) // or just Q ***ptr
{
  *ptr = new_Qstarstar_value(); // write a new *pointer* value to the thing ptr points to
}

int main( void )
{
  Q * * var;   // or just Q **var
  foo( &var ); // write a new value to var
}

The expression *ptr in foo has the same type as the expression var in main, so writing to *ptr is equivalent to writing to var.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

passing a pointer address vs pointer to a pointer in C

From Dev

Is passing pointer to an array as pointer to pointer UB in C?

From Dev

Passing pointer by reference in C

From Dev

C - Passing pointer to function

From Dev

Passing a pointer to a pointer vs passing a pointer for initialisation in C

From Dev

C++: Passing lambda pointer as a function pointer

From Dev

passing function pointer in c with a pointer argument

From Dev

Passing single pointer and double pointer to a function in c

From Dev

C++ passing pointer reference

From Dev

Passing a pointer to an array in a function in C

From Dev

passing array (not pointer) to a struct in c

From Dev

C++ passing function pointer

From Dev

Passing Function Pointer in C#

From Dev

Passing String Pointer Array in C

From Dev

Passing a pointer (string) to a C function

From Dev

confusion in passing a pointer to function in C

From Dev

Passing pointer of an array to a function in C

From Dev

passing a pointer through a function c

From Dev

Passing a void* pointer and then cast in C

From Dev

c++ passing by const pointer or reference to const pointer

From Dev

Boost C++. Passing a member function pointer with signature pointer to function

From Dev

Passing pointer from C# to C++

From Dev

Passing a Pointer as an Argument in C/C++?

From Dev

Passing pointer to pointer and reallocate space

From Dev

Incompatible pointer type when passing a pointer of pointer

From Dev

Passing Array of Structs to a Function as a Pointer (C)

From Dev

c++ passing template param by shared pointer

From Dev

Passing value to pointer parameters in C++

From Dev

passing a pointer of different type to a function (C)

Related Related

HotTag

Archive