Thread: about decleration?

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    38

    about decleration?

    hello

    please can someone tell me

    is it ok to write

    char *s = "hello";

    or to write

    char *s;
    s = "hello";

    i want to know what is the difference

    what about

    int *i = 7; is it acceptable? and what will it do?

    Thanks
    please set free our POWs

  2. #2
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    char* s = "hello"; // is acceptable
    char* s;
    s = "hello world"; // Not allowed.

    int* i = 7; // assigns i to point to address 7; not acceptable or clear code
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    38
    hi

    thank you for your response

    you said that

    char* s;
    s = "hello world"; // Not allowed

    i've compiled it and there were no errors

    so why it's not allowed? what will it do?
    please set free our POWs

  4. #4
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    It is not a part of the standard. It is assigning a const array of chars to a pointer to char variable.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    It actually *is* legal, and it works quite nicely, but however, there should be a change to BOTH pieces of code:

    const char *s = "hello";

    const char *s;
    s = "hello";

    The second is fully legal; the type of a string literal is "const char *" so it is fully legal. Without the const, it still works, but this is a bad thing to use -- this is the only instance in C++ of an automatic cast from a const to a non-const, and, like ANY cast from const, it can easily be abused.

    In fact, the cast is listed as deprecated, so new code should never use it; it may be phased out eventually.

    This, however, is NOT legal:

    char arr[6];
    arr = "hello";
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    135

    Re: about decleration?

    Originally posted by kuwait
    hello

    please can someone tell me

    is it ok to write

    char *s = "hello";

    or to write

    char *s;
    s = "hello";

    i want to know what is the difference
    Technically, the former is an example of initialisation whereas the latter is an example of assignment. Other than that, there is no significant difference in the example as posted.

    If you know, when you declare the pointer, that you wish to initialise it with a value straight away then it makes sense to use the former (initialisation) rather then to declare it and follow with an assignment straight afterwards, but it's no big deal.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problems with variable in class decleration prog.
    By ronin_spirit in forum C++ Programming
    Replies: 5
    Last Post: 12-15-2008, 09:55 PM
  2. multiple types in one decleration
    By hebele in forum C++ Programming
    Replies: 1
    Last Post: 10-17-2004, 07:23 AM