Home > Mobile >  finding the size of an string array that saves integers C
finding the size of an string array that saves integers C

Time:07-20

I am currently working on a project where I am manipulating command line data, but I can't seem to get it work if I initialize the size of the array before.

char* len2[50]; // will store "1","2","3" from command line args

int size_arr = sizeof(len2) / sizeof(len2[0]);
printf("%d", size_arr);

this will input 50 when I am looking for it to input 3. How would I be able to find the size?

CodePudding user response:

int size_arr = sizeof(len2) / sizeof(len2[0]);

sizeof(len2) asks for the total allocated size of len2 in bytes. This only works because C knows how many you allocated at compile time and turns it into a constant. It doesn't tell you which ones you've filled in. C does not keep track of that; you have to.

Because len2 is an array of pointers, you can mark the end with a null pointer. The term for this is a sentinel value. First, be sure to initialize the array to null.

// This will initialize the entire array to NULL
char* len2[50] = {NULL};

Now you can find how many elements are in the array by looking for the first null, or by hitting the allocated size.

size_t len2_size = sizeof(len2) / sizeof(len2[0]);

int size = 0;
for( ; len2[size] != NULL && size < len2_size; size   );

printf("size: %d\n", size);

This is, incidentally, basically how C strings work. The end of the string is marked with a 0.

Alternatively, you can store the allocated size and number of elements in a struct and keep track, but that's another question.

Finally, if you're just reading command line arguments, use argc and argv. argc is the size of argv. argv[0] is the name of the program, and the rest are the command line arguments.

int main( const int argc, const char **argv) {
    printf("%d arguments in argv\n", argc-1);
}

And argv is also terminated with a NULL pointer so it's easy to iterate through.

// Because argv[0] is the name of the program,
// start at 1 and read until you hit a null pointer.
for( int i = 1; argv[i] != NULL; i   ) {
  printf("argv[%d] = %s\n", i, argv[i]);
}

CodePudding user response:

From the comments:

I guess what I'm trying to find is the number of items. Since there are 3 numbers I'll be getting from the command line, I want to be able to manipulate the array using 3 for a for or while loop for example.

According to the standard you have two versions of main function available (while the implementation defining further ones is legal):

int main();
int main(int, char**);

The second form gets the command line parameters passed to directly while the first form can be used if command line parameters are irrelevant.

Typically (but not necessarily) these two arguments get named argc and argv with argc containing the total number of command line arguments and argv a null-terminated array to the actual arguments. First one of is always the name that has been used to call the programme (which can differ in some cases from the actual executable name, e.g. if symbolic links are involved) which you might want to skip.

So a programme simply iterating over all arguments and printing them back to console might look like this:

int main(int argc, char* argv[])
{
    for(  argv; *argv;   argv)
    {
        printf("%s\n", *argv);
    }
    return 0;
}

with first argv skipping the programme name, *argv profiting from and testing for the null-terminator of the array and second argv being the normal loop post operation.

If you want to see the programme name as well you might just skip very first pointer increment:

for(;*argv;  argv)

Alternative variants might use an integer to count up to argc – just a matter of personal taste...

  •  Tags:  
  • c
  • Related