开发者

defining and iterating through array of strings in c

开发者 https://www.devze.com 2023-03-22 11:26 出处:网络
How can I define an array of string in c then iterate with a loop through the items in the array? So far I have

How can I define an array of string in c then iterate with a loop through the items in the array?

So far I have

char myStrings[][10] = { "one", "two", "three", "four", "five" };
// do I need to specify the "10" maximum length?
// also does it automatically create a nul开发者_开发百科l 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 *
}


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

char myStrings[][10] = { "one", "two", "three", "four", "five" };

for (size_t i = 0; i < sizeof(myStrings) / sizeof(myStrings[0]); i++)
{
    fooThatReceivesOneString(myStrings[i]);
}

Edit - sizeof()

sizeof() returns the size of a variable. It doesn't matter if the variable is an int, array or 2d array.

For example, see the following program

#include <stdio.h>
int main() {
    char myStrings[][10] = { "one", "two", "three", "four", "five" };
    printf("sizeof(myStrings):    %zu\n", sizeof(myStrings));
    printf("sizeof(myStrings[0]): %zu\n", sizeof(myStrings[0]));
    return 0;
}

Which outputs (on my machine):

sizeof(myStrings):    50
sizeof(myStrings[0]): 10

Since each element in the array has the same size (in our case declared to be 10 bytes) we can divide the sizeof the outer array by the sizeof any of its elements to get the number of constituent elements. The simplest option is to just take the first one!


void loopftn (void)
{
  char *numbers[] = {"One", "Two", "Three", ""}, **n;

  n = numbers;
  while (*n != "") {
    printf ("%s\n",  *n++);
  }
  return;
}
  1. You don't need to have a maximum length.
  2. All strings in C are null-terminated.


In C the statement as shown above **n != "" , is illegal at first sight. It compares a pointer with a string. Even *n != "", would compare the pointer of the string with the "" stack string pointer, not the strings. Should use strcmp or compare first character **n=='\0' or **n==0. Also +*n increments the character int the pointed string, not the pointer to string...

Here is a good implementation:

Code:

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


do I need to specify the "10" maximum length?

Yes, apart from the 1st dimension of the array you need to mention all the subsequent dimensions

lso does it automatically create a null ending character after the string?

Yes

I need to pass each string to a function which accepts const char *

You can pass each string like this:

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

Also, You can choose between const char* and char*; since you have declare this as an array it's modifyable; had it been something like

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

then you must have to pass it as const char* because string literals should always be const char*


ad 1) You need to specify a length

ad 2) Yes, string literals are null-ended.

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


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


Try this..

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


I always do it that way, seems elegant and easy to me.
You just define an array as usual, without fixed index.
All you have to make sure is to add a single 0 at the end.
Then you can make the shortest TEST case possible and iterate through it with for or while.

Here is an example, it displays a list of options and the fitting help element (in that case the help array needs to be at least as long as the options one.

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]);
}


This might 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

精彩评论

暂无评论...
验证码 换一张
取 消