I have a segmentation fault in the code below, but after I changed it to pointer to pointer, it is fine. Could anybody give me any reason?
void memory(int *
In C, you have only pass-by-value. So your function memory() gets it own local-only copy of p. malloc inside memory() assigns only to the copy of p that is local to the function. When memory() returns to main(), the copy of p from main is unchanged. In C++, you solve this by using pass-by-reference, like this:
void memory(int*& p, int size)
In C, you use double pointers to achieve similar results.