string question

This is a discussion on string question within the C Programming forums, part of the General Programming Boards category; hi all, how to assign a value to a string variable? for example: char string[5]; string="hello"; is it correct? if ...

  1. #1
    abc
    abc is offline
    Registered User
    Join Date
    Oct 2001
    Posts
    6

    string question

    hi all,

    how to assign a value to a string variable?

    for example:

    char string[5];

    string="hello";

    is it correct? if not, how to do it?

    thank you

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    If you declare and initialise a variable, then you can use either of these
    char string[] = "hello"; // a char array
    char *string = "hello"; // a pointer to a char (in this case, the first of 5)

    But if you want to assign a value later on, then
    with the char array, you must do this
    strcpy( string, "world" );

    with the char pointer, you must do this
    string = "world";
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    abc
    abc is offline
    Registered User
    Join Date
    Oct 2001
    Posts
    6
    Originally posted by Salem
    If you declare and initialise a variable, then you can use either of these
    char string[] = "hello"; // a char array
    char *string = "hello"; // a pointer to a char (in this case, the first of 5)

    But if you want to assign a value later on, then
    with the char array, you must do this
    strcpy( string, "world" );

    with the char pointer, you must do this
    string = "world";
    thank you very much
    but if i want to print the value out
    - printf("%s", &string);

    is it the correct way?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    No

    And do you have a book?

    Because it's starting to look like you are learning by guesswork.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    > - printf("%s", &string);

    This would be:

    printf("%s", string);

    For one thing, the printf function doesn't require you to pass by reference. It is not manipulating the string, just taking its value (hence, pass by value) and insterting it in the const string. Also, you won't use the address of (&) operator for a string because "string" refers to the address of the array. That is why you can't do:

    string = "world"

    Because then you would try to set the address of string to "world". You have to copy it, like Salem said, with strcpy.

    --Garfield
    1978 Silver Anniversary Corvette

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    11

    Question

    char x[]="abcd";

    Can you tell me what's the mechanism behind this statement? I mean why can we declare the string in the variable declaration and we have to use strcpy in the rest of the program??

    char x[5];
    strcpy(x,"this is a string!");


    I noticed that strcpy, doesn't check the maximum length of x and sets
    x equal to "this is a string!"

    strcpy allocates extra space for the extra characters? If that's what happens then x does't occupy space reserved for other variables...So what' s the point in defining the length of a string? Good programming practice only? Portability maybe?

  7. #7
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    > strcpy(x,"this is a string!");

    Okay, x is the address of the first element, or char. When you copy a string, you copy each char into the next mem spot. So, it will continue to copy each const char into x until there is no more.

    --Garfield
    1978 Silver Anniversary Corvette

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    >> strcpy allocates extra space for the extra characters? If that's
    >> what happens then x does't occupy space reserved for other
    >> variables...So what' s the point in defining the length of a
    >> string? Good programming practice only? Portability maybe?

    No. strcpy does _NOT_ allocate space.

    > When you copy a string, you copy each char into the next mem
    > spot. So, it will continue to copy each const char into x until
    > there is no more.

    Clarification: until there is no more of the string left to copy.

    Thus:

    char x[5];
    strcpy( x, "hello there this is a buffer overflow" );

    Will copy that entire string into your array x, thus, it overflows the buffer and trashes your memory.

    Use "strncpy".

    strncpy( x, "hello there this is a buffer overflow", 5 );

    This will only copy 5 characters to this buffer.
    Here is a quote: http://www.rt.com/man/strncpy.3.html
    The strncpy() function is similar, except that not more
    than n bytes of src are copied. Thus, if there is no null
    byte among the first n bytes of src, the result wil not be
    null-terminated.

    In the case where the length of src is less than that of
    n, the remainder of dest will be padded with nulls.
    Quzah.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    11
    Originally posted by Salem
    If you declare and initialise a variable, then you can use either of these
    char string[] = "hello"; // a char array
    char *string = "hello"; // a pointer to a char (in this case, the first of 5)

    But if you want to assign a value later on, then
    with the char array, you must do this
    strcpy( string, "world" );

    with the char pointer, you must do this
    string = "world";
    When we asssign a const string to a char pointer what happens is that we point to the address where for example "world" is kept by the compiler???
    Why don't we do the following:
    char *string ="hello";
    delete string;
    string = "world";

    is it because const strings are kept in a special place not in the heap??

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > what happens is that we point to the address where for example "world" is kept by the compiler?
    Correct

    > is it because const strings are kept in a special place not in the heap?
    Also correct, you can only delete what you used new to create (noting that this is now C++ and not C)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Reusing a string pointer question
    By chiefmonkey in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2009, 04:53 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 05:44 PM

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