I have to write a program that finds a pattern in a text using Brute-force method.
I have made functions to read the text file, pattern file and to execute the brute force algorithm(called match). These are called in main(). i need to store the index (at which the pattern is found in the text file)in an array and then print out that array in main.
How do I pass the array by reference??? Please help!!
the program gives an error that you cannot pass an array by reference.
Also, whenever I declare an array (in the function, match()) like this:
int arr[text_file_size]; (text_file_size is the size of the text file obtained by fseek() and ftell())
the program gives an error that you cannot initialize a constant array of size 0.
I printed out the text_file_size,it was not zero.
Please help.
Remy the cook
0
Newbie Poster
Recommended Answers
Jump to PostAs firdousahmad said, you have to declare it in your calling function as void function(int *a). Let's see an example:
#include <iostream> using namespace std; void ssum(int* ar){ for (int i=0;i<5;i++){ ar[i]=ar[i]+100; } } int main(){ int array[5]={2,5,6,7,8}; ssum(array); for (int i=0;i<5;i++){ cout<<array[i]<<" "; } return (0); …
Jump to PostIn C++ you can certainly pass an array directly by reference:
#include <iostream> using namespace std; void foo(int (&a)[10]) { for (int i = 0; i < sizeof a / sizeof *a; ++i) { cout << a[i] << '\n'; } } int main() { int x[10] = …
Jump to PostAnother thing, arrays are by default passed by reference in C++.
Jump to PostAnother thing, arrays are by default passed by reference in C++.
Nope, they're passed as a pointer to the first element. This is a subtle but significant difference.
All 15 Replies
firdousahmad
-1
Junior Poster in Training
Lucaci Andrew
140
Za s|n
deceptikon
1,790
Code Sniper
Team Colleague
Featured Poster
Lucaci Andrew
140
Za s|n
deceptikon
1,790
Code Sniper
Team Colleague
Featured Poster
Lucaci Andrew
140
Za s|n
firdousahmad
-1
Junior Poster in Training
deceptikon
1,790
Code Sniper
Team Colleague
Featured Poster
Remy the cook
0
Newbie Poster
deceptikon
1,790
Code Sniper
Team Colleague
Featured Poster
Remy the cook
0
Newbie Poster
deceptikon
1,790
Code Sniper
Team Colleague
Featured Poster
Remy the cook
0
Newbie Poster
Lucaci Andrew
140
Za s|n
Remy the cook
0
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.