You want to call forkpty(). From the man page:
#include <pty.h> /* for openpty and forkpty */
pid_t forkpty(int *amaster, char *name, struct termios *termp, struct
winsize *winp);
Link with -lutil.
The forkpty() function combines openpty(), fork(), and login_tty() to
create a new process operating in a pseudo-terminal. The file descrip‐
tor of the master side of the pseudo-terminal is returned in amaster,
and the filename of the slave in name if it is not NULL. The termp and
winp parameters, if not NULL, will determine the terminal attributes
and window size of the slave side of the pseudo-terminal.
Your parent process talks to the child by reading and writing from the file descriptor that forkpty stores in "amaster" - this is called the master pseudo-terminal device. The child just talks to stdin and stdout, which are connected to the slave pseudo-terminal device.