X 在此函数中使用未初始化

X is used uninitialized in this function

为什么我会收到此警告?: 警告:'row1[3]' 在此函数中使用未初始化 [-Wuninitialized] 我已经用谷歌搜索了一段时间,但我找不到任何答案,可能只是因为我不擅长在 Google.

上搜索答案
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int setfunc(int x);

int main()
{
    int row1[3]{0,0,0};
    setfunc(row1[3]);
}

int setfunc(int x[])
{
    string sub;
    int rowset;
    stringstream rs;
    string snums;
    int elementnum = sizeof(x) / sizeof(0);
    for(int z = 1; z <= elementnum; z++)
    {
        int find = snums.find(",");
        if(find == -1)break;
        else
        {
        sub = snums.substr(0, snums.find(","));
        rs << sub;
        rs >> rowset;
        snums.erase(0, snums.find(",") +1);
        }
        x[z] = rowset;
        cout << x[z] << endl;
    }
return 0;

}

感谢所有帮助

int row1[3]{0,0,0}; setfunc(row1[3]); 的行为是未定义。这是因为索引从 0 到 2,所以 row1[3] 正在访问其范围之外的数组。编译器在这里帮助你,虽然在我看来,这个警告有点误导。

sizeof(x) / sizeof(0); 也不正确。 sizeof(0)int 的大小,因为 0int 类型。正常的成语是sizeof(x) / sizeof(x[0])。但是你 不能 在你的情况下这样做,因为函数参数 xdecayed 成一个指针。您应该明确地将元素的数量传递给函数。