it looks like the D40 is only compatible with cards up to 32GB. 64GB cards use a file system called exFAT and are not compatible with older cameras.
here is a link to the sandisk product compatibility tool
Compatiblity tool
http://pct1.sandisk.com/NewSearch.aspx
Looks like the D5300 can use up to 512GB cards so using this card in that camera will be no problem.