C ++ Primer ex 7.16 - main的参数 [英] C++ Primer ex 7.16 - arguments to main
问题描述
我无法弄清楚错误:
/ * C ++ Primer - 4 / e
*
*练习7.16
*声明:
*编写一个接受main参数的程序。打印
*传递给main的值。
*
* /
#include< iostream>
int main(int argc,char ** argv)
{
std :: cout<< 这些参数被传递给''main()'':\t" ;;
/ *一个int vale可以轻松打印* /
std :: cout<< argc<< " \t" ;;
/ *来打印字符串数组,我们需要考虑2个指针:
一个字符串文字中的第一个字符在数组中显示第二个
到数组本身的第一个元素。
打印字符串文字我们将使用指针指向char
并在数组中移动我们将使用指向char的指针。
* /
char * pchar = 0;
pchar ** p_string = ** argv;
char * p_index = * argv;
while(* p_string ++!=''\ 0'')
{
while(** p_index!=''\ 0'')
{
std :: cout<< ** p_index ++;
}
std :: cout<< " \t";
}
返回0;
}
/ *输出
~ / programming / cpp $ g ++ -ansi -pedantic -Wall -Wextra ex_07-16.cpp
ex_07-16.cpp:在函数''int main( int,char **)'':
ex_07-16.cpp:28:错误:''p_string''未在此范围内声明
ex_07-16。 cpp:33:错误:''一元*''的无效类型参数
ex_07-16.cpp:35:错误:''一元*'的无效类型参数
~ / programming / cpp $
* /
-
http://arnuld.blogspot.com
g ++ -ansi -pedantic -Wall -Wextra ex_07-16.cpp
ex_07-16.cpp:在函数''int main(int,char **)'':
ex_07-16.cpp:28:错误:''p_string''未在此范围内声明
ex_07-16.cpp:33:错误:无效的类型参数''一元*''
ex_07-16.cpp:35:错误:''一元*''无效类型参数
~ / programming / cpp
* /
-
http://arnuld.blogspot.com
arnuld写道:
i我无法弄清楚错误:
/ * C ++ Primer - 4 / e
*
*练习7.16
*声明:
*编写一个接受main参数的程序。打印
*传递给main的值。
*
* /
#include< ; iostream>
int main(int argc,char ** argv)
{
std :: cout<< ; 这些参数被传递给''main()'':\t" ;;
/ *一个int vale可以轻松打印* /
std :: cout<< argc<< " \t" ;;
/ *来打印字符串数组,我们需要考虑2个指针:
一个字符串文字中的第一个字符在数组中显示和
第二个数组本身的第一个元素。
打印字符串文字我们将使用指针指向
char并在数组中移动我们将使用指向char的指针。
* /
char * pchar = 0;
''pchar''是变量名。
pchar ** p_string = ** argv ;
这里你试图使用''pchar'',好像它是一个类型。顺便说一句,你不需要
真的需要将''argv''分配给另一个变量*除非*你需要它
以后的其他东西。只需使用''argv''你想用的地方
''p_string''。
但是,真的,为什么你需要所有这些指针算术
时你可以使用索引吗?
char * p_index = * argv;
while(* p_string ++!=''\ 0'')
{
while(** p_index!=''\ 0'')
{
std :: cout<< ** p_index ++;
}
std :: cout<< " \t";
}
返回0;
}
/ * OUTPUT
~ / programming / cpp
i am not able to figure out the error:
/* C++ Primer - 4/e
*
* exercise 7.16
* STATEMENT:
* write a programme that accepts the arguments to main. print
* the values passed to main.
*
*/
#include <iostream>
int main( int argc, char **argv )
{
std::cout << "These arguments were passed to ''main()'' :\t";
/* an int vale can be printed easily */
std::cout << argc << "\t";
/* to prinit array of strings, we need to consider 2 pointers:
one to the 1st character in string literal presented in the array and 2nd
to the 1st element of the array itself.
to print the string literals we will use pointer to a pointer to char
and to move around in the array we will use a pointer to char.
*/
char *pchar = 0;
pchar **p_string = **argv;
char *p_index = *argv;
while( *p_string++ != ''\0'' )
{
while( **p_index != ''\0'' )
{
std::cout << **p_index++;
}
std::cout << "\t";
}
return 0;
}
/* OUTPUT
~/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra ex_07-16.cpp
ex_07-16.cpp: In function ''int main(int, char**)'':
ex_07-16.cpp:28: error: ''p_string'' was not declared in this scope
ex_07-16.cpp:33: error: invalid type argument of ''unary *''
ex_07-16.cpp:35: error: invalid type argument of ''unary *''
~/programming/cpp $
*/
--
http://arnuld.blogspot.com
解决方案g++ -ansi -pedantic -Wall -Wextra ex_07-16.cpp
ex_07-16.cpp: In function ''int main(int, char**)'':
ex_07-16.cpp:28: error: ''p_string'' was not declared in this scope
ex_07-16.cpp:33: error: invalid type argument of ''unary *''
ex_07-16.cpp:35: error: invalid type argument of ''unary *''
~/programming/cpp
*/
--
http://arnuld.blogspot.com
arnuld wrote:i am not able to figure out the error:
/* C++ Primer - 4/e
*
* exercise 7.16
* STATEMENT:
* write a programme that accepts the arguments to main. print
* the values passed to main.
*
*/
#include <iostream>
int main( int argc, char **argv )
{
std::cout << "These arguments were passed to ''main()'' :\t";
/* an int vale can be printed easily */
std::cout << argc << "\t";
/* to prinit array of strings, we need to consider 2 pointers:
one to the 1st character in string literal presented in the array and
2nd to the 1st element of the array itself.
to print the string literals we will use pointer to a pointer to
char and to move around in the array we will use a pointer to char.
*/
char *pchar = 0;''pchar'' is a variable name.
pchar **p_string = **argv;Here you''re trying to use ''pchar'' as if it is a type. BTW, you don''t
really need to assign ''argv'' to another variable *unless* you need it
for something else later. Just use ''argv'' where you wanted to use
''p_string''.
But, really, why do you need all this pointer arithmetic exposed when
you can just use indexing?
char *p_index = *argv;
while( *p_string++ != ''\0'' )
{
while( **p_index != ''\0'' )
{
std::cout << **p_index++;
}
std::cout << "\t";
}
return 0;
}
/* OUTPUT
~/programming/cpp
这篇关于C ++ Primer ex 7.16 - main的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!