Using the &-operand when passing a pointer to a function?

int enter_path(char** path) {
  char* standard = "./questions.txt";
   printf("\n\t%s\n\t%s",
          "Please enter the file location.", "path: ");
   fgets(*path, MAX_PATH_LENGTH ,stdin);
     *(*path + (strlen(*path)-1) ) = '\0';
   if(**path == '\n' )
     *path = strdup(standard);
   return 0;
}

int main(void){
  char* path = malloc(MAX_PATH_LENGTH);
  enter_path(&path);
  some_other_function_using_the_path_string(path);
}

the code above works fine, the reason why i'm posting this is the following.

Why do i have to pass the adress of the pointer "path" to the function enter_path, so some other function can use/read the modified value. So in short, Why do i have to use the &-operand when calling the "enter_path" function, and can't define enter_path as "int enter_path(char* path);"

I know this is normal variables refering to datatypes like int, long, etc. but i assumed that when working with pointers the change should be visible for every other function. cause the pointer still referes to the same point in the memory, just the value has been changed.

Could it be i'm seeing this all wrong?

PS: I'm trying my best to keep my code ansi-C compliant, which is the reason i'm using fgets instead of readline. Just sayin that cause i've allready got some comments regarding the fact that the readline function is easier/safer to use.

With kind regards, JD

Answers


If you have a variable of type T that you want a function to be able to modify, you need to pass it via a pointer, i.e.:

void foo(T *p) { *p = 42; }

int main(void) {
    T t;
    foo(&t);
    // The value of t is now 42
}

In your code, T is char *; in other words, your function needs to be able modify a pointer, so you need to pass it a pointer to that pointer.


However, this is not good code. If (**path == '\n') is true, then you will leak memory.


The reason is that if you specify the parameter type as char *, you will operate on a copy of the pointer in the function, so your code (modified for this scenario)

path = strdup(standard);

Would not reflect back in the caller after the function ends

By passig the address of the variable, you can change the pointed-to pointer and this change will persist even after the function returns

Think of the pointer as your data (just like with int, etc.) by passing a char * pointer you can modify the pointed-to characters; by passing a char ** pointer, you can modify the pointed-to pointer (with the modification reflecting in the caller function as well).


You are passing a variable path that is itself a pointer. But you are modifying the pointer itself in the function enter_path, hence you will have to pass the pointer to path and hence the address of path is to be passed and hence &path is passed.

In the current scenario, the line

*path = strdup(standard);

it does modify the variable path in the function but it's value will not change outside the function, i.e. in function main.


Need Your Help

Assigning member in optional structure memeber

c++ boost-optional

What is the best style for assigning an optional member of structure?

Tried and tested ways to automate load tests with Maven

java performance maven load-testing

I'm trying to figure out the best way to automate the execution of and result recording of load tests. We currently use Maven; ideally the solution would be executed as part of the Maven project life-