Also, gcc -pendantic advises:
test2.c:10: warning: ISO C90 forbids mixed declarations and code
which means you should swap line 9 and 10 around. It is much nicer to put all your declarations first, since then you know exactly where they are when your code gets longer. I believe the compiler prefers this too, someone in the know here may explain.
You're right, I tried running it like I posted it and Visual Studio didn't like it very much, but swapping those lines works.

Quote Originally Posted by cas View Post
Code:
p=(int**)malloc(n*sizeof(int));
This is the only "real" problem I see with the code. You're allocating pointers here, not ints, so you should multiply by sizeof(int *); or, perhaps somewhat nicer:
Code:
p = malloc(n * sizeof *p);
Yes, that makes sense, now that I think about it. I felt there was something strange about that, just couldn't put my finger on it.

All your other suggestions are helpful too. It's funny I didn't spot that I could do away with k,l and temp1 as well. Hopefully this will satisfy my prof., though even if it doesn't I think I've at least learned some handy things here. I guess these are rather basic for a more experienced programmer, but I'm only a newbie yet.