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 8937423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:24:47+00:00 2026-06-15T10:24:47+00:00

I have defined 2 variables, one a pointer and one an array char* ptr;

  • 0

I have defined 2 variables, one a pointer and one an array

char* ptr;
char* array;

ptr = "12345";
array = new int[5];

for(int i = 0; i < 5; i++)
    array[i] = i;

while(*ptr != 0)
        cout << *ptr++ << endl;

//Get garbage values
for(int i = 0; i < 5; i++)
    cout << ptr[i];

I was wondering what are the major differences between the variables.
And why I get garbage values when I try to print the values in “ptr[]”
the array way, but it’s perfectly fine when iterate through the
vales. I can’t seem to understand how my variable “ptr” can point to 5
characters, since it should only be able to point to one.

  • 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-06-15T10:24:47+00:00Added an answer on June 15, 2026 at 10:24 am

    There are many differences between pointers and arrays, but the reason you are getting garbage, is that by the time you use ptr with an index, it already points to the null terminator of “12345”.

    Everytime you do this:

    *ptr++;
    

    ptr points to the next element it used to point (i.e. 1, 2, 3, …). When the loop ends, it points to the null terminator \0 and then when you try to index it with i, it points to unknown memory.

    I suggest you use a temp pointer to iterate through the elements instead:

    const char* ptr; // declaring ptr constant in this case is a good idea as well
    
    ...
    
    ptr = "12345";
    char *tmp = ptr;
    
    ...
    
    while(*tmp != 0)
        cout << *tmp++ << endl;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a struct where a constant char string is defined and a pointer
There's probably a silly error in my code. I have defined the following variables:
I am trying to access instance variables I have defined in RSpec in the
I have a set of variables S , and a boolean function f defined
If I define these variables: double x0, xn, h; int n; and I have
Possible Duplicate: Copying one structure to another I have a struct defined as struct
I have 2 datasources defined in my dataSource.groovy one is default dataSource { all
I have defined a variable with an own type, say Dim point As DataPoint
I have to extract defined variable and function names from a js code passed
In SAS/IML I have defined a character variable: var1 = usernames; Previously in the

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.