简体   繁体   English

是否可以将指针数组作为“ int”参数传递给函数,从而将地址转换为整数?

[英]Can passing an array of pointers to a function as an 'int' parameter convert the addresses to integers?

Beginner c++ programmer, writing code to solve Sudoku puzzles. 初学者c ++程序员,编写解决数独难题的代码。 I'll keep the background info short to avoid confusion. 为了避免混淆,我将简短地介绍背景信息。

I have an array of pointers, int *P[9] , I have assigned each entry a specific address. 我有一个指针数组int *P[9] ,我为每个条目分配了一个特定的地址。 I want to assign these addresses to another array of pointers, int *B[81] . 我想将这些地址分配给另一个指针数组int *B[81]

P[0] should correspond to B[0] , P[1] to B[8] , and so on. P[0]应对应于B[0]P[1]B[8] ,依此类推。

When I pass these to a function: 当我将这些传递给函数时:

void (int B[  ], int P[  ] ) {...}

it seems like the function is converting the address P[ ] is pointing to into an integer value. 似乎函数正在将指向的地址P[ ]转换为整数值。 Before the function is called P[0] points to the address 0x7fff978d46b0 , if I check the value of P[0] inside the function it's a number like `48782346 . 在调用该函数之前, P[0]指向地址0x7fff978d46b0 ,如果我检查函数内部的P[0]值,则该数字类似于`48782346。

#include<iostream>
using namespace std;

void assign_box(int matrix[], int P[])
{
    cout << "P[0] in function: " << P[0] << "\n";
    matrix[0]=P[0];
}

int main()
{
    int table[9][9];
    //Initialise table entries to 0
    for(int i=0; i<9; i++)
    {
        for(int j=0; j<9; j++)
        {
            table[i][j]=0;
        }
    }
    //Assign addresses to vector P, for brevity P is of length one
    int *P[1];
    P[0]=&table[0][0];

    cout<< "P[0] before function: " << P[0] << "\n";

    int*B[81];
    assign_box(B[81], P[9]);
}

If it did this and worked I wouldn't care, but unfortunately when I assign B[0] = P[0] , it hits me with a Segmentation fault (core dumped) , which makes me wonder is the function trying to assign the pointer B[0] to the address 48782346. 如果它做到了这一点,我不在乎,但是不幸的是,当我分配B[0] = P[0] ,它遇到了Segmentation fault (core dumped) ,这让我很奇怪,这让我怀疑是该函数试图分配指向地址48782346的指针B[0]

Is it possible for the function to convert an address into an integer value? 该函数是否可以将地址转换为整数值?

Apologies if my question is unclear or verbose, first time asker. 抱歉,如果我的问题不清楚或冗长,请首次提问。 And thank you for edits. 并感谢您的编辑。

If you dereference int*[] (or int** ), you get an int* . 如果取消引用int*[] (或int** ),则会得到一个int* If you dereference an int* , you get an int . 如果取消引用int* ,则会得到一个int This is exactly what you are doing, and why you end up with an int at the end. 这正是您正在做的事情,以及为什么最后会得到一个int

//main
int *P[1]; //Array of pointers to int
int *B[81]; //Array of pointer to int
assign_box(B[81], P[9]); //Pass in two pointers to int

//assign_box
matrix[0]=P[0]; //assign int to int

You probably meant to call assign_box like assign_box(B, P) , and have the signature be void assign_box(int *B[], int *P[]); 您可能打算像assign_box(B, P)那样调用assign_box ,并且使签名为void assign_box(int *B[], int *P[]); . This would then allow you to assign one pointer inside an array to another pointer inside an array. 然后,这将允许您将数组内的一个指针分配给数组内的另一个指针。

There are multiple things that could be causing segmentation faults, but they all stem from invalid array indices. 可能有多种原因导致分段错误,但它们均源自无效的数组索引。 If an array is declared like type identifier[size]; 如果声明的数组类似于type identifier[size]; , it has valid indices from 0 to size - 1 . ,它具有从0size - 1有效索引。 So, int *B[81]; 因此, int *B[81]; means B[81] is invalid. 表示B[81]无效。

You're passing in the wrong parameters. 您传递的参数错误。 You're trying to pass in an array object B[81] which does NOT EXIST. 您正在尝试传入不存在的数组对象B [81]。 You only have B[0] - B[80]. 您只有B [0]-B [80]。 Also, B[80] isn't an int pointer. 另外,B [80]不是int指针。 It's an int within an int array. 它是int数组中的int。 P[9] is a pointer to an array of integers. P [9]是指向整数数组的指针。 So, you're trying to pass an integer in an array slot that does not exist into a parameter that does not take integers -- it takes integer arrays. 因此,您尝试将不存在的数组插槽中的整数传递给不接受整数的参数-它接受整数数组。

#include<iostream>
using namespace std;

void assign_box(int matrix[], int P[])
{
    cout << "P[0] in function: " << P[0] << "\n";
    matrix[0]=P[0];
}

int main()
{
    int table[9][9];
    //Initialise table entries to 0
    for(int i=0; i<9; i++)
    {
        for(int j=0; j<9; j++)
        {
            table[i][j]=0;
        }
    }
    //Assign addresses to vector P, for brevity P is of length one
    int *P[1];
    P[0]=&table[0][0];

    cout<< "P[0] before function: " << P[0] << "\n";

    int*B[81];
    assign_box(B[81], P[9]); // WRONG

}

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

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