Thread: Question on character arrays

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    19

    Question on character arrays

    Is it possible to do the following?

    Code:
    char s[] = " hello my name is john";
    And then to print, not the entire sentence, but only "hello" and then on another line to print "name" and etc.

    I'm not sure if this is possible since a char is an array and it stores each letter and the null character seperately. It doesn't recognize whole words.

    Can this be done with sscanf maybe?

  2. #2
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    I'd make a for loop and print out a '\n' each time a space ' ' is encountered.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    19
    Nope you don't quite understand what I mean. Let me rephrase it.

    I need a character array to hold about 10 words, and to have the ability to pick any of those 10 words and print them out seperately. For example if I want only 1 word out of the 10, I print only it.

    I don't think an array can do that right? Actually that sounds more like a struct to me, but I need an array to do that. Heh.

    I need the character array to be like a list of seperate words where I can select whichever word I want.
    Last edited by Countfog; 04-29-2008 at 10:56 PM.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,499
    Well you could have an array of string literals or something:
    Code:
    char *words[] = { "hello", "my", "name", "is", "john" };
    You could also tokenize a string and build an array of words using something like a two-dimensional array of chars.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    19
    Quote Originally Posted by citizen View Post
    Well you could have an array of string literals or something:
    Code:
    char *words[] = { "hello", "my", "name", "is", "john" };
    You could also tokenize a string and build an array of words using something like a two-dimensional array of chars.

    Cool I didn't know you could do that. Thats what I mean. What would I use to select each seperate word? Can you give me a sample printf statement with

    Code:
    char *words[] = { "hello", "my", "name", "is", "john" };

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,499
    Well you would use it like an array. Because the element type is a char* you can print it with the %s modifier for printf.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    19
    This just prints hello though. What If I want to print "name"? Doesn't the pointer only point to the first element of the array?

    Code:
    #include <stdio.h>
    
    int main( void )
    {
    	char *words[] = { "hello", "my", "name", "is", "john" };
    	
    
    
    	printf("&#37;s", *words);

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,499
    An array isn't just a pointer to the first element. The compiler actually sets aside the space for all the elements in a static array like the one we are using here. Arrays merely become pointers to the first element in pointer contexts, like when you try to pass an array to a function or access an element. The rest of the time, it is in an array context.

    So hopefully that explains why *words prints the first element, because you're using an array in a pointer context. As for printing individual elements:
    Quote Originally Posted by citizen View Post
    Well you would use it like an array. Because the element type is a char* you can print it with the &#37;s modifier for printf.
    So if you wanted to print the first word, words[0], the second word is words[1], and so on until you reach the array end, words[N - 1].

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    19
    Yep as you were posting this, I found a nice example of this in my book. Thanks.
    Last edited by Countfog; 04-29-2008 at 11:30 PM.

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    it should be
    Code:
    const char* words[] = ...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Just a quick question about character arrays
    By Welshy in forum C Programming
    Replies: 3
    Last Post: 04-03-2006, 07:20 AM
  2. Replies: 11
    Last Post: 11-26-2004, 08:13 PM
  3. Comparing Character Arrays
    By LostNotFound in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2003, 12:42 PM
  4. strings or character arrays
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 07-21-2002, 10:55 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM