-1

我不明白这段代码中 *str[] 的使用,它与这段代码中使用的 str[][] 有什么不同吗?

#include<iostream.h>
#include<fstream.h>
#include<conio.h>

int main()
{
    char a[10], b[10], c, buffer[50], str[1][9], s[10];
    //decalaring char a, b, buffer, str[][], s//
    int y;

    ofstream out;
    out.open("output.cpp");
    out<<("\nOPCODE\tMACHINE_CODE\n");
    do
    {
        y = 0;
        cout<<"ENTER THE OPCODE&MACHINE CODE";
        cin>>a>>b;
        out<<"\n"<<a<<"\t"<<b<<"\t"<<"\n";
        cout<<"PRESS 1 TO CONTINUE";
        cin>>y;
    }while(y == 1);
    out.close();
    ifstream in;
    in.open("output.cpp");
    while(in)
    {
        c = in.get();
        cout<<c;
    }
    in.close();
    cout<<"ENTER THE OPCODE TO SEARCH";
    cin>>s;
    in.open("output.cpp");
    in.getline(buffer,50);
    while(in)
    {
        *str[0] = '\0'; // i dont understand the use of *str[] here, is it diff from str[][]?
        *str[1] = '\0';
        in>>str[0];
        in>>str[1];
        if(strcmpi(str[0], s) == 0)
        {
            cout<<"\n"<<str[0]<<"\t"<<str[1];
        }
    }
    return 0;
}
4

1 回答 1

1

*x意思是“取消引用指针x”。现在,只要您有一个数组位于语言期望指针的位置,它就会隐式衰减为指向第一个元素的指针。

所以

*str[0]='\0';

意思是“分配'\0'给 1 元素字符数组str[0]的第一个元素,它本身就是 9 元素数组的第一个元素str”。

意思是完全一样的

str[0][0] = '\0';

并且可以说应该是这样写的。或者str应该char str[9]首先声明为。

于 2013-08-11T20:56:55.397 回答