foreach - supported in C coding language?

I'm writing a program in C on the PC. I want to loop as a function of the number of array elements in the same way I'm used to doing in Unix c-shell scripts (an implied counter):

  set NameList = ( Joe Bob Karen Larry )
  foreach Name ( $NameList )
     processing...
  end

-- or --

  foreach Name ( Joe Bob Karen Larry )
     processing...
  end

Can I do this, or do I have to know the size of the array in advance and do it like,

   #define NAME_SIZE 4
   int i;
   for(i = 0; i < NAME_SIZE; i++)
   {
      processing...
   }

If it's not supported, can you suggest the code to implement a C function that would work like foreach?
lhgarrettAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
How do you declare the array?
0
rbrCommented:
Try this
#define NAME_SIZE 4
char *NameList[NAME_SIZE]={"Joe","Bob","Karen","Larry"};


for (i=0;i<NAME_SIZE;i++) {
.... Do what ever you want
.... For accessing the array use NameList[i]

}
0
ahoffmannCommented:
char *NameList[NAME_SIZE]={"Joe","Bob","Karen","Larry",'\0'};
int i=0;
while (NameList[i]!='\0') {
   /* processing...  */
   i++;
}
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

ozoCommented:
char *NameList[]={"Joe","Bob","Karen","Larry"};
for( i=0;i<sizeof(NameList)/sizeof(NameList[0]);i++ )
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lhgarrettAuthor Commented:
To rbr:

Thanks, but the idea I was looking for was a way of not having to count the elements in the array.
0
lhgarrettAuthor Commented:
> while (NameList[i]!='\0') {
This will work, however...

> for( i=0;i<sizeof(NameList)/sizeof(NameList[0]);i++ )
This actually is closer to what I was looking for!

Points to ozo this time, but thanks to both of you!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.