Portuguese is one of the very rare languages that use the personal infinitive, an infinitive inflected, besides the impersonal infinitive.
The rules are quite complicated and take several pages in native grammars. Fortunately, many times to use one or another is optional.
As a first approach one should know when it is compulsory to inflect the infinitive.
1) The subject changes [and is not after a causative verb ("fazer, mandar, deixar') or a sensitive verb ("ver, ouvir, sentir")]:
"Ele deu-me uma maçã para comeres." (ele ->tu)
He gave me an apple for you to eat.
2) When the subject is expressed by a subject pronoun or noun [and is not after a causative verb ("fazer, mandar, deixar') or a sensitive verb ("ver, ouvir, sentir")]:
"Comprámos maçãs para nós comermos e não eles." (nós)
We bought apples for us to eat and not them.
If the subject is the same and if you are a beginner you should not try to inflect infinitive because the rules are quite complicated as I said above. For example, in this case both sentencences are correct:
"Comprámos maçãs para comermos no jantar."
"Comprámos maças para comer no jantar."
We bought apples to eat for dinner.