这确实非常容易,但如果有人能够解释“sval”带“1美元”——0-499阵列指数“500美元”的最容易的方法。 然而,在以下法典中,斜体为:
#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
或......用透镜进行审判时,仍然有问题,一把钱放在前线......
#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;
}
}
果园* 和扼杀并不很好地共同奏效。 i m 仍然无法确定如何在整件事情之前获得“美元”。
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 */