0
#include<stdio.h>
/**
 * main - prints all arguments without using ac
 * @ac: number of arguments in av
 * @av: array of strings (arguments)
*/

int main(int ac, char **av)
{
    int i;
    for(i = 0; i < ac; i++)
    {
        (void) ac;
        printf("%s\n", av[i]);
    }
    return (0);
}
mch
  • 9,424
  • 2
  • 28
  • 42
Dr.Nati M
  • 7
  • 3
  • What's wrong with `for(i = 0; i < ac; i++)`? Is it a home work to do it without `ac`? What do you know about the `av` array? – mch May 04 '22 at 09:21
  • How do you expect to know how many entries are in what's supposed to be an array pointed to by `av` ? – wohlstad May 04 '22 at 09:21
  • yhaa without ac – Dr.Nati M May 04 '22 at 09:22
  • yhaa that's exactly what's bugging me, anyways thanks @mch – Dr.Nati M May 04 '22 at 09:25
  • 1
    https://www.geeksforgeeks.org/command-line-arguments-in-c-cpp/ You can read this, there is a section "Properties of command line arguments". The point 4 should be very helpful for your task. – mch May 04 '22 at 09:28

2 Answers2

1

The standard says in chapter 5.1.2.2.1 paragraph 2 among other statements:

argv[argc] shall be a null pointer.

So you can loop through the array of pointers until you find a null pointer.

the busybee
  • 10,755
  • 3
  • 13
  • 30
0

You can print all arguments passed to the command line using both ac and av. But your question is how to print arguments only by using av. Well, av is a NULL terminated array of string. So, you can loop until av[i] != NULL.

#include <stdio.h>

int main(__attribute__((unused))int ac, char **av)
{
    int i;
    
    for (i = 1; av[i] != NULL; i++)
    {
        printf("%s\n", av[i]);
    }
    return (0);
}
Neo Luka
  • 1
  • 1