とはいえ、この質問には以前に良い回答がありましたが、自分で追加することは避けられませんでした。Collegue でパスカル プログラミングを学んだので、C 関連のプログラミング言語でこれを行うのに慣れています。
void* AnyFunction(int AnyParameter)
{
void* Result = NULL;
DoSomethingWith(Result);
return Result;
}
これにより、デバッグが容易になり、ポインターに関連する @ysap による言及のようなバグを回避できます。
覚えておくべき重要なことは、単一のポインターを返すという質問の言及です。これは、ポインターを使用して単一のアイテムまたは連続した配列をアドレス指定できるため、これは一般的な警告です!!!
This questionは、ARRAY SYNTAXを使用せずに、ポインターを使用して配列をコンセプトとして使用することを提案しています。
// returns a single pointer to an array:
student_record* answer4(student_record* student, unsigned int n)
{
// empty result variable for this function:
student_record* Result = NULL;
// the result will allocate a conceptual array, even if it is a single pointer:
student_record* Result = malloc(sizeof(student_record)*n);
// a copy of the destination result, will move for each item
student_record* dest = Result;
int i;
for(i = 0; i < n ; i++){
// copy contents, not address:
*dest = *student;
// move to next item of "Result"
dest++;
}
// the data referenced by "Result", was changed using "dest"
return Result;
} // student_record* answer4(...)
ポインタによるアドレス指定のため、ここに添字演算子がないことを確認してください。
Pascal と C フレームの戦争を始めないでください。これは単なる提案です。