3
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

1
  • Oli, on which line did you find a memory leak? Commented Jun 16, 2012 at 12:00

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Why should there be a memory leak when duplicating the standard string to the path pointer? Seems to be ok to me.
@user1460515: Because you've malloc-ed some memory, and stored its address in a pointer. But you then overwrite that pointer with a different address (with *path = strdup(blah)). So there is no longer any way of accessing the malloc-ed memory, so it can never be freed.
Thanks, You're totaly right! But the only solution i have to solve it is to copy one char at a time to the **path pointer. Or are there other posibilities?
@DJake: You can use strcpy or strncpy (and if you do that, you no longer need a pointer-to-pointer!). But as with all string operations in C, you need to make sure you're not writing past the end of your destination buffer.
1

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).

2 Comments

So you're saying it's obligated to pass the adress regarding the fact that the datatype could be a normal variable or a pointer?
@user1460515 - if you want to make a change to te pointer in enter_path that you want to be able to observe in main() (or any other calling function), you will have to pass the address of the pointer
0

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.

1 Comment

Actualy Kraken, Oli Charlesworth has pointed me to the fact that when i call *path = strdup(standard). The adress to which path point is changed. so in other words path is now lost in space, and now referes to the value to which standard points.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.