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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:58:53+00:00 2026-06-01T15:58:53+00:00

this is prob pretty easy but if someone could explain the easiest way to

  • 0

this is prob pretty easy but if someone could explain the easiest way 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 = new data_t[500];
        f1(&d);
    }

    /* code for function f1 to fill in array begins */
    void f1(data_t **d)
    {
        char str[5];
        for (int i=0; i<500; i++)
        {
            (*d)[i].ival = i+1;
            itoa (i+1,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

or… trying it with stringstream i still get problems and i’m nowhere getting the ‘$’ in the front, yet…

#include<iostream>
#include<sstream>
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(sizeof(data_t)*500));  //for legacy c
    d = new data_t[500];
    f1(&d);
}

/* code for function f1 to fill in array begins */
void f1(data_t **d)
{
    stringstream ss;
    char *str;

    for (int i=0; i<500; i++)
    {
        (*d)[i].ival=i+1;
        ss << i;
        str = ss.str();
        (*d)[i].sval= str;
    }
}

char * and string do not work well together… and again.. i’m still not sure how to get the “$” in front of the whole thing… ugh

oh… and if it helps.. this is the code given: and my requirements
The following program contains an array of structures of type data_t. The declaration for the variable ‘d’ of type data_t is given. Write logic into the main program to allocate memory for variable ‘d’ such it contains an array of 500 elements, each of type data_t. Don’t bother with freeing the memory or testing the return value of malloc for NULL. Then, write function ‘f1’ to fill in each of the 500 elements of the array such that the integer field ‘ival’ has the values 1-500 for array indexes 0-499 respectively, and the string field ‘sval’ contains the string “$1” – “$500” for array indexes 0-499 respectively. The call for function ‘f1’ is given at the end of the main program.

typedef struct data_t {
    int ival;
    char *sval;
} data_t;
main()
{
data_t *d; /* declaration for an array of  data_t structures */
    /* allocate memory for a 500 element array of structures begins */
    /* allocate memory for a 500 element array of structures ends */
    f1(&d); /* function call to fill in array */
}
/* code for function f1 to fill in array begins */
f1(data_t **d)
{
}
/* code for function f1 to fill in array ends */
  • 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-01T15:58:55+00:00Added an answer on June 1, 2026 at 3:58 pm
    void f1(data_t **d)
    {
        char str[5];
        for (int i=0; i<500; i++)
        {
            (*d)[i].ival = i+1;
            itoa (i+1,str,10);
            (*d)[i].sval = str;
        }
    }
    

    You’re assigning each sval member to point to the same array (str). That is, (*d)[i].sval will be pointing to the same memory location for all elements. Worse, they’re all pointing to a local array that will become garbage when f1 returns.

    If you want each array element’s sval member to point to its own string, you must explicitly allocate memory yourself (and explicitly free it later).

    void f1(data_t **d)
    {
        for (int i=0; i<500; i++)
        {
            char *str = malloc(5);
            if (str == NULL) {
               abort(); // Or fail gracefully somehow.
            }
    
            (*d)[i].ival = i+1;
            itoa (i+1,str,10);
            (*d)[i].sval = str;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi folks this is prob quiet simple but not sure how to do it.
This is prob more of a geometry question, but I have an array of
I know this has prob been answered, but I've searched for almost an hour
alert(window.open('http://www.google.com')); i tried this on firefox, and i get null, no prob in ie.
Prob something im overlooking but this problem has annoyed me greatly. im trying to
Prob an easy question, but I am new to forming XML in SQL 2005,
This somehow simple task is not so simple. I can get the number of
This should be simple, but the answer is eluding me. If I've got a
this is my code that I write it but I want to use LINQ
This is probably a stupid question, but how much of a security risk is

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.