Reputation: 3
I'm a beginner and try to learn some c. I make a program to learn the pointer on functions in with I give 3 arguments to the program at launch: an int, an operator and another int. Everything works fine with %, -, +, and /. But for * I have a very weird bug. After a lot of write() everywhere to find where it starts to bug, I find that it is at the very beginning: when I display argv[2][0], it display the good char for everything exept for the * where it shows "Makefile"... I really don't understand this issue and I don't find anything related to * and arguments bugs. I don't even bug on the pointers or other thing since it's from the start.
Here is the start of the code so you can see:
#include"header.h"
void ft_putchar(char c)
{
write(1, &c, 1);
}
void ft_putstr(char *str)
{
int i;
i = 0;
while (str[i] != '\0')
{
ft_putchar(str[i]);
i++;
}
}
int main(int argc, char **argv)
{
int a;
int b;
char operator;
ft_putchar(argv[2]); //here it shows "Makefile"...
And some output: ( first the char representation, then the number of chars, then another time the char representation but further away in the code and then the result, * is always 0 because it just quit the main after not following any conditions next)
➜ exercice05 ./ft_op 100 / 5
/1/20
➜ exercice05 ./ft_op 100 + 5
+1+105
➜ exercice05 ./ft_op 100 - 5
-1-95
➜ exercice05 ./ft_op 100 % 5
%1%0
➜ exercice05 ./ft_op 100 * 5
Makefile80
The bug is so strange I can only imagine it's something obvious but I'm clueless right now to find it. I do have a Makefile in my dir but I really don't see where would be the problem. I'm on macOS Catalina, tried on bash and zsh, same result. Just in case here is the makefile :
TARGET = ft_op
SRC = $(wildcard *.c)
OBJ = $(SRC:.c =.o)
FLAG = -Wall -Wextra -Werror
CC = gcc
$(TARGET): $(OBJ)
$(CC) $(FLAG) $^ -o $@
.PHONY: clean fclean
clean:
rm -f *.o
fclean: clean
rm -f $(TARGET)
Thank you
Upvotes: 0
Views: 59
Reputation: 48672
Your C program isn't getting *
as an argument, because your shell is expanding it first. You need to use it as '*'
or \*
when you call your program: exercice05 ./ft_op 100 '*' 5
or exercice05 ./ft_op 100 \* 5
Compare the outputs of echo *
, echo '*'
and echo \*
to make it more clear.
Upvotes: 1