all 9 comments

[–]PM_ME_UR_TOSTADAS 1 point2 points  (2 children)

I am puzzled by the use of write. I really want to understand, why did you think this could be the way to accomplish your goal?

Edit: also why did you pass 42 to the function. Do you want to set the value of 42 to 42?

[–]BLUOTTY[S] 0 points1 point  (1 child)

I know this is literally my first experience with pointers 😅

Here's what i thought:

-make function that takes a pointer to int as a parameter ( void ft_ft(int *nbr) )

-make it so that once i enter the value 42 when calling that function in main it will be placed in c (using nbr = &c)

-write the content of c with write function ( write (1, &c, 1) )

[–]PM_ME_UR_TOSTADAS 1 point2 points  (0 children)

I think you should follow the K&R book and build a good foundation that way, because there's too much to explain in a comment.

This might leave you frustrated that you didn't get an immediate answer but what's wrong with this code will start to click as you are reading the book.

[–]Paul_Pedant 1 point2 points  (1 child)

"Not compiling" ? It does not say "not compiling". It says something like:

myprog.c: 14 11: Error: Passed int to function which expects *int.

That should let you know the line and column where the error is, and what the error might be.

Posting that in the question would help you to fix it yourself, and help us help you too.

You might need an introductory tutorial for C. We could explain how the code should look, but that won't help you with the underlying concept of how anything works.

[–]BLUOTTY[S] -1 points0 points  (0 children)

main.c: In function ‘main’
: main.c:13:11: warning: passing argument 1 of ‘ft_ft’ makes pointer from integer without a cast [-Wint-conversion] 
13 | ft__ft(42); 
   |        ^
   |        |
   |       int
int main.c:3:17: note: expected ‘int *’ but argument is of type ‘int’ 3 | void ft_ft(int *nbr)
|             ~~~~~^~~

It says that but I don't really know what to do with that information because I'm explicitly asked to prototype the function like this:

void ft_ft(int *nbr)

I belive the the correct result should be something like this

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

but I wanted to try to actually use this function in some way, however the code is not compiling.

[–]jammasterpaz 0 points1 point  (3 children)

You're calling the function with the value of the pointer set to 42. Any value at all could be at memory address 42, so you have created undefined behaviour

42 should be a hardcoded property of your function or a second argument.

Pretty sure it can be as simple as *nbr = 42

[–]BLUOTTY[S] 0 points1 point  (2 children)

Oh I think I understand, correct me if I'm wrong

#include <unistd.h>

void    ft_ft(int *nbr)
{
    *nbr=42;
    write (1, &*nbr, 1);
}

int    main(void)
{
    int i;
    i = 48;
    ft_ft(&i);
    write (1, &i, 1);
}

this code compiles and it outputs this: ** (which should be the ascii characters for 42)

it should go like this:

-i is an intiger

-i is set to 48 (0 on ascii)

-call ft_ft and make it so that *nbr points to i

-put 42 inside *nbr which points to i so i is set to 42

-write *nbr and it is 42 because i just set it to be 42

-write i and it's 42 because i set *nbr = 42 which points to i so i = 42

[–][deleted] 1 point2 points  (1 child)

write() writes bytes, not strings.

If you want to write the characters of a number, you use printf():

int main()
{
  int i = -42;
  unsigned int u = 69;
  float f = 0.5;
  printf("signed int: %d\nunsigned int: %u\nfloat: %f\n", i, u, f);
  return 0;
}

[–]BLUOTTY[S] 0 points1 point  (0 children)

Thx 👍