0

The issue I'm running into is defining the arguments of a function where the data structure is defined in a different file. In the main file I define the arrays nPart[3]={a,b,c}; and particle p[np]; where a,b,c,np are integers. I then define the array:

particle *pp[ nPart[0] ][ nPart[1] ][ nPart[2] ][ np ];

which points to different particles in the p array.

In a second file I want to create a function:

void function(int np, particle *p, particle *pp[ nPart[0] ][ nPart[1] ][ nPart[2] ][ np ]){...}

however since I have defined nPart in the first file I'm unsure how I should do this. The second file's .h file is included in the first document.

I feel like I should somehow pass nPart as an argument to the function, along with some information about the location of pp and then be able to construct pp inside the function however I'm pretty lost as to how I should go about this.

EDIT: My lack of knowledge of terminology and C in general is probably making this more confusing that it should be.

I'll try to make a simple example:

In the first file's main function I define:

int x[3] = {1,2,2};
int *p[ x[0] ][ x[1] ][ x[2] ];

Then in the second file I want to be able to be able to use *p as an argument to functions. However, since x is defined locally in the main function I'm unsure how to do this.

The reason I'm trying to define *p in terms of x is that there are other things which also depend on x and so I want to be able to adjust just x rather than manually changing everything.

EDIT 2: It seems that by switching the order of the arguments in my original code with nPart before *pp I am able to do what I wanted to do.

see relemos' answer here: Passing multidimensional arrays as function arguments in C

I guess I just wasn't searching with the right terminology the first time I looked for answers.

Community
  • 1
  • 1
overfull hbox
  • 747
  • 3
  • 10
  • 3
    `pp` isn't a structure, it's a 4-dimensional array of pointers. – Barmar Sep 18 '15 at 00:39
  • I think you are somewhat confused about function declarations/definitions. You don't need to know the actual definition of `npart` to write `function`. All you need is to define a parameter to `function` that has a same (or compatible) type to `npart`. So for example: `void function (int np, particle *p, particle *pp, int part_array[3]);`. And then when you call `function` you would pass in `npart` as the last argument. – kaylum Sep 18 '15 at 01:12
  • Thanks for bearing with me. As you can tell I'm really a novice when it comes to C.. The issue I'm having is that ```pp``` is a multidimensional array with dimensions determined by the values of ```nPart``` but since ```nPart``` isn't defined in the second file, I don't know how to have ```pp``` as an argument. – overfull hbox Sep 18 '15 at 14:44

0 Answers0