Defining arrays globally, using them inside a function...

Good morning,

I am still very noob in c++, pardon me if there are terms I am misusing below.

I have two problems regarding calling arrays within functions:

1. I am having difficulties to call char arrays (as you will see below in the code)

2. I am trying to flesh out my code so I dont have monster function with 70 arguments. However I can find a to extract and print a value whitin an array without having to call the array as an argument. Is it possible to define an array globally and just print anything from it from a function without having to pass it as argument ?

Here's below the problem code wise:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <curses.h>


void some_function(int argumentA, int argumentB, int argumentD, char *item_array[10]);

//ARRAY PRINT THROUGH FUNCTION
int main ()
{
int argumentA, argumentB, argumentD;

char *item_array[10];
item_array[1]="apple";

some_function(argumentA, argumentB, argumentD, item_array[10]); //<-- 1st problem here, "cannot convert 'char' to 'char**' for argument 4"
getch();
}


//FUNCTIONS CODE
void some_function(int argumentA, int argumentB, int argumentD, char *item_array[10]){
printf(item_array[1]);
}
}


Also, assuming the code above works, I want to reach something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
include <curses.h>

void some_function(int argumentA, int argumentB, int argumentD);

//ARRAY PRINT THROUGH FUNCTION
int main ()
{
int argumentA, argumentB, argumentD;

char *item_array[10];
item_array[1]="apple";

some_function(argumentA, argumentB, argumentD); //<--only 3 arguments here.
getch();
}


//FUNCTIONS CODE
void some_function(int argumentA, int argumentB, int argumentD){
printf(item_array[1]);
}


Thanks in advance.

Wissam
Last edited on
item_array is an array of pointer to char, when you pass item_array[10] to the function, you are trying to pass the (unexisting) 11th element of that array
I don't get it, so I should pass on item_array[9] ?? doesn't work too.

can you rewrite the portion you think is wrong ?

thnx
because array[index] is an element of the array, not the array itself
Well thanks for your input, that allowed me to fix problem 1.

Here's the clean code now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <curses.h>


void some_function(int argumentA, int argumentB, int argumentD, char *item_array[10]);

//ARRAY PRINT THROUGH FUNCTION
int main ()
{
int argumentA, argumentB, argumentD;

char *item_array[10];

initscr();

item_array[1]="apple";

getch();

some_function(argumentA, argumentB, argumentD, item_array); 
getch();
}


//FUNCTIONS CODE
void some_function(int argumentA, int argumentB, int argumentD, char *item_array[10]){
printf(item_array[1]);
getch();
}



Now is there a way to not pass "item_array" as argument in the function and still gather a value to print whithin the function ?

Many thanks again,

Wissam
You need to pass item_array, remove the [10]

And in the second piece of code, move the definition of item_array[10] outside of the functions.
(i.e. write it right above main() or somewhere else as long as it is outside of a function and defined before it is used in a function)


Edit:

This post was not intended for the post above, you posted it while i was writing =P
Last edited on
Thank you skillless, I did not think of defining arrays before the int main().

Both problems are now solved, thank you very very much guys.

Wissam
Topic archived. No new replies allowed.