English 中文(简体)
页: 1
原标题:int to char* and memory allocation

这确实非常容易,但如果有人能够解释“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 */
最佳回答
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.

如果您希望每个阵列元素sval<>/code> 成员指出自己的座右铭,你必须明确分配记忆(并明文免费)。

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;
    }
}
问题回答

如果你能够改变结构定义,那么界定这一定义最容易的事情就是这样:<代码> val的斜体内包含:

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

(否则,您将需使用malloc()和free(>,详见jamesdlin的答复)。

然后,为了进行扼制转换并插入$ nature,您可使用snprintf()。 与此类似:

for (int i=0; i<500; i++)
{
    (*d)[i].ival = i+1;
    snprintf((*d)[i].sval, 4, "$%i", i+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; // <-- WRONG! str is allocated on the stack!
        // And you use the same char[] for the 500 data_t.sval
    }
}




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签