1

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.

5
  • 2
    You can pass the array directly: void makeGraph(char graph[][60]) { note that you don't need to specify the first dimension, call it using makeGraph(graph) Commented Nov 5, 2016 at 6:30
  • Are dimensions fixed at compile time (so you can for example use #define for them)? Commented Nov 5, 2016 at 6:32
  • Yeah they're fixed. I do a #define and then some computation with them. Commented Nov 5, 2016 at 6:38
  • @keineLust why do I need to specify the size? And why don't I need to specify the size of the first array? Commented Nov 5, 2016 at 6:38
  • void makeGraph(int rows, int cols, char graph[rows][cols]), makeGraph(40, 60, graph) Commented Nov 5, 2016 at 7:11

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

Comments

2

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]);    

4 Comments

Ok got it. Why is it that C knows the first array?
Sorry, I didn't get you.
Oh sorry! Why is it that you don't have to specify the size of the first array?
@namarino; In case of 2D array, compiler just need size of second dimension for pointer arithmetic. First dimension can be ignored.
2
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) 

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.