This paper considers the problem of joint channel allocation and power control in the uplink scenario of device-to-device communications underlaying cellular networks. The problem is formulated as a coalition formation game, then a merge-split algorithm for that game is investigated. We analyze the conditions for which an merge-split algorithm will always converge to the Dc-stable coalitional structure and optimal solution. Since those conditions are shown not to hold in most practical scenarios of users' position distribution, that idealized algorithm is not applicable. We propose a merge-split based algorithm that can be applied to practical systems. The analytical results are confirmed by the numerical simulations through multiple users' position scenarios. The simulation results also show that our approach achieves high individual and system performance compared with others.