unix programming(memory problem i think)

In my program, the parent create processes and the child compute the sum
The problem is the parent code
so for example if going to computer 1 2 3 4 5
it going to add 1 and 2 first and then 3 and 4 and then 5 and 0(created)
There have two for loops, when it finish the inner for loop once the output is alright, but it goes into twice time the output go weird.
also it works fine with argument less than 5(1 2 3 4)
Parent code

Slave 1 2
total is 3
print index of n is 1
This child PID is 7166 and total of this child sum is 3
MASTER: first arguement = 3, second arguement = 2, third Arg = 3
Slave 3 4
total is 7
print index of n is 2
This child PID is 7167 and total of this child sum is 7
MASTER: first arguement = 3, second arguement = 7, third Arg = 3
Slave 5 0
total is 5
print index of n is 3
This child PID is 7168 and total of this child sum is 5
MASTER: first arguement = 3, second arguement = 7, third Arg = 5 // end of first inner loop
Slave 3 7
total is 10
print index of n is 1
This child PID is 7169 and total of this child sum is 10
MASTER: first arguement = 10, second arguement = , third Arg = 5 (the second one is miss, i have no idea)
Slave 5 0
total is 5
print index of n is 2
This child PID is 7170 and total of this child sum is 5
MASTER: first arguement = 105, second arguement = 5, third Arg = 5 (and later is became 105, it should be 10, but dont know how is it got 105)
Slave 105 5
total is 110
print index of n is 1
This child PID is 7171 and total of this child sum is 110
MASTER: first arguement = 110, second arguement = 0, third Arg = 5

argv is perfectly writeable and argv[argc] = "0"; is not out of bounds (the size of argv is argc+1), that's not the problem here, per se, the problem is that the arrays pointed to by the pointers in argv are not big enough to fit the results of your additions:

When you added 3 and 7, you got 10 from the child, and you executed this line:

sprintf(argv[n+1], "%i", num); // store the sum in the first argument

your n was 0, so you wrote the three bytes '1', '0', and '\0' into the memory pointed to by argv[1]

But your program was called with argv[1] = "1", with only two bytes to spare, so the last null was written who knows where.

It appears that the C runtime that called your main(), set the pointers stored in argv pointing at subsequent locations of the same array, so that '\0' actually overwrote the character in argv[2]. On the next loop, you wrote "5" into argv[2], which overwrote that null with the five. So now you have:

av[1] av[2]
| |
v v
'1' '0' '5' '\0'

That's why when you printed argv[1] as a string, you got 105.

PS: as gcc rightfully complained, your execl() call is wrong: the last argument must be (char*)0, not just 0.

For convenience, I guess: the sentinel is there so that argv can be passed into another execv() (according to the original man page), and the count must be there so that you don't have to loop through argv yourself. After all, in practice, everyone is using argc.