Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Using character pointers

talhanasirtalhanasir Posts: 13Member
How come the value of last cout statement is
[blue] My sister's married name is name was [/blue]

char *c = "Bettye Lou Horn";
cout << "My sister's maiden name was" << c << "
";
c = c+11; // Make c point to the last name (the 12th character)
c = "Henderson";
c = c - 11; // Make c point to its original location.
cout << "My sister's married name is" << c << "
";
// when we use *c in cout statement only B prints why

Comments

  • LundinLundin Posts: 3,711Member
    : c = "Henderson";

    [green] You can't do this. You will set the pointer to point at a new location - the static string "Henderson". It will forget where it pointed at before.[/green]

    : c = c - 11; // Make c point to its original location.
    [green]So when you subtract 11 here you are pointing at a non-valid memory location. The program will likely crash here.

    Try something like this instead:[/green]

    [code]
    char str[21] = "Bettye Lou Horn";
    /* Make str an array of chars large enough to hold "Bettye Lou Henderson" = 20 letters + 1 null termination */

    char* lastName = "Henderson";

    cout << "My sister's maiden name was " << str << endl;

    strcpy(str+11, lastName);

    cout << "My sister's married name is " << str << "
    ";

    [/code]
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [b][red]This message was edited by AsmGuru62 at 2006-6-8 4:29:39[/red][/b][hr]
    : [code]
    : char str[21] = "Bettye Lou Horn";
    : /* Make str an array of chars large enough to hold "Bettye Lou
    Henderson" = 20 letters + 1 null termination */
    :
    : [/code]
    :
    [blue]Just one small addition: if you omit the value in brackets the compiler will allocate memory exactly enough to hold a string, so you do not have to count symbols:
    : [code]
    : char str[] = "Bettye Lou Horn";
    : [/code]



  • LundinLundin Posts: 3,711Member
    : [b][red]This message was edited by AsmGuru62 at 2006-6-8 4:29:39[/red][/b][hr]
    : : [code]
    : : char str[21] = "Bettye Lou Horn";
    : : /* Make str an array of chars large enough to hold "Bettye Lou
    : Henderson" = 20 letters + 1 null termination */
    : :
    : : [/code]
    : :
    : [blue]Just one small addition: if you omit the value in brackets the compiler will allocate memory exactly enough to hold a string, so you do not have to count symbols:
    : : [code]
    : : char str[] = "Bettye Lou Horn";
    : : [/code]
    :
    :
    :
    :

    Yep, but in this case it should be initialized to
    "Bettye Lou Horn" but be large enough to be able to hold
    "Bettye Lou Henderson"
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    : : [b][red]This message was edited by AsmGuru62 at 2006-6-8 4:29:39[/red][/b][hr]
    : : : [code]
    : : : char str[21] = "Bettye Lou Horn";
    : : : /* Make str an array of chars large enough to hold "Bettye Lou
    : : Henderson" = 20 letters + 1 null termination */
    : : :
    : : : [/code]
    : : :
    : : [blue]Just one small addition: if you omit the value in brackets the compiler will allocate memory exactly enough to hold a string, so you do not have to count symbols:
    : : : [code]
    : : : char str[] = "Bettye Lou Horn";
    : : : [/code]
    : :
    : :
    : :
    : :
    :
    : Yep, but in this case it should be initialized to
    : "Bettye Lou Horn" but be large enough to be able to hold
    : "Bettye Lou Henderson"
    :
    [blue]I see... I stand corrected.[/blue]
Sign In or Register to comment.