int (*ptr[3])()
は整数を返す関数へのポインタ3の配列です。
もう少し明確に、それは関数ポインタの配列であることを意味します。配列には3つのスペースがあり、関数はintを返すと予想されます。
のコード例
といくつかの他の問題のコードを持ついくつかの問題は、私が行って、それを片付けありました。
配列内の範囲外の割り当ては、の未定義の動作です。コードのクリーンアップ
test.c:30:3: warning: array index 3 is past the end of the array (which contains 3
elements) [-Warray-bounds]
ptr[3] = ccc;
^ ~
test.c:23:3: note: array 'ptr' declared here
int (*ptr[3])();
:
#include <stdio.h>
/* Missing function prototypes
* For C a function taking no arguments should have void for its argument
* For C++ void can be skipped
*/
int aaa(void);
int bbb(void);
int ccc(void);
/* The function should be declared with type specifier, although it is assumed
* by default to return int.
*/
int aaa(void) {
/* Unless this print statement is going to be part of more output, you should
* return a newline
*/
printf("hi\n");
/* As this function is meant to return an int, we will return 0*/
return 0;
}
int bbb(void) {
printf("hello\n");
return 0;
}
int ccc(void) {
printf("ccc\n");
return 0;
}
int main(void) {
int (*ptr[3])();
ptr[0] = aaa;
ptr[1] = bbb;
/* Now assigning to valid ptr[2] and not out of bounds ptr[3] */
ptr[2] = ccc;
ptr[2]();
}
注'それが最も顕著なことは*未定義の動作の*(そのインデックスをmissuses、物事のかなり多くに依存します。それが巻き出された場合、それははるかに簡単になります範囲外の配列)。 –
したがって、 "out of bound"の位置が他のプロセスによって書き込まれる場合にも、エラーが発生する可能性があります。 – lifeisshubh
@Someprogrammerdude: "* by by" bye "' * "私はOPのコードが" bye "という印をつけている確率は非常に、非常に、非常に低く、0とは言いません。 – alk