问题描述:

I'm trying to write a function that manipulates an array directly.I don't want to return anything, so obviously I'm going to be using pointers in some fashion.

void makeGraph(some parameter) {

//manipulates array

}

int main() {

char graph[40][60];

makeGraph(????)

}

I can't figure out what to pass in as parameters. Any help would be appreciated.

网友答案:

In C array can be passed as a pointer to its first element. The function prototype could be any of these

void makeGraph(char graph[40][60]);
void makeGraph(char graph[][60]);
void makeGraph(char (*graph)[60]);  

To call this function you can pass argument as

makeGraph(graph);  //or
makeGraph(&graph[0]);    
网友答案:

I'm trying to write a function that manipulates an array directly.I don't want to return anything, so obviously I'm going to be using pointers in some fashion.

In C when you pass array automatically the base address of the array is stored by the formal parameter of the called function(here, makeGraph()) so any changes done to the formal parameter also effects the actual parameter of the calling function(in your case main()).

So you can do this way:

void makeGraph(char graph[][60])
{
    //body of the function...
}

int main(void)
{
     //in main you can call it this way:
     char graph[40][60];
     makeGraph(graph)
}

There are even other ways you can pass an array in C. Have a look at this post: Correct way of passing 2 dimensional array into a function

网友答案:

As other suggested. Simple

void makeGraph(char graph[40][60]                 
{  // access array elements as graph[x][y] x and y are any number with in the array size 
 }

 int main(void) 
{ //in main you can call it this way: 
 char graph[40][60];  

 makeGraph(graph) 

}
相关阅读:
Top