You have the following line after you have captured all the information
free(p);
So you have just thrown away all the information that you carefully captured.
You are also missing an open brace after the second
for
statement. It should be
for(i = 0; i < n; i++)
{