0

I have a CPP program (let's call it program 1) which calls another CPP program (Program 2). This Program2 is built in debug mode. That is:

   int main(int argc, char** argv) {
    ///Function body#
     eerot[0] = atof(argv[1]); 
eerot[1] = atof(argv[2]); 
eerot[2] = atof(argv[3]); 
eetrans[0] = atof(argv[4]); //Exception thrown here due to memory clash
     ///Continue
    }

The above fragment of the code describes the structure of my main program in program2. Program2 has neither execution error nor does it throw any exception. Now I want to call this main function of program2 from program1 but I am stuck with the double pointer variable. Because the argv variable is double pointer, and even though I was able to create a string, double reference it and pass it as parameter to program2, I get exceptions. There is no compile error. All the appropriate headers are perfectly defined.

I cannot edit program2 since it was developed by some other team and it is quite difficult to follow. I have changed the name of main function of program 2(say to "calculate()") and called that from main of program1. I have also included directories and header files appropriately to make all functions and classes of program2 visible in program1. Right now only thing I am stuck is with the double pointer argv variable. How can I pass the parameters which I have to pass via the command prompt be passed via another program?

Note that the parameters we pass in command prompt for program2 is of type double. I tried to use arrays but I keep getting exceptions thrown at me. Let me know anything that comes to your mind when trying to solve the problem.

LoLance
  • 25,666
  • 1
  • 39
  • 73

2 Answers2

0

I believe the problem is the triple pointer in the main() declaration. You probably meant:

int main(int argc, char** argv) {

or

int main(int argc, char* argv[]) {

donjuedo
  • 2,475
  • 18
  • 28
  • Thank you. Yeah I made a typo there. I changed it. However my problem is still there – prashanth kumar May 21 '19 at 19:15
  • @prashanthkumar argv[0] is the invoked program name. argv[1] is the first parameter. What happens if you run the program with an extra parameter or two? If no exception, that would strongly hint at a mistake in the indexes of the parameters used. – donjuedo May 21 '19 at 19:28
0

you want either char **argv ... OR char *argv[], NOT char **argv[]

both of the above are pointers to arrays. What you have is a pointer to a 2D array basically so the way you are accessing the elements ends up exceeding the number of array elements.

Ryan
  • 106
  • 1
  • 4
  • Sorry about that. I made a typo there. But my problem persists even if I just use **argv or *argv[] – prashanth kumar May 21 '19 at 19:18
  • argc will be the number of strings entered through command line arguments, maybe add some error checking so you never access argv[argc] or higher? – Ryan May 21 '19 at 20:43