After a long time spent on making this code work, can someone explain to me why I need 2 stars when I pass a pointer to a string as an argument to the function? A pointer, by definition, keeps the address to a memory where a certain variable will be placed. So it is a variable that has got its own address and under this address is address to another variable. Okay. So if I pass a pointer to a function I use ampersand because I must pass the pointer address to the function. Fine. But then what happens. The function receives the information where in the memory is located this pointer. Okay. This is what I understand. What I do not understand is why I need two stars when I define the function and its argument. I am passing a pointer to a char variable. Why not void wpisuj(char * w). Why wpisuj(char** w). Memory allocation is understandeable to me - I reserved memory with malloc and malloc returns address of this memory, so I place this address as the value of the variable w. And then again something I do not understand, if *w is the pointer and keeps the address of the newly created place in the memory, why I use *w to place there a string. Should it not be *(*w)? Since *w is the address of the reserved memory, then *(*w) is the contents of this memory.
Summing up. What I do not understand is: 1) why wpisuj (char **w) instead of wpisuj (char *w) 2) why strcpy(w, bufor) instead of strcpy((*w), bufor)
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
# define SIZE 256
void wpisuj(char** pw){
char bufor[256];
scanf("%s", bufor);
int l;
l=strlen(bufor)+1;
*pw=(char*)malloc(l*sizeof(char));
strcpy(*pw, bufor);
}
int main(){
char* w;
wpisuj(&w);
printf("%s", w);
return 0;
}
And if I may also ask about freeing the memory. Am I correct in thinking that this is the corect amount of stars (as in the code below):
void free_memory(char **w){
free(*w);
}
but, if I freed memory in main() I would have:
int main(){
w=malloc(sizeof(buffer)+sizeof(char));
/* some code before */
free(w);
}
*= pointer.**= pointer to pointer. that's all - Marc Bwis a pointer to main'sw. Perhaps call itpw. Then*pwin the function means the same variable aswin main. - M.M