1

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 */
4

3 に答える 3

1

構造体の定義を変更できる場合、最も簡単な方法は、バイトsvalが構造体に含まれるように定義することです。

typedef struct data_t {
    int ival;
    char sval[5];
} data_t;

(それ以外の場合は、jamesdlin の回答で説明されているようにmalloc()andを使用する必要があります。)free()

次に、文字列変換を行って文字を挿入するには$、 を使用できますsnprintf()。このような:

for (int i=0; i<500; i++)
{
    (*d)[i].ival = i+1;
    snprintf((*d)[i].sval, 4, "$%i", i+1);         
}
于 2012-04-10T10:32:21.090 に答える
1
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;
    }
}

sval各メンバーが同じ配列 ( ) を指すように割り当てていますstr。つまり(*d)[i].sval、すべての要素に対して同じメモリ位置を指します。さらに悪いことに、それらはすべてローカル配列を指しており、返されるとガベージになりf1ます。

各配列要素のsvalメンバーが独自の文字列を指すようにする場合は、自分でメモリを明示的に割り当てる (後で明示的に解放する) 必要があります。

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;
    }
}
于 2012-04-10T10:32:30.587 に答える
0
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; // <-- WRONG! str is allocated on the stack!
        // And you use the same char[] for the 500 data_t.sval
    }
}
于 2012-04-10T10:32:56.890 に答える