argv /指针问题 [英] argv/pointer problems

查看:45
本文介绍了argv /指针问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这是一个愚蠢的问题,请提前抱歉 - 我是C ++的新手。


在从命令行转换程序的过程中

进入一个从另一个程序运行的函数我注意到了行为

我不明白。考虑下面的示例程序:


下面的程序1是一个简单的程序,只输出命令

行参数。使用Microsoft Visual C ++编译并运行良好

6.0和g ++ 3.3.1。


程序2在两者中都会出现编译时错误;在MVC ++中它是


" test_cline_2.cpp:在函数`int main()''中:

test_cline_2.cpp:19:错误:非lvalue in increment",


,以g ++为单位


" test_cline_2.cpp(19):错误C2105:''++ ''需要l-value'。


程序3编译并运行正常。


有谁能告诉我为什么程序2不能编译?一位朋友已经尝试过使用C ++ .NET上的代码,显然它已*编译好了。


提前谢谢,


Robin Sanderson


-----


//程序1

#include< ; iostream>

使用std :: cout;使用std :: endl;


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

{

int i;


for(i = 0; i< argc; i ++)

{

cout<< " I ="<< I<<英寸; * argv =" << * argv<< endl;

argv ++;

}


返回0;

}
< br $>
-----


//程序2

#include< iostream>

使用std :: cout;使用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 ++;

}


返回0;

}
< br $>
-----


//程序3

#include< iostream>

使用std :: cout;使用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);


返回0 ;

}


void vFunction(int argc,char ** argv)

{

int i;


cout<< 使用argv ++: << endl;

for(i = 0; i< argc; i ++)

{

cout<< " I ="<< I<<英寸; * argv =" << * argv<< endl;

argv ++;

}

cout<<结束;


返回;

}

解决方案

Robin Sanderson写道:

如果这是一个愚蠢的问题,请提前抱歉 - 我是C ++的新手。

在从命令行转换程序的过程中<进入一个从另一个程序运行的函数我注意到我不理解的行为。考虑下面的示例程序:

下面的程序1是一个简单的程序,只输出命令
行参数。使用Microsoft Visual C ++
6.0和g ++ 3.3.1编译并运行良好。

程序2在两者中都会产生编译时错误;在MVC ++中它是

" test_cline_2.cpp:在函数`int main()''中:
test_cline_2.cpp:19:错误:非增值的左值,

以g ++为单位

" test_cline_2.cpp(19):错误C2105:'''''''需要l值。

程序3编译并运行正常。

任何人都可以告诉我为什么程序2不能编译?一位朋友尝试过使用C ++ .NET上的代码,显然它已*编译好了。

提前致谢,

罗宾·桑德森



//程序1
#include< iostream>
使用std :: cout;使用std :: endl;

{
int i;

for(i = 0; i< argc; i ++)
{
cout<< " I ="<< I<<英寸; * argv =" << * argv<< endl;
argv ++;


返回0;
}


在这个程序中,argv是一个指针。

-----

//程序2
#include< iostream>
使用std :: cout;使用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 ++;
}

返回0;
}


在这个程序中,argv是一个数组。你不能增加数组。

-----

//程序3
#include< iostream>
使用std :: cout;使用std :: endl;

void vFunction(int argc,char * argv []);

int main()
{i / int} ;
char * cArray [4];

iInt = 3;
cArray [0] =" program_name";
cArray [1] =" argument_1" ;;
cArray [2] =" argument_2" ;;
cArray [3] = NULL;

vFunction(iInt,cArray);

返回0;
}

void vFunction(int argc,char ** argv)
{
int i;

cout< < 使用argv ++: << endl;
for(i = 0; i< argc; i ++)
{
cout<< " I ="<< I<<英寸; * argv =" << * argv<< endl;
argv ++;
}
cout<<结束;

返回;
}




这里,argv再次成为指针。


-

2小时?男孩,那太老了。您应该使用更新版本。

(Waldo Bastian关于2小时CVS版KDE中的复制错误)


< blockquote>谢谢,罗尔夫。任何人都可以解释为什么代码在.NET上运行?


我刚刚注意到我发布了错误版本的Programm 3 -

the ; ** argv应该是* argv [],这在下面更正

- 虽然这不会改变它是否编译。


这个可能是一个(另一个)愚蠢的问题,但它是什么使得使用char * argv [3]之间的

差异。在计划2和char

* argv []中在计划3?是否可以在程序2中将argv声明为指针

(因此它可以与其他

程序中使用的方式相同)?


谢谢,


Robin

---- -

//程序3
#include< iostream>
使用std :: cout;使用std :: endl;

void vFunction(int argc,char * argv []);

int main()
{i / int} ;
char * cArray [4];

iInt = 3;
cArray [0] =" program_name";
cArray [1] =" argument_1" ;;
cArray [2] =" argument_2" ;;
cArray [3] = NULL;

vFunction(iInt,cArray);

返回0;
}

void vFunction(int argc,char * argv []){
int i;

cout<< 使用argv ++: << endl;
for(i = 0; i< argc; i ++)
{
cout<< " I ="<< I<<英寸; * argv =" << * argv<< endl;
argv ++;
}
cout<< endl;

返回;
}



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;
}

解决方案

Robin Sanderson wrote:

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;
}
In this program, argv is a pointer.

-----

//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;
}
In this program, argv is an array. You can''t increment an array.
-----

//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;
}



Here, argv is again a pointer.

--
2 hours? Boy, that''s way too old. You should use a more recent version.
(Waldo Bastian about a copile error in a 2 hours old CVS version of KDE)


Thanks, Rolf. Can anyone explain why the code runs on .NET?


I have just noticed that I posted the wrong version of Programm 3 -
the "** argv" should have been "* argv[]", and this is corrected below
- although this does not alter whether or not it compiles.

This may be a(nother) daft question, but what is it that makes the
difference between the use of "char * argv[3]" in Program 2 and "char
* argv[]" in Program 3? Is it possible to declare argv as a pointer
in Program 2 (so it can be used in an identical way as in the other
programs)?

Thanks,

Robin

-----

//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;
}



这篇关于argv /指针问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆