Stability of cosmic structures in scalar–tensor theories of gravity