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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:00:46+00:00 2026-06-01T17:00:46+00:00

trying to get ‘sval’ to contain the string $1 – $500 for array indexes

  • 0

trying to get ‘sval’ to contain the string “$1” – “$500” for array indexes 0-499. in the following code, however itoa is giving me strange strings in the code below:

    #include<iostream>
    #include <stdio.h>
    #include <stdlib.h>
    using namespace std;


    typedef struct data_t {
        int ival;
        char *sval;
    } data_t;

    void f1(data_t **d);
    int main()
    {
    data_t *d;

        d=static_cast<data_t*>(malloc(500));  //is this even needed?
        d = new data_t[500];
        f1(&d);
    }

    /* code for function f1 to fill in array begins */
    void f1(data_t **d)
    {
        int i;
        char str[5];
        for (int i=0; i<500; i++)
        {
            (*d)[i].ival=i+1;
            itoa (i,str,10);
            (*d)[i].sval= str;
        }
    }

it also seems itoa has been depreciated, but that was what i got when i googled int to string

  • 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-01T17:00:47+00:00Added an answer on June 1, 2026 at 5:00 pm

    You don’t need ltoa, cout should be just fine. Why do you need to keep the number and its string representation in the array? when you do cout << 10 you get “10” on the output, you don’t need any conversions of your own

    You, on the other hand, do ltoa without allocating any memory for the strings, which is not healthy as you have probably noticed. You use a local variable (the same, for all the 500 array members), which you try to access after you exit the function – a big no-no, its undefined behavior.

    And:

        d=static_cast<data_t*>(malloc(500));  //is this even needed?
        d = new data_t[500];
    

    No. Not only not needed – shouldn’t be there at all! When in C++ – use new and delete, never malloc, that’s a C function.

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

Sidebar

Related Questions

I'm trying get values from a GridView using the following code: foreach (GridViewRow row
I am trying get all html links within a string and replace them using
I am trying get all html links within a string and replace them using
Trying to get comfortable with jQuery and I have encountered some sample code that
Trying to get a value out of 2d array inside of a hash and
When I'm trying get method from remote webservice it gives me error. My code
I'm trying get a subset of keys for each hash in an array. The
I am trying get my head around how you can run the following php:
I'm trying get some licensing code from AndroidPit.com working, but I get Unable to
I am trying get the following to valid to dd-MMM-yyyy. The calendar extender passes

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.