You do append to digest, but you don't reassign the pointer so that's all fine.
&md5_digest is a pointer to a pointer, if you try that you will get a compiler error unless you also change the prototype to:
Code:
int md5_file (const char *file, char **digest);
and make corresponding changes within the definition. The purpose of that would be if you wanted to do something like this:
Code:
*digest = malloc(some memory);
Notice **digest is dereferenced to yield the original pointer submitted. Why not just submit the pointer itself? Because it is just a value -- a memory address. If you reassign that value, it would be the same thing as passing an int argument:
and then saying x += 10. That does not change the value of whatever variable provided the value for x, because all the function gets passed is that value. If you want to change the original variable's value, you would pass a pointer to it:
This is sometimes (casually, in C) called "pass by reference". The value here is the address of an int variable, so you can change the value at that address and the original variable's value changes also. **digest is the same thing -- it's the address of md5_digest in main. Which is a pointer, so the value at that address is another address (actually it's not a pointer, so you could not make this work with md5_digest -- array names can be used as pointers for certain purposes, so pretend...). You can now assign to that and *md5_digest (if it were a real pointer) would be affected. Here's a simple illustration:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void test (char **p) {
*p = malloc(11);
strcpy(*p,"hello world");
}
int main () {
char *p;
test(&p);
puts(p);
return 0;
}
Pointers are a difficult concept for many/most people to grasp initially, but once you get it it's fairly simple. I tried a while back to write an explanation:
Pointers: Pass by Value, Pass by Reference
but it's unfinished, I kind of decided my approach was wrong and I haven't had time to change it -- but if you have time take a look and lemme know what you think, what is not clear, etc. Looking at it now I'm not sure what I thought was wrong earlier (of course, I understand pointers already...) maybe I should finish that off.