Using write function to display pointer

I wrote the attached code with the intention to get 42 as the output but I instead get an asterisks (*) I know that 42 is the ACII value of an asterisk. Please look at my code and see something I might have missed. The program is in C using the <unistd.h> library, I have to use the unistd.h and not the stdio.h for this code.

[code]
#include <unistd.h>

void      ft_putchar(char c)
{
      write(1, &c, 1);
}

void    ft_putnbr(int nbr)
{
    write(1, &nbr, 1);
}

void      ft_ft(int *nbr)
{
      *nbr = 42;
}

int      main(void)
{
      int a;
      int *ptr;
      char c;

      a = 7;
      ptr = &a;
      ft_ft(ptr);
    ft_putnbr(a);
      ft_putchar('\n');
}
/code]
Trevor MaselemeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

evilrixSenior Software Engineer (Avast)Commented:
Write just writes a series of bytes to stdout, which is being interpreted as a series of chars by the shell.

"write() writes up to count bytes from the buffer pointed buf to the file referred to by the file descriptor fd."

https://linux.die.net/man/2/write

You'll need to use printf.

http://en.cppreference.com/w/cpp/io/c/fprintf

"Loads the data from the given locations, converts them to character string equivalents and writes the results to a variety of sinks."

#include <unistd.h>
#include <cstdio>

void      ft_putchar(char c)
{
      write(1, &c, 1);
}

void    ft_putnbr(int nbr)
{
    printf("%d", nbr);
}

void      ft_ft(int *nbr)
{
      *nbr = 42;
}

int      main(void)
{
      int a;
      int *ptr;
      char c;

      a = 7;
      ptr = &a;
      ft_ft(ptr);
    ft_putnbr(a);
      ft_putchar('\n');
}

Open in new window


https://ideone.com/lkj2mX

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.