Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the number of strings in an array of strings in C

char* names[]={"A", "B", "C"}; 

Is there a way to find the number of strings in the array? For example, in this case it should output: 3.

like image 944
user1128265 Avatar asked Mar 01 '12 19:03

user1128265


People also ask

How do you check if a string is in an array of strings in C?

We will use strcmp() to compare the two strings. int strcmp(const char *str1, const char *str2); strcmp() compares string str1 to string str2. The function returns 0 if both the strings are same.

How do you find the number of items in an array?

Just divide the number of allocated bytes by the number of bytes of the array's data type using sizeof() . int numArrElements = sizeof(myArray) / sizeof(int);

Can you make an array of strings in C?

Use 2D Array Notation to Declare Array of Strings in C Note that one extra character space for the terminating null byte should be considered when copying the character string to the array location. Thus, one can declare a two-dimensional char array with brackets notation and utilize it as the array of strings.

How do you find the size of an array in C?

To determine the size of your array in bytes, you can use the sizeof operator: int a[17]; size_t n = sizeof(a); On my computer, ints are 4 bytes long, so n is 68. To determine the number of elements in the array, we can divide the total size of the array by the size of the array element.


2 Answers

In this case you can divide the total size by the size of the first element:

num = sizeof(names) / sizeof(names[0]); 

Careful though, this works with arrays. It won't work with pointers.

like image 145
cnicutar Avatar answered Sep 19 '22 08:09

cnicutar


For an array, which the examples in the bounty are, doing sizeof(names)/sizeof(names[0]) is sufficient to determine the length of the array.

The fact that the strings in the examples are of different length does not matter. names is an array of char *, so the total size of the array in bytes is the number of elements in array times the size of each element (i.e. a char *). Each of those pointers could point to a string of any length, or to NULL. Doesn't matter.

Test program:

#include<stdio.h>  int main(void) {     char* names1[]={"A", "B", "C"}; // Three elements     char* names2[]={"A", "", "C"}; // Three elements     char* names3[]={"", "A", "C", ""}; // Four elements     char* names4[]={"John", "Paul", "George", "Ringo"}; // Four elements     char* names5[]={"", "B", NULL, NULL, "E"}; // Five elements      printf("len 1 = %zu\n",sizeof(names1)/sizeof(names1[0]));     printf("len 2 = %zu\n",sizeof(names2)/sizeof(names2[0]));     printf("len 3 = %zu\n",sizeof(names3)/sizeof(names3[0]));     printf("len 4 = %zu\n",sizeof(names4)/sizeof(names4[0]));     printf("len 5 = %zu\n",sizeof(names5)/sizeof(names5[0])); } 

Output:

len 1 = 3 len 2 = 3 len 3 = 4 len 4 = 4 len 5 = 5 

EDIT:

To clarify, this only works if you've defined an array, i.e. char *names[] or char names[][], and you're in the scope where the array was defined. If it's defined as char **names then you have a pointer which functions as an array and the above technique won't work. Similarly if char *names[] is a function parameter, in which case the array decays to the address of the first element.

like image 30
dbush Avatar answered Sep 22 '22 08:09

dbush