C++我需要了解在哪里使用指针和双指针

C++ I need to understand where to use pointers and double pointers

本文关键字:指针 在哪里 了解 C++      更新时间:2023-10-16

我有一个基本的多工具程序,目标是在包含一些字符串的文件上完成四个函数。。以字符串为例,取一行并将其大写。我知道它还不完美,但我需要理解为什么我的char*和char**做得一团糟。

非常感谢您抽出时间


#include <iostream> //to use enter and exit
#include <cstring>
#include <string> 
#include <fstream> //to use files
using namespace std;
/*--------------------------FONCTION PART-----------------------------------*/
//define one fonction for each transformation 
//FONCTION 1 
void fonctionu (char* argv){

char pattern = argv[2];
char file = argv[3];

fstream data(file, ios::in);  

if (data){
string line;
while (getline (data ,line)){
unsigned found = line.find(pattern);
if (found != string::npos) {
for (int i = 0; line[i]!=''; i++) {
if(line[i] >= 'a' && line[i] <= 'z') {
line[i] = line[i] - (32);// (ASCII)
cout<<line[i];
}}  }
if (found == string::npos) {
for (int i = 0; line[i]!=''; i++) {
cout<<line[i];
}}}}}
//FONCTION 2
void fonctiond( char* argv){ //remove line with xyz
char pattern = argv[2];
char file = argv[3];
fstream data(file, ios::in);

if (data){
string line;
while (getline (data ,line)){
unsigned found = line.find(pattern); //as a reminder argv[2] is the pattern (warning : it need to be an unsigned to compare two unsigned)
if (found != string::npos) {
//delete the line of the file when there is the pattern 
cout<<''; }
if (found == string::npos){
for (int i = 0; line[i]!=''; i++) {
cout<< line[i];
}}}}}
//FONCTION 3 
void fonctionc( char* argv){
char pattern = argv[2];
char file = argv[3];
fstream data(file, ios::in); 

if (data ){
string line;
while (getline (data ,line)){
unsigned found = line.find(pattern); //as a reminder argv[2] is the pattern 
if (found != string::npos) {
for (int i = 0; line[i]!=''; i++) {
cout<< "33[31m"<< line[i]; //the line will be red
} 
}
if (found == string::npos){
for (int i = 0; line[i]!=''; i++) {
cout<< line[i];
}}}}}
//FONCTION 4 
//replace the pattern xyz by abc
void fonctions ( char* argv){
char pattern = argv[2];
char file = argv[3];
fstream data(file, ios::in); 

if (data ){
string line;
while (getline (data ,line)){
unsigned found = line.find(pattern);
if (found != string::npos) {
for (int i = 0; line[i]!=''; i++) {
if(line[i] >= 'a' && line[i] <= 'z') {
line[i] = line[i] - (97); // (ASCII) //we creat a shift that allow to make the x became a 'a' and the y a 'b'...etc
cout<<line[i];}} 
} 
if (found == string::npos) {
for (int i = 0; line[i]!=''; i++) {
cout<<line[i];
}}}}}

/*--------------------------MAIN PART---------------------------------------*/
int main(char* argv){
// ipsacs : argv[0]
//option : 
string a = argv[1]; //string ! to compare line 103,108 and 112
// pattern : argv[2];
//file :  argv[3];

if (argv[1]=='' || argv[2]==''){
cout <<"ERROR"<<'n';}
if (a == "u"){
char fonctionu (argv);}
if (a== "d"){
char fonctiond(argv);}
if (a == "c"){
char fonctionc(argv);}
if (a == "s"){
char fonctions(argv);} 
return 0;
}
/*--------------------------TEST PART--------------------------------------*/
/*
pour compiler : g++ -Wall ipsacs.cpp -o ipsacs
./ipsacs u xyz foo //how the program would receive arguments, basically
Le programme ne renvoie rien. 
*/

这是编译日志:

main.cpp:22:30: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
fstream data(file, ios::in);  
^
In file included from main.cpp:8:0:
/usr/include/c++/6/fstream:902:7: note:   initializing argument 1 of ‘std::basic_fstream<_CharT, _Traits>::basic_fstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits; std::ios_base::openmode = std::_Ios_Openmode]’
basic_fstream(const char* __s,
^~~~~~~~~~~~~
main.cpp: In function ‘void fonctiond(char*)’:
main.cpp:46:29: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
fstream data(file, ios::in);
^
In file included from main.cpp:8:0:
/usr/include/c++/6/fstream:902:7: note:   initializing argument 1 of ‘std::basic_fstream<_CharT, _Traits>::basic_fstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits; std::ios_base::openmode = std::_Ios_Openmode]’
basic_fstream(const char* __s,
^~~~~~~~~~~~~
main.cpp: In function ‘void fonctionc(char*)’:
main.cpp:66:29: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
fstream data(file, ios::in); 
^
In file included from main.cpp:8:0:
/usr/include/c++/6/fstream:902:7: note:   initializing argument 1 of ‘std::basic_fstream<_CharT, _Traits>::basic_fstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits; std::ios_base::openmode = std::_Ios_Openmode]’
basic_fstream(const char* __s,
^~~~~~~~~~~~~
main.cpp: In function ‘void fonctions(char*)’:
main.cpp:88:29: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
fstream data(file, ios::in); 
^
In file included from main.cpp:8:0:
/usr/include/c++/6/fstream:902:7: note:   initializing argument 1 of ‘std::basic_fstream<_CharT, _Traits>::basic_fstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits; std::ios_base::openmode = std::_Ios_Openmode]’
basic_fstream(const char* __s,
^~~~~~~~~~~~~
main.cpp: At global scope:
main.cpp:109:5: warning: first argument of ‘int main(char*)’ should be ‘int’ [-Wmain]
int main(char* argv){
^~~~
main.cpp:109:5: warning: ‘int main(char*)’ takes only zero or two arguments [-Wmain]
main.cpp: In function ‘int main(char*)’:
main.cpp:113:20: error: conversion from ‘char’ to non-scalar type ‘std::string {aka std::basic_string}’ requested
string a = argv[1]; //string ! to compare line 103,108 and 112
~~~~~~^
main.cpp:121:26: error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
char fonctionu (argv);}
^
main.cpp:123:25: error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
char fonctiond(argv);}
^
main.cpp:125:27: error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
char fonctionc(argv);}
^
main.cpp:127:27: error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
char fonctions(argv);} 
^

char *是指向包含字符的内存地址的指针,通常用于c字符串。

char **是指向内存地址的指针,其中包含指向内存地址(其中包含字符(的指针,通常用于c字符串数组。

您的main签名不是有效的形式,因为main没有只接受字符指针的形式。您最可能要查找的表单是int main( int argc, int **argv )。这需要两个参数:argc是传递给程序的参数数量,argv包含传递给程序作为c字符串的参数。

在主方法中,您通常会确保argc是正确的,即您有正确数量的参数传递到您的函数中(注意:argv[0]是程序名称时,它总是至少为1(。

std::string a = argv[1]很好,它将导致argv指向的字符串转换为字符串。调用函数时,您有char functionu(argv);。您将希望从该行中删除char;实际上,按照它的编写方式,您正在尝试创建一个名为functionuchar变量,该变量的初始值为argv

你的每个函数,而不是接受char *,应该接受char **,然后当你有类似char file = argv[3];的东西时,你会想把它改为char *file = argv[3];std::string file = argv[3];