We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Indexing Char *

cfans
cfans asked
on
Medium Priority
277 Views
Last Modified: 2010-04-15
Say i've got:
char * word = "hello";

how do I index through hello.

I wanted to use a for loop going through the word one character at a time.
I thought that word[0] would give me "o" but it gives the whole thing "hello".
So i'm not sure how to index through.
Comment
Watch Question

Author

Commented:
The reason I'm indexing in is to then change a letter within it.
CERTIFIED EXPERT
Commented:
char *word = "hello";
char *pIter = word;
char letter;

while( pIter != NULL )
{
    letter = *pIter;
    printf( "%c", letter );
    ++pIter;
}

here are the items of the array
word[0] = 'h';
word[1] = 'e';
word[2] = 'l';
word[3] = 'l';
word[4] = 'o';
word[5] = '\0'; // << terminating zero-byte


hope it helps :)
ike

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Ike, thanks.. I can't seem to get change an entry in it.. have any idea how to change.. like 'h' to 's'
to ultimately make the work then "sello"
CERTIFIED EXPERT

Commented:
tdeclare your strings this way:

char word[] = "hello";

word[1] = 'x';

but this is a static-sized string, don't put more than 5 letters in it!!
otherwise use dynamically allocated arrays ...

ike
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.