get single character from string

This is a discussion on get single character from string within the C Programming forums, part of the General Programming Boards category; this has been giving me fits... i have been searching on c board all morning and found lots of things ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    7

    get single character from string

    this has been giving me fits... i have been searching on c board all morning and found lots of things to try, but cant get this fixed...

    Code:
        char text_send[] = ">>>This is line 1<<<";
        char text_single[] = text_send[1];
    All i want to do is be able to get an individual character from the text_send array.
    This is the error i get:

    test635.c: In function ‘int main(int, char**)’:
    test635.c:66: error: initializer fails to determine size of ‘text_single’
    test635.c:66: error: invalid initializer

    To try to get around this I tried:

    Code:
        char text_send[] = ">>>This is line 1<<<";
        char text_single[1] = text_send[1];
    Then I just get this error:
    test635.c: In function ‘int main(int, char**)’:
    test635.c:68: error: invalid initializer


    I know that text_send is getting filled because I cant print that string out. But I cant seem to get a single character from it.

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    When you append [] on the end of an identifier in a declaration, you are saying "I want an array". Why would you need an array when you just want a single character?
    Code:
    char text_send[] = ">>>This is line 1<<<";
    char text_single;  /* no [], not an array */
    text_single = test_send[1];

  3. #3
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    I bet you started with a different programming language, PHP or something, where functions that return a 'character' really return a string that is one character wide.

    Note that in C, a 'character' is not a string; it is just an integer with a small range that uses a small amount of memory.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    7
    Thanks!
    Rashakil, that helps clear up my question
    thanks cwr

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    9
    Hrm, for some reason that doesn't work here;

    Code:
    #include <stdio.h>
    
    int main()
    {
    
        char string[] = "randomtext";
        char string_bit;
        
        string_bit = string[1]; /* should be 'a' now, right? */
        
        printf("string = \'%s\'\n", string);
        printf("string_bit = \'%s\'\n", string_bit);
    
    }
    Gives me:
    Code:
    grimzy@deepdish ~/c gcc string_test.c -o string_test
    grimzy@deepdish ~/c ./string_test 
    string = 'randomtext'
    Segmentation fault
    grimzy@deepdish ~/c
    Any ideas?

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You print a string with %s, and a character with %c.
    Code:
    printf("string_bit = \'%s\'\n", string_bit);
    ->
    Code:
    printf("string_bit = \'%c\'\n", string_bit);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You get a segmentation fault because printf() is expecting a pointer to a string, and you pass it 'a', which isn't a valid memory location, evidently.

    Here's a reference for printf()'s format specifiers: http://www.cplusplus.com/ref/cstdio/printf.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    9
    Ah, ofcourse!

    Thanks man

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. Printing single character from a string
    By TJJ in forum C Programming
    Replies: 4
    Last Post: 11-05-2003, 05:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21