Pass Array from one function to another

I'm trying to pass an array from one function to another without resorting to pointers and I have to use Pass by Reference. I can't seem to figure it out. I've been looking at many examples and just don't get it.

the function createAbc makes an array populated with the Alphabet. I need to pass that into my display function so that i can work with the array there. I don't know how to pass it in there... :(

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
#include "stdafx.h"
#include <iostream>
#include <cctype>
#include <time.h> 
using namespace std; 

void createAbc(char(&)[500]); 
int display(int,int,char[]);
int main(){
	char letterArray[500] = {0};
	// newArray[500]={0};
	createAbc(letterArray); 
	display(0,0,letterArray);

}
void createAbc(char (&letterArray)[500]){

	 letterArray['Z' - 'A' + 1] ; 

for( char ch = 'A'; ch <= 'Z'; ch++ )
{
    letterArray[ch - 'A'] = ch ;

	//cout<<ch; 
}
}
Last edited on
Can't you pass it the same way as you did with the createAbc function?
1
2
3
4
5
6
void createAbc(char[500]); 
void createAbc(char letterArray[500])

//...
createAbc(letterArray); 
//... 

Will work fine and letterArray will be modified.
@MiiNiPaa
He didn't want to use pointers.
Thanks for the replies.

You guys notice the char[] in display function?

I need that char array to be populated with the data in createAbc.

CreateAbc works when called but how do I get that data into the display function?

EDIT I just realized that arrays are already like integer values with the & AKA they are pass by reference intrinsically.

So I've redone the code to look like this but still don't know how to throw it into display function.

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
#include "stdafx.h"
#include <iostream>
#include <cctype>
#include <time.h> 
using namespace std; 

char createAbc(char []); 
int display(int,int,char[]);
int main(){
	char letterArray[500] = {0};
	// newArray[500]={0};
	char newArray[500]=createAbc(letterArray); //this does not work :(
	display(0,0,newArray); 

}
char createAbc(char letterArray[500]){

	 letterArray['Z' - 'A' + 1] ; 

for( char ch = 'A'; ch <= 'Z'; ch++ )
{
    letterArray[ch - 'A'] = ch ;

	//cout<<ch; 
}
}
Last edited on
The code that you had initialy looked fine except that you have not defined the display function.
It is defined on my computer. But since the actual code of Display doesn't seem relevant I decided to exclude it atm. I still can't figure out how to pass the contents of the array in creatAbc function into display function so that I can do stuff to the array from creatAbc in display function.

OMG, wow. So apparently it just works as long as you maintain this order.

createAbc(letterArray);
display(0,0,letterArray);

I did not know that...

Doing it that way, my display function randomly selects letters from the alphabet now. Now I just need to figure out how to use tolower properlly so that I can have random lower case and capital letters.

Last edited on
Arrays you passing to your functions can be modified:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void modifyArray(char arr[])
{
    arr[0] = '1';
}

void displayArray(char arr[])
{
    std::cout << arr[0];
}

int main()
{
    char array[1] = {'0'}//Note we initializating it as '0'
    modifyArray(array);
    displayArray(array);
}
displays:
1


That because arrays are very similar to pointers: You can do like:
1
2
3
4
5
6
7
int array[5] = {0, 1, 2, 3, 4};
array[3];//3
*array;//0
*(array + 1);//1
(array + 2)[0];//2
(array + 1000)[-999];//1
2[array];//2 - really blows your mind does it? 
Thanks guys. Now I'm just trying to see how to get my alphabet of capital letters to be both upper and lower case.
Ok I thought I understood what was going on. Except now when I'm trying to pass my counter array to display the count in another function it does not work the same way as it did above? Counter[i] is all zeros? Even though counter in Display function is assigned a value.


EDIT: Think I realize what I was doing wrong... I forgot to declare the array's scope per function and not just in one function.
Last edited on
Topic archived. No new replies allowed.