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.
Recommended Answers
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++.
Nope, they're passed as a pointer to the first element. This is a subtle but significant difference.
Jump to PostPerhaps that's why many books consider passing an array as always being passed by reference.
Certainly not any good books. Like I said, there's a subtle but significant difference between a pointer and a true reference.
Jump to PostI was told to read the file by using SEEK_SET in fseek to avoid opening the file again.But I don't know how to implement it.
Just read the file again. It's not gonna kill you, and it'll certainly keep your code simpler and less buggy. ;)
int …
All 15 Replies
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.