-1

char * strがあり、そのサイズがまだわからないので、宣言することしかできないとしましょう。次に、それを関数に渡します。この関数はそのサイズを知っているので、初期化して設定します。これどうやってするの?

char * str;
func(&str);

void func(char ** str) {
    // initialize str...
}
4

1 に答える 1

2
#define SIZE 10  //or some other value  

また

const int SIZE = 10;   //or some other value  

それで:

void init( char** ptr) // pass a pointer to your char*
{
    *ptr= malloc( SIZE ); //of any size
}

int main()
{
    char *str;
    init( &str ); //address of pointer str
    //...Processing

    free(str);
    return 0;
}
于 2013-10-21T18:56:10.693 に答える