What prsgoddess said.
If Charles had been given a normal ducal title, say Duke of Clarence for example.. then if he died before the Queen, William would become the next Duke of Clarence.
But all of Charles' titles are strictly reserved for the
eldest son and heir apparent of the Sovereign. And he has to be both the eldest son and heir.
Neither the Dukedom of Cornwall, the Dukedom of Rothesay or any of their subsidiary titles may be inherited. If the eldest son and heir apparent dies, the titles revert to the crown until such time as there is another eldest son and heir apparent to a reigning sovereign.
If Prince Charles
dies before becoming king, then there will not be another Duke of Cornwall or Rothesay until William succeeds to the throne and has a son and heir of his own - If, however, Charles
does outlive his mother and becomes king, then William as eldest son and heir apparent, will become Duke of Cornwall and Rothesay, et. al.