If Charles were to die while Queen Elizabeth is still alive, would that not make Andrew the next in line?
No.
William follows Charles.
I’m not an expert, but I don’t think that the death of Charles would make any difference. The rule seems to be that the eldest branch of descendents take precedence. So the grandson via the eldest brother, comes before a second brother. And I think they changed the law so that male or female line of descent doesn’t matter anymore.