简体   繁体   English

C ++将char数组传递给void函数

[英]c++ pass char array to void function

Task is to change every uppercase letter to lowercase and vice versa, and increase every number by 1, and change 9 for 0. 任务是将每个大写字母更改为小写,反之亦然,并将每个数字增加1,并将9更改为0。

#include <iostream>
using namespace std;

void modif ( char*& arr){
    for (int i=0;arr[i]!=0;i++)
        if ('a'<=arr[i] && arr[i]<='z'){
            arr[i]=char(((int)arr[i])-32);
        }
        else if ('A'<=arr[i] && arr[i]<='Z'){
            arr[i]=char(((int)arr[i])+32);
        }
        else if ('0'<=arr[i] && arr[i]<='8'){
            arr[i]=char(((int)arr[i])+1);
        }
        else if (arr[i]== '9'){
            arr[i]=char(((int)arr[i])-8);
        }
}

int main() {
    char * s=new char [100];
    cin.getline (s, 100);
    void modif (char * s);

    cout << s  << endl;
    delete [] s;
}

I can get it to work without void function, but can't with it 我可以在没有void函数的情况下使其正常工作,但不能使用它

Try this so that you actually call the function: 尝试这样做,以便您实际调用该函数:

int main() {
    char * s=new char [100];
    cin.getline (s, 100);
    // void modif (char * s); // removed line
    modif(s);                 // new line

    cout << s  << endl;
    delete [] s;

} }

This line: 这行:

void modif (char * s);

is a function declaration, you want to call it with: 是一个函数声明,您要使用以下命令调用它:

modif(s);

also you dont need char*& in your function, char* will suffice. 您也不需要在函数中使用char*&char*就足够了。

void modif (char * s); is a forward declaration . 前瞻性声明 It is unnecessary in your example, because modif is defined above its point of invocation. 在您的示例中这是不必要的,因为modif是在其调用点之上定义的。 You would need it if you wanted to move modif to a place in the file below main , or to a different file altogether: 如果您想将modif移到main下方文件中的某个位置,或者完全移至另一个文件,则需要它:

#include <iostream>
using namespace std;

int main() {
    char * s=new char [100];
    cin.getline (s, 100);
    void modif (char*&); // Forward declaration
    modif(s);            // Call
    cout << s  << endl;
    delete [] s;
}

void modif ( char*& arr){
    for (int i=0;arr[i]!=0;i++)
    if ('a'<=arr[i] && arr[i]<='z'){
       arr[i]=char(((int)arr[i])-32);
    }

   else if ('A'<=arr[i] && arr[i]<='Z'){
       arr[i]=char(((int)arr[i])+32);
    }
    else if ('0'<=arr[i] && arr[i]<='8'){
       arr[i]=char(((int)arr[i])+1);
    }
    else if (arr[i]== '9'){
       arr[i]=char(((int)arr[i])-8);
    }
}

Forward declaration is a promise to the compiler that there is going to be a definition of your function somewhere else. 前向声明向编译器保证在其他地方将定义您的函数。 This way the compiler knows how to make a call if you choose to call your function. 这样,如果您选择调用函数,则编译器知道如何进行调用。 Note that the signatures in the declaration and in the definition must match. 请注意,声明和定义中的签名必须匹配。

The call, on the other hand, looks like this: 另一方面,该呼叫如下所示:

modif(s);

Here, you tell the compiler that you want to invoke function modif , and pass the current value of s for its formal parameter called arr . 在这里,您告诉编译器您要调用函数modif ,并将s的当前值传递给它的形式参数arr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM