Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6369973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:52:19+00:00 2026-05-25T00:52:19+00:00

I was just experimenting with the use of pointers when dealing with arrays and

  • 0

I was just experimenting with the use of pointers when dealing with arrays and I’ve become a bit confused with how C++ is handling the arrays. Here are the relevant bits of code I wrote:

//declare a string (as a pointer)
char* szString = "Randy";               

cout << "Display string using a pointer: ";
char* pszString = szString;
while (*pszString)
cout << *pszString++;

First off, when I tried using cout to write what was in “pszString” (without de-referencing)I was a bit surprised to see it gave me the string. I just assumed it was because I gave the pointer a string and not a variable.

What really caught my attention though is that when I removed the asterisk from the line cout << *pszString++; it printed “Randyandyndydyy”. I’m not sure why it’s writes the array AND then writes it again with 1 letter less. My reasoning is that after writing the char string the increment operator immediately brings the index to the next letter before it can reach the null terminator. I don’t see why the null terminator wouldn’t cause the loop to return false after the string is output for the first time otherwise. Is this the right reasoning? Could someone explain if I’m getting this relationship between arrays and pointers?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:52:19+00:00Added an answer on May 25, 2026 at 12:52 am

    cout has an operator<< overload for char* to print the entire string (that is, print each character until it encounters a 0). By contrast, the char overload for cout‘s operator<< prints just that one character. That’s essentially the difference here. If you need more explanation, read on.

    When you dereference the pointer after incrementing it, you’re sending cout a char, not and char*, so it prints one character.

    So cout << *pszString++; is like doing

    cout << *pszString;
    pszString = pszString + 1;
    

    When you don’t dereference the pointer, you’re sending it a char* so cout prints the entire string, and you’re moving the start of the string up by one character in each iteration through the loop.

    So cout << pszString++; is like doing

    cout << pszString;
    pszString = pszString + 1;
    

    Illustration with a little loop unrolling:

    For cout << *pszString++;

    Randy\0
    ^ pszString points here
    
    // this means increment pszString and send cout the character at which pszString *used* to be pointing
    cout << *pszString++;
    
    // so cout prints R and pszString now points
    Randy\0
     ^ here
    
    // this means increment pszString and send cout the character at which pszString *used* to be pointing
    cout << *pszString++;
    
    // so cout prints a and pszString now points
    Randy\0
      ^ here
    
    // and so on
    

    For cout << pszString++;

    Randy\0
    ^ pszString points here
    
    // this means increment pszString and pass the old pointer to cout's operator<<
    cout << pszString++;
    
    // so cout prints Randy, and now pszString points
    Randy\0
     ^ here
    
    cout << pszString++;
    
    // cout prints andy, and now pszString points
    Randy\0
      ^ here
    
    // and so on
    

    I am glad you are experimenting with pointers this way, it’ll make you actually know what’s going on unlike many programmers who will do anything to get away from having to deal with pointers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im experimenting with some JQuery and timing. I use the code below just for
I'm just experimenting with boost::pool to see if its a faster allocator for stuff
I just started experimenting with Aptana Jaxer server side javascript engine for my next
I've just started experimenting with SDL in C++, and I thought checking for memory
I've just been experimenting with the boost::function_types library recently, and I've come across a
I've just started experimenting with WPF templates vs. styles and I'm not sure what
Hi I've just started experimenting with .live and .die and having some great results
I've just been experimenting with a CSS tooltip that fades in with CSS3's transitions.
I've been searching and experimenting for nearly four hours now, so I'm gonna just
Just looking for the first step basic solution here that keeps the honest people

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.