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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:24:46+00:00 2026-05-16T23:24:46+00:00

My code basically is to list ASCII codepoints of a string that is input,

  • 0

My code basically is to list ASCII codepoints of a string that is input, my following code is simple, here:

#include <iostream>
#include <string.h>

using namespace std;

int main() {
    char str[20];
    int result[20];
    cin >> str;

    for(int i = 0; i != strlen(str); i++) {
        result[i] = (int)i; 
    }

    for(int i = 0; i != 20; i++)
        cout << result[i] << ", "; 
}

when I run it, no matter what the input it outputs a pile of gibberish like undefined memory like so:

0, 1, 2, 3, 4, 5, 1, -1217349408, -1220040795, -1220041307, -1076427112, 134514781, -1218903292, 134519344, -1076427096, 134514004, -1217411568, 134519344, -1076427048, 134514681,

Am I missing something simple in how I append each integer to the array?

Just note this is a simple example, my input will not be larger than 20 characters.

EDIT Typo in my result.. cin>>result was cin>>str

  • 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-16T23:24:47+00:00Added an answer on May 16, 2026 at 11:24 pm

    This loop will iterate a number of times equal to the length of ‘str’. That is, it will iterate once for each character in ‘str’, and stop at the ‘null terminator’ (char value of 0) which is how c strings are ended. In each loop, the value of ‘i’ is the loop number, starting at 0 – and this is the value you assign to that index in the results array.

    for(int i = 0; i != strlen(str); i++) {
        result[i] = (int)i; 
    }
    

    So for example, for a string of length 5, you will assign the values ‘0, 1, 2 ,3, 4’ to the result array at those indexes, respectively. The other values in the result array are not assigned – and so could hold any value (generally, whatever was in that bit of memory before you started using it). If your string is longer than 20 characters, you’re in trouble, because you will start trying to access the array at index 20 and beyond, which is not memory that belongs to your program.

    This loop prints out all the values in the ‘result’ array, from the value at index 0 to the value at index 19:

    for(int i = 0; i != 20; i++)
        cout << result[i] << ", "; 
    

    So it will print the initialised values, and, if the string was less than 20 characters long, the uninitialised values as well.

    At a minimum, to start getting anything like the results you’re after, you want to change

    result[i] = (int)i; 
    

    to

    result[i] = str[i];
    

    but as mentioned by others, and to escape some of the memory access issues I mentioned above, it would be much better if you use an iterator to get the character values.

    for(string::iterator i = str.begin(); i != str.end(); i++)
      // access char here using '*i'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the JQuery DropdownChecklist as available from here: http://code.google.com/p/dropdown-check-list/ Basically I'm using some
I have this small piece of code that basically takes a list and runs
I have a block of html in a string that is basically a list
This code basically translates characters based on position in one string to the character
My code basically needs to start up a simple chat server with a client.
The following code basically works as expected. However, to be paranoid, I was wondering,
I have this horribly stripped delphi code that basically login to server, save cookie
Awhile ago I came across a script that basically fetched a list of countries/states
I'm using the anythingSlider by Chris Coyier. Basically it makes list-items slide in from
It's a pygame question, and the list basically holds the positions that the user

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.