A man visiting a friend in the hospital.
A man and a patient in a hospital room.
Visiting a friend  who is a patient usually happen in the hospital.
If the man is visiting his friend in the hospital this implies that his friend is a 'patient' and that they are in a hospital room.
Sentence 1 explains that the man is visiting a friend in the hospital while the second one explains that the friend the man was visiting is a patient in the hospital.