C++ 如何在代码中正确使用指针

C++ 如何在代码中正确使用指针,c++,pointers,C++,Pointers,对于下面的代码,如何使用指针找到[A^-1](公式[A^-1]=1/det(A)?我不确定指针是否用于算术或是否用于调用值。解释什么是指针很好,因为我甚至不确定它们是做什么的。我已经编写了大部分代码,除了这一部分,因此非常感谢任何帮助。提前感谢 #include <iostream> #include <cmath> #include <string> #include <fstream> #include <ctime> #inclu

对于下面的代码,如何使用指针找到[A^-1](公式[A^-1]=1/det(A)?我不确定指针是否用于算术或是否用于调用值。解释什么是指针很好,因为我甚至不确定它们是做什么的。我已经编写了大部分代码,除了这一部分,因此非常感谢任何帮助。提前感谢

#include <iostream>
#include <cmath>
#include <string>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <bits/stdc++.h>
#define N 3

using namespace std;
template<class T>

void display(T A[N][N])
{
for (int i=0; i<N; i++)
{

for (int j=0; j<N; j++)     
cout << A[i][j] << "\t\t";
cout << endl;
}
}

template<class T>

void display(T A[N])

{

for (int i=0; i<N; i++)

{
cout << A[i]<< "\n";
}
}

void getCofactor(int A[N][N], int temp[N][N], int p, int q, int n)

{

int i = 0, j = 0;

for (int row = 0; row < n; row++)

{

for (int col = 0; col < n; col++)

{
if (row != p && col != q)

{

temp[i][j++] = A[row][col];

if (j == n - 1)
{

j = 0;

i++;

}

}

}

}

}

int determinant(int A[N][N], int n)
{

int D = 0; 

if (n == 1)

return A[0][0];

int temp[N][N]; 

int sign = 1; 

for (int f = 0; f < n; f++)

{
getCofactor(A, temp, 0, f, n);

D += sign * A[0][f] * determinant(temp, n - 1);
sign = -sign;
}

return D;
}

void adjoint(int A[N][N],int adj[N][N])
{
if (N == 1)
{

adj[0][0] = 1;

return;
}

int sign = 1, temp[N][N];

for (int i=0; i<N; i++)
{

for (int j=0; j<N; j++)
{
getCofactor(A, temp, i, j, N);
sign = ((i+j)%2==0)? 1: -1;
    adj[j][i] = (sign)*(determinant(temp, N-1));
}
}
}

bool inverse(int A[N][N]){

int det = determinant(A, N);

if (det == 0){

cout << "Singular matrix, can't find its inverse";

return false;
}

return true;
}

void computeInverse(int A[N][N], float inverse[N][N]){

int det = determinant(A, N);

int adj[N][N];

adjoint(A, adj);

for (int i=0; i<N; i++)

for (int j=0; j<N; j++)

inverse[i][j] = adj[i][j]/float(det);

cout<<"\nThe Inverse of the Matrix A is:"<<endl;

display(inverse);

}
int main()
{
system("cls");

int A[N][N] = {{-1, 4, -2}, {-3, -2, +1}, {+2, -5, +3}};

char X[N];

int B[N];

float inv[N][N];

cout<<"\nEnter a 3*3 Matrix A"<<endl;

for(int i=0;i<N;i++){

for(int j=0;j<N;j++){

cin>>A[i][j];

}

}

cout<<"\nEnter variables x, y, z for Matrix X"<<endl;

for(int i=0;i<N;i++){

cin>>X[i];

 }

 if (X[0] == 'x' && X[1] == 'y' && X[2] == 'z')

 cout<<"\nMatrix X is Valid"<<endl;

else{

cout<<"\nMatrix X is Invalid"<<endl;

return -1;

}

cout<<"\nEnter values for Matrix B"<<endl;

for(int i=0; i<N; i++)

cin>>B[i];

cout<<"\nMatrix A is:------->\n";

display(A);

cout<<"\nMatrix X is:------->\n";

display(X);

cout<<"\nMatrix B is:------->\n";

display(B);

bool isInverseExist = inverse(A);

if (isInverseExist)

computeInverse(A, inv);

return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义n3
使用名称空间std;
样板
无效显示(T A[N][N])
{

对于(int i=0;i好的,下面是一个使用字符数组的简单指针示例。这也适用于其他数据类型。不记得我在哪里读过它;将指针视为空瓶子

让我们先用英语把它分解一下,然后我将分享一个可以编译的简单示例。假设你有10个m&m坐在你面前的桌子上。 这些m&m表示一个字符数组,我们称之为
char mm[10];
假设此数组中填充了表示所有10个m&m颜色的字符

现在,我们不想整天把糖果放在桌子上,所以我们将把所有十个m&m储存在一个专门为m&m制作的特殊罐子里。这个罐子将被称为
char*mm\u ptr;

现在我们剩下一张m&m表和一个罐子,放在堆旁边。下一步是用m&m填满罐子,你这样做:
mm\u ptr=mm;

您现在有了一个“装满m&m的罐子”。这允许您直接从罐子访问m&m

下面是一个将m&m放入罐子的工作示例:

#include <iostream>
//using namespace std;


int main(){//The main function is our table

        char mm[10];    //Our pile of m&m's

        for(int i=0; i<10; i++){
                if(i < 5){
                        mm[i] = 'b';    //Lets say that half of the m&m's are blue
                }else{
                        mm[i] = 'r';    //and that the other half is red
                }
        }

        char* mm_ptr;           //This is our bottle that we will 
                                //use to store our m&m's

        mm_ptr = mm;    //this is us storing the m&m's in the jar!

        for(int i=0; i<10; i++){//Lets dump those candies back onto the table!
                std::cout<<mm_ptr[i]<<std::endl;

        }
        return 0;
}
#包括
//使用名称空间std;
int main(){//主函数是我们的表
char-mm[10];//我们那堆m&m's

对于(int i=0;i指针的可能副本只是一个变量,它将其他对象的地址作为其值。换句话说,指针指向可以找到其他对象的地址。例如,
int A=5;
将立即值
5
存储为其值,
int*b;
创建一个指向
int
b=&a;
存储
a
的地址(当前存储
5
的内存地址)作为其值。由于
b
指向存储
5
的地址,如果您更改该值(例如
*b=6;
6
现在存储在
5
以前的地址。如果你希望其他人阅读你的问题,你应该不厌其烦地正确缩进你的代码。注意:作为初学者,千万不要使用浮点。由于只有7位精度,很容易失去准确性,最终没有一个有意义的答案。始终使用双精度除非你真的、真的知道自己在做什么,否则不要使用浮动广告。即使这样,通常也是错误的。@RCE11我试图回答你之前的问题。当你承认我在这个问题下面的“答案”时(只需给我留下一条评论,就行了),我会继续删除它,因为它不属于这里。但你的问题是有效的,值得回答。