Thread: Strings in C?

  1. #1
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438

    Strings in C?

    Where are the Strings in C?

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    17

    They are nowhere!!!!

    Strings in C are just characters arrays!!!

    to declare one it's just for example

    char string[] = "thisisastring";

    and it will create a character array containing "thisisastring" or

    char string[10];

    and it will create an array with 10 positions

    I think you should read a tutorial

  3. #3
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438

    Re: They are nowhere!!!!

    That makes perfect sense. I'm just glancing over C and found it interesting they don't just use String. I guess using a char array does give you better control though. Thanks.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There is no string type in C. Furthermore, while it is true that they're arrays of characters, there is one omitted fact that must hold true for it to be considered a string:

    All strings are null terminated. Without being null terminated, it is not a string, but rather its just an array of characters.

    char array[3] = { 'a', 'b', 'c' };

    This is not a string.

    char array[4] = { 'a', 'b', 'c', '\0' };

    This is a string.

    Strings may be actual arrays, or may be allocated via pointers, such as:

    char *s = "mystring";

    In this case, because we're using the double quotes, the null is automaticly provided.

    char *s;

    s = malloc( 10 );
    strncpy( s, "hello", 5 );
    s[5] = '\0';

    Here we've manually allocated some space, copied in some data to said space, and just for good measure, provided a null.

    I could have just used 'strcpy' and had it automaticly provide the null for me. Or, I could have used 6 for the size instead of 5, and it would have copied the null there also with strncpy.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Smile

    Strings in C are variable length character arrays terminated with the null character.
    Mr. C: Author and Instructor

  6. #6
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Ok, using this code, how can I have the actual string displayed instead of the memory address?

    Code:
    #include <stdio.h>
    
    void showStringValues()
    {
    	char *string = "Some Text";
    
    	printf("%d", string);
    }
    
    int main()
    {
    	showStringValues();
    
    	return 0;
    }

  7. #7
    Registered User unixOZ's Avatar
    Join Date
    Feb 2002
    Posts
    91
    Code:
    char something[] = "hello";
    printf("%s", something);
    /* more code */

  8. #8
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Thanks!

    So what's the difference between using %d and %s?

  9. #9
    Registered User unixOZ's Avatar
    Join Date
    Feb 2002
    Posts
    91
    %s is for characters, while %d is for integers

  10. #10
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM