Defining and iterating through an array of strings in c

How can I define an array of a string in c and then repeat the loop using the elements in the array?

I still have

char myStrings[][10] = { "one", "two", "three", "four", "five" }; // do I need to specify the "10" maximum length? // also does it automatically create a null ending character after the string? int i = 0; for( i = 0; i < ; i++) { // I need to pass each string to a function which accepts // const char * } 
+8
c
source share
9 answers

When you declare a char sequence with "" , a null terminator is added.

 char myStrings[][10] = { "one", "two", "three", "four", "five" }; size_t i = 0; for( i = 0; i < sizeof(myStrings) / sizeof(myStrings[0]); i++) { fooThatReceivesOneString(myStrings[i]); } 
+10
source share
 void loopftn (void) { char *numbers[] = {"One", "Two", "Three", ""}, **n; n = numbers; while (*n != "") { printf ("%s\n", *n++); } return; } 
  • You do not need to have a maximum length.
  • All lines in C end with a null character.
+7
source share

Do I need to specify a maximum length of "10"?

Yes, except for the 1st dimension of the array, you need to specify all subsequent sizes

Does lso automatically create a null ending character after a string?

Yes

I need to pass each line to a function that takes const char *

You can pass each line as follows:

 for( i = 0; i < ; i++) { foo(myStrings[i]); } 

Alternatively, you can choose between const char* and char* ; since you declare it as an array that it modifies; if it was something like

 const char *mystrings[] = { " ", " "}; // string literal 

then you need to pass it as const char* , because string literals should always be const char*

+2
source share

ad 1) You need to specify the length

ad 2) Yes, string literals have a null value.

Inside yours, just call the function with the parameter myStrings [i].

+1
source share

Yes, you need to specify the length or add a NULL entry as the last entry in your string array. C does not do this automatically for you.

0
source share

Try it.

 int j =0; while (char myStrings[][j] !='\0'){ i++; } for (i = 0; i < j; i++) { somemethod(myStrings[][i]); } 
0
source share

I always do that, it seems elegant and light to me.
You simply define an array, as usual, without a fixed index.
All you have to do is add one 0 to the end.
You can then make the shortest TEST possible and skip it with or over.

Here's an example, it displays a list of options and an installation help item (in this case, the help array should be at least until it is selected.

 const char *options[]={ "CAL_MAG_MIN", "CAL_MAG_MAX", 0 }; const char *help[]={ "<X,Y,Z>", "<X,Y,Z>", 0 }; int pos; for (pos=0;options[pos];pos++) { printf("\t %s %s\n",options[pos],help[pos]); } 
0
source share

In C, the statement shown above ** n! = "Is illegal at first glance. It compares a pointer to a string. Even * n! =" ", Will compare a string pointer to a string pointer" "rather than strings. Must use strcmp or compare the first character ** n == '\ 0' or ** n == 0. Also, + * n increases the int character of a pointed line, not a pointer to a line ...

Here is a good implementation:

the code:

 static const char* strings[]={"asdf","asdfasdf",0}; const char** ptr = strings; while(*ptr != 0) { printf("%s \n", *ptr); ++ptr; } 
0
source share

This can help

 int main() { int upp; int num; char password[20]; int i; printf("Enter a Password (Must include a special char, a number, and an uppercase letter): \n"); scanf(" %s", password); for (i=0;i<strlen(password);i++){ if (isupper(password[i])){ upp=1; }else if (isdigit(password[i])){ num=1; } }if (num==1 && upp==1){ printf("u may enter"); }else printf("try again"); return 0; } 
0
source share

All Articles