5 Replies - 7719 Views - Last Post: 16 March 2011 - 05:33 AM Rate Topic: -----

#1 <3DIC  Icon User is offline

  • D.I.C Regular


Reputation: 6
  • View blog
  • Posts: 327
  • Joined: 06-October 10

going through each element of a char array in c

Posted 15 March 2011 - 08:58 PM

How can i loop through a char array in c?

will something like this work?

for (i = 0; i < sizeof(input); i++)

Is This A Good Question/Topic? 0
  • +

Replies To: going through each element of a char array in c

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: going through each element of a char array in c

Posted 15 March 2011 - 09:04 PM

strlen() is better. You'll only traverse through actual data.
Was This Post Helpful? 2
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: going through each element of a char array in c

Posted 15 March 2011 - 09:20 PM

Generally sizeof() is not much use with c-string which are "zero-terminated char arrays".

So that means I may allocation 100 bytes in a static array of chars -- but that may only hold the string: "Hello" which is: {'H', 'e', 'l', 'l', 'o', '\0' }

note that the end of a c-string (zero-terminated char array) is always a '\0' char (value 0x00). You can use this knowledge to scroll though the chars in a string:

char str[] = "I had a tiny turtle";
for(int i = 0; str[i] != '\0'; ++i) {
    str[i] = toupper(str[i]);
}


The loop will look for the '\0' char and exit when it is found.

if you need to know the length of a string you can use strlen()
Was This Post Helpful? 2
  • +
  • -

#4 legadema  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 25
  • Joined: 15-December 10

Re: going through each element of a char array in c

Posted 15 March 2011 - 09:21 PM

yup use strlen, unless u want to go throughout the entire array
char str[20]= "hello";

cout << sizeof(str) << endl;
cout << strlen(str) << endl;


this outputs 20 and 4
Was This Post Helpful? 1
  • +
  • -

#5 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: going through each element of a char array in c

Posted 15 March 2011 - 10:00 PM

I hope you don't just memorize "use strlen". Think about what strlen actually does.

Strlen walks through the array, counting until it hits a '\0', and then returns the count. So here are the things to think about:

- If the array does not contain a C string, you have no guarantee that strlen will stop at the array bound (because there's no guarantee of a '\0'). Consequently, you can index out of the array and that's undefined behavior.
- If you are looking to get the size of the array, you can't do that with strlen. That's because it only counts up to the first occurence of '\0'. Unless you happen to have a array that's exactly sized to a contained C string, it's not going to give you the array size.
- And if you really wanted strlen, using it to derive the upper limit in a for loop isn't necessarily sensible. You walk through the array once to get the length, and then you walk through it again. Talk about inefficiency.

So, think about these things.
Was This Post Helpful? 2
  • +
  • -

#6 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: going through each element of a char array in c

Posted 16 March 2011 - 05:33 AM

an even shorter way of iterating char arrays is

while(*input)
        printf("%c", *input++);


This post has been edited by codeprada: 16 March 2011 - 05:33 AM

Was This Post Helpful? -1
  • +
  • -

Page 1 of 1