First, what happens with element 0: after you enter the number of elements which is read by scanf("%d")
, you press Enter so that your input reaches the program. But the scanf
calls only reads the number, not the newline character. So the first call to fgets
gets what remains of the line — an empty line.
See Get scanf to quit when it reads a newline? for several approaches to solve this problem. The most robust way is to call fgets
to read a line (which is expected to call the number), then call sscanf
to parse the number in the string in memory.
The second problem is in the call fgets(temp+i,150,stdin)
. A good compiler would produce a warning like this:
a.c:17:9: warning: passing argument 1 of ‘fgets’ from incompatible pointer type [enabled by default]
You're passing a pointer to a pointer to char
(the address of a location containing the address of a string buffer), but fgets
expects a pointer to char
(the address of a string buffer). Since temp+i
is a pointer to the entry in the array A
(which is an array of pointers), what you need to pass to fgets
is the value that this pointer points to: *(temp+i)
, which is more commonly written temp[i]
. Given what you do with the variable temp
, just drop it and use A
.
fgets(A[i], 150, stdin);
After that, your sort routing doesn't always return the correct result, but that's another story.