-1
整数と文字列の2つのフィールドを持つ構造体のプログラムを記述する必要があります。次に、この構造体を動的に割り当てる関数を書く必要があります。intとstringをパラメータとして渡して、割り当てられた構造体に渡します。この関数は、新しく作られた構造体へのポインタも返します。このプログラムの第2の要素は、struct pointerをパラメータとしてとり、画面上のすべてのfiledsを出力し、次にstructのメモリを解放する関数でなければなりません。これは私が思いつくことができる最高です。構造体の動的メモリ割り当て
#include <stdio.h>
#include <stdlib.h>
struct str{
int num;
char text[20];
};
struct str* return_address(int *num, char *text){
struct str* new_struct=malloc(sizeof(struct str));
new_struct->num=num;
new_struct->text[20]=text;
return new_struct;
};
void release(struct str* s_pointer){
printf("%d %s", s_pointer->num, s_pointer->text);
free(s_pointer);
};
int main()
{
struct str* variable=return_address(1234, "sample text");
release(variable);
return 0;
}
質問は何ですか? – sergej
私はこのプログラムを動作させることができませんでした。コンパイルされてもクラッシュします。 –
私は、 'new_struct-> text [20] = text;'はあなたが望むものではないと思います。 –