Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 指向指针函数参数的指针_C++_Function_Pointers - Fatal编程技术网

C++ 指向指针函数参数的指针

C++ 指向指针函数参数的指针,c++,function,pointers,C++,Function,Pointers,函数应该只读取一个矩阵。 为什么在我输入第一个字符后它会冻结 #include "stdafx.h" #include <iostream> using namespace std; void as(char **p,int n,int m) { int i, j; for (i = 0; i < n; i++) for (j = 0; j < m; j++) { cout << "p

函数应该只读取一个矩阵。 为什么在我输入第一个字符后它会冻结

#include "stdafx.h"
#include <iostream>

using namespace std;

void as(char **p,int n,int m)
{
    int i, j;
    for (i = 0; i < n; i++)
        for (j = 0; j < m; j++)
        {
            cout << "p[" << i << "][" << j << "]=";
            cin >> p[i][j];
        }
}

int main()
{
    char *a[100];
    as(a, 3, 3);
    return 0;
}
#包括“stdafx.h”
#包括
使用名称空间std;
无效为(字符**p,整数n,整数m)
{
int i,j;
对于(i=0;icout这是未定义的行为:您的数组是一个包含100个指向
char
的指针的数组。但是您从未初始化过它们。因此,当您寻址
p[i]
时,它会得到一个未初始化的指针,可以指向任何地方,当您用p[i][j取消引用它时然后,您可能会冻结或遭受任何其他未定义行为的症状

如果您想学习使用指针和数组: 解决方案1:将数组定义为
chara[100][100];

解决方案2:在as()的外循环中,开始分配
p[i]=new char[m];

如果您想学习现代C++,请: 解决方案3:忘记内存分配和释放,改用向量。向量完全是动态的,因此不再最多100行:

void as(vector<vector<char>> &p, int n, int m)
{
    p.resize(n); 
    int i, j;
    for (i = 0; i < n; i++) {
        p[i].resize(m); 
        for (j = 0; j < m; j++)
        {
            cout << "p[" << i << "][" << j << "]=";
            cin >> p[i][j];
        }
    }
}

int main()
{
    vector<vector<char>>a;
    as(a, 3, 3);
    return 0;
}
void as(向量&p,整数n,整数m)
{
p、 调整大小(n);
int i,j;
对于(i=0;icout我有一个关于mallocs reallocs和指针的简单伪数组。也许你会感兴趣:

typedef struct arr_str_t{
    size_t rows, columns;
    char **table;
}dynamicStringTable_t;

int CreateStringTable(dynamicStringTable_t **ptr, int rows, int columns)
{
    int result = 0;
    *ptr = (dynamicStringTable_t *)malloc(sizeof(dynamicStringTable_t));
    if (ptr == NULL) return - 1;
    (*ptr)->rows = rows;
    (*ptr)->columns = columns;
    (*ptr) -> table = (char *)malloc(rows * columns * sizeof(char *));
    if (*ptr == NULL)
    {
        free(*ptr);
        return -1;
    }
    for (int i = 0; i < rows * columns; i++) (*ptr)->table[i] = NULL;
    return 0;
}

char *getString(dynamicStringTable_t *ptr, int x, int y)
{
    char *result = (ptr == NULL || x >= ptr->columns || y >= ptr->rows || !x || !y) ? NULL : "";
    if (result != NULL)
    {
        result = ptr->table[x + y * ptr->rows];
    }
    return result;
}

int putString(dynamicStringTable_t *ptr, int x, int y, const char *str)
{
    int result = (ptr == NULL || x >= ptr->columns || y >= ptr->rows || str == NULL || !x || !y) * -1;
    if (!result)
    {
        char *tmp = (char *)realloc(ptr->table[x + y * ptr->rows], (strlen(str) + 1) * sizeof(char));
        if (tmp == NULL) result = -2;
        else
        {
            ptr->table[x + y * ptr->rows] = tmp;
            strcpy(tmp, str);
        }
    }
    return result;
}

int removeString(dynamicStringTable_t *ptr, int x, int y)
{
    int result = (ptr == NULL || x >= ptr->columns || y >= ptr->rows || !x || !y) * -1;

    if (!result)
    {
        free(ptr->table[x + y * ptr->rows]);
        ptr->table[x + y * ptr->rows] = NULL;
    }

        return result;
}

int destroyStringTable(dynamicStringTable_t *ptr, int x, int y)
{
    int result = (ptr == NULL || x >= ptr->columns || y >= ptr->rows || !x || !y) * -1;

    if (!result)
    {
        if (ptr->table != NULL)
        {
            for (int i = ptr->rows * ptr->columns - 1; i >= 0; i--)
                free(ptr->table[i]);
            free(ptr->table);
        }
        free(ptr);
    }
    return result;
}
typedef结构arr\u str\t{
行、列的大小;
字符**表;
}动态触发表;
int CreateStringTable(dynamicStringTable\u t**ptr、int行、int列)
{
int结果=0;
*ptr=(dynamicStringTable_t*)malloc(sizeof(dynamicStringTable_t));
if(ptr==NULL)返回-1;
(*ptr)->行=行;
(*ptr)->列=列;
(*ptr)->table=(char*)malloc(行*列*大小)(char*);
如果(*ptr==NULL)
{
免费(*ptr);
返回-1;
}
对于(inti=0;i表[i]=NULL;
返回0;
}
char*getString(dynamicStringTable_t*ptr,int x,int y)
{
字符*结果=(ptr==NULL | | x>=ptr->columns | | y>=ptr->rows | | |!x | |!y)?NULL:;
如果(结果!=NULL)
{
结果=ptr->表格[x+y*ptr->行];
}
返回结果;
}
int putString(dynamicStringTable_t*ptr、int x、int y、const char*str)
{
int result=(ptr==NULL | | x>=ptr->columns | | y>=ptr->rows | | str==NULL | | x | | y)*-1;
如果(!结果)
{
char*tmp=(char*)realloc(ptr->table[x+y*ptr->rows],(strlen(str)+1)*sizeof(char));
如果(tmp==NULL)结果=-2;
其他的
{
ptr->表[x+y*ptr->行]=tmp;
strcpy(tmp,str);
}
}
返回结果;
}
内部重新投资(dynamicStringTable_t*ptr,内部x,内部y)
{
int result=(ptr==NULL | | x>=ptr->columns | | y>=ptr->rows | | x | | y)*-1;
如果(!结果)
{
自由(ptr->table[x+y*ptr->rows]);
ptr->表[x+y*ptr->行]=NULL;
}
返回结果;
}
int destroyStringTable(dynamicStringTable_t*ptr,int x,int y)
{
int result=(ptr==NULL | | x>=ptr->columns | | y>=ptr->rows | | x | | y)*-1;
如果(!结果)
{
如果(ptr->table!=NULL)
{
对于(int i=ptr->rows*ptr->columns-1;i>=0;i--)
自由(ptr->表[i]);
免费(ptr->表格);
}
免费(ptr);
}
返回结果;
}

您的代码中有一个很大的问题。您正面临以下方面的问题:

char *a[100]; // an array of 100 pointer to a character
// it is not initialized yet
  • 上面没有初始化任何元素(甚至没有分配)
要更正代码,请执行以下操作:

char *a[100];
// Allocating the array of 100 elements on the heap:
for(int i(0); i < 100; i++){
    a[i] = new char[100]; // let's say your array is n = m
}

as(a, 3, 3);

for(int i = 0; i < 3; i++){
    for(int j(0); j < 3; j++)
        cout << a[i][j] << ", ";
    cout << endl;
}
char*a[100];
//在堆上分配100个元素的数组:
for(int i(0);i<100;i++){
a[i]=new char[100];//假设数组为n=m
}
as(a,3,3);
对于(int i=0;i<3;i++){
对于(int j(0);j<3;j++)

cout未定义的行为。您有一个未初始化指针数组,而不是二维整数数组。您可能需要参考post以了解如何正确使用高维数组。和数组的可能重复项是一维数组,而不是二维数组。@Javia1492确实如此,但这不是问题:您可以使用
a[i][j]来处理数组元素
是否将
a
定义为2d数组
char a[MAXI][MAXJ]
或指针的1d数组
char*a[MAXI]
(前提是您初始化此1d数组中的指针)。谢谢!不过我有一个问题。为什么需要在“vector&p”中引用p?@BeachWilliams当您传递数组ad参数时,将通过引用传递该参数(即,将传递指向数组开头的指针)。因此,在函数中更改数组的值将更改原始数组。除非像此处那样通过引用显式传递向量,否则向量将按值传递。因此,如果缺少-,则将更改向量的本地副本,而原始副本将保持为空。顺便提一下,另一种方法是在函数中创建向量打开并将其作为结果返回(按值)。
for(int i = 0; i < 100; i++)
    delete[] a[i];