2015-03-02 45 views
-2

我正在尝试仅使用指针修改数组。使用指针修改数组中的索引

void modify(){ 
int *ptr = &b[2]; 
*ptr = 90; 
} 

//I have my main function 
void main() { 
    int b[15]; 
//fill the array with values using loop..skipping this part 
modify(); 
} 

,它给我的错误是:错误:使用未声明的标识符“B”的

谁能给我一些见解,为什么编译器不能识别数组b?

回答

3

bmain()中声明为局部变量,因此只能由main()访问。若要使b对其他函数可见,请将其作为全局变量声明它在任何函数之外:

int b[3]; 

void modify(){ 
    int *ptr = &b[2]; 
    *ptr = 90; 
} 

int main(void) { //This is one of the standard signatures of main 
    //Fill the array with values using a loop 
    modify(); 
    return 0; //main returns an int 
}