R
Robin Sanderson
Sorry in advance if this is a stupid question - I am new to C++.
In the process of converting program to be run from the command line
into a function to be run from another program I noticed behaviour
that I do not understand. Consider the example programs below:
Program 1 below is a simple program that merely outputs the command
line arguments. This compiles and runs fine with Microsoft Visual C++
6.0 and g++ 3.3.1.
Program 2 brings up a compile time error in both; in MVC++ it is
"test_cline_2.cpp: In function `int main()':
test_cline_2.cpp:19: error: non-lvalue in increment",
and in g++ it is
"test_cline_2.cpp(19) : error C2105: '++' needs l-value".
Program 3 compiles and runs fine.
Can anyone tell me why Program 2 does not compile? A friend has tried
the code on C++ .NET and apparently it *did* compile.
Thanks in advance,
Robin Sanderson
-----
//Program 1
#include <iostream>
using std:: cout; using std::endl;
int main(int argc, char * argv[])
{
int i;
for(i=0;i<argc;i++)
{
cout << "i="<<i<<"; *argv = " << *argv << endl;
argv++;
}
return 0;
}
-----
//Program 2
#include <iostream>
using std:: cout; using std::endl;
int main()
{
int i;
int argc=3;
char * argv[3];
argv[0] = "program_name";
argv[1] = "argument_1";
argv[2] = "argument_2";
for(i=0;i<argc;i++)
{
cout << "i="<<i<<"; *argv = " << *argv << endl;
argv++;
}
return 0;
}
-----
//Program 3
#include <iostream>
using std:: cout; using std::endl;
void vFunction(int argc,char * argv[]);
int main()
{
int iInt;
char * cArray[4];
iInt=3;
cArray[0] = "program_name";
cArray[1] = "argument_1";
cArray[2] = "argument_2";
cArray[3] = NULL;
vFunction(iInt,cArray);
return 0;
}
void vFunction(int argc, char ** argv)
{
int i;
cout << "Using argv++:" << endl;
for(i=0;i<argc;i++)
{
cout << "i="<<i<<"; *argv = " << *argv << endl;
argv++;
}
cout << endl;
return;
}
In the process of converting program to be run from the command line
into a function to be run from another program I noticed behaviour
that I do not understand. Consider the example programs below:
Program 1 below is a simple program that merely outputs the command
line arguments. This compiles and runs fine with Microsoft Visual C++
6.0 and g++ 3.3.1.
Program 2 brings up a compile time error in both; in MVC++ it is
"test_cline_2.cpp: In function `int main()':
test_cline_2.cpp:19: error: non-lvalue in increment",
and in g++ it is
"test_cline_2.cpp(19) : error C2105: '++' needs l-value".
Program 3 compiles and runs fine.
Can anyone tell me why Program 2 does not compile? A friend has tried
the code on C++ .NET and apparently it *did* compile.
Thanks in advance,
Robin Sanderson
-----
//Program 1
#include <iostream>
using std:: cout; using std::endl;
int main(int argc, char * argv[])
{
int i;
for(i=0;i<argc;i++)
{
cout << "i="<<i<<"; *argv = " << *argv << endl;
argv++;
}
return 0;
}
-----
//Program 2
#include <iostream>
using std:: cout; using std::endl;
int main()
{
int i;
int argc=3;
char * argv[3];
argv[0] = "program_name";
argv[1] = "argument_1";
argv[2] = "argument_2";
for(i=0;i<argc;i++)
{
cout << "i="<<i<<"; *argv = " << *argv << endl;
argv++;
}
return 0;
}
-----
//Program 3
#include <iostream>
using std:: cout; using std::endl;
void vFunction(int argc,char * argv[]);
int main()
{
int iInt;
char * cArray[4];
iInt=3;
cArray[0] = "program_name";
cArray[1] = "argument_1";
cArray[2] = "argument_2";
cArray[3] = NULL;
vFunction(iInt,cArray);
return 0;
}
void vFunction(int argc, char ** argv)
{
int i;
cout << "Using argv++:" << endl;
for(i=0;i<argc;i++)
{
cout << "i="<<i<<"; *argv = " << *argv << endl;
argv++;
}
cout << endl;
return;
}