在函数中使用指针和结构

时间:2022-06-26 11:45:48

I've just started looking into C++, and I'm can't seem to wrap my head around a couple of concepts.

我刚刚开始研究C ++,而我似乎无法理解几个概念。

Context

Struct:

typedef struct {
    int left;
    int right;
    int mid;
} boundtype;

Function:

boundtype sortintarray(int *curArray[], boundtype bounds){
    int left(bounds.left);
    int right(bounds.right);
    unsigned int refValue(*curArray[(left+right)/2]);

    while (left < right){
        if (*curArray[left] > refValue){
            if (*curArray[right] < refValue){
                int swap(*curArray[left]);
                *curArray[left] = *curArray[right];
                *curArray[right] = left;
                right--;
                left++;
            } else{right--;}
        } else{left++;}
    }
    bounds.left = left;
    bounds.right = right;
    bounds.mid = (left + right)/2;

    return bounds;
};

Function Call:

unsigned int pancakes[10]; //Filled via std::cin
int size(sizeof(pancakes)/sizeof(int));
boundtype bounds(0,size - 1);
bounds = sortintarray(&pancakes, bounds);

My Questions

1)

When I define a struct, is it possible to do something like:

当我定义一个结构时,是否可以执行以下操作:

typedef struct{
   int a;
   int b;
   int c = a/b;
}

and expect c to be constantly updated whenever a and b are updated?

并且希望在a和b更新时不断更新c?

2)

Is my way of passing in a struct/ returning a struct/ defining the function as so correct?

我的方式是传递结构/返回结构/定义函数是否正确?

3)

I understand that * references the value of an address, whereas & references the address of a value. I'm confused about how I use them in functions. How do I pass in the value, how do I define it in the function input, and how do I reference the value within the function?

我理解*引用地址的值,而&引用值的地址。我对如何在函数中使用它感到困惑。如何传入值,如何在函数输入中定义它,以及如何在函数中引用值?

My whole goal with this particular example is to edit the original array without making copies in the function.

这个特定示例的整个目标是编辑原始数组而不在函数中创建副本。

Any advice/help will be greatly appreciated!

1 个解决方案

#1


1  

1) No. But your code is C not C++. The C++ way to do what you want is

1)不。但你的代码是C而不是C ++。 C ++做你想做的事的方式是

struct boundtype
{
    int a;
    int b;
    int get_c() const { return a/b; }
};

2) Yes

3) To sort the array in place you only need a pointer

3)要对数组进行排序,只需要一个指针

boundtype sortintarray(int *curArray, boundtype bounds){
    int left(bounds.left);
    int right(bounds.right);
    unsigned int refValue(curArray[(left+right)/2]);

    while (left < right){
        if (curArray[left] > refValue){
            if (curArray[right] < refValue){

etc.

A pointer to the first element of an array is sufficient to access the whole array.

指向数组第一个元素的指针足以访问整个数组。

#1


1  

1) No. But your code is C not C++. The C++ way to do what you want is

1)不。但你的代码是C而不是C ++。 C ++做你想做的事的方式是

struct boundtype
{
    int a;
    int b;
    int get_c() const { return a/b; }
};

2) Yes

3) To sort the array in place you only need a pointer

3)要对数组进行排序,只需要一个指针

boundtype sortintarray(int *curArray, boundtype bounds){
    int left(bounds.left);
    int right(bounds.right);
    unsigned int refValue(curArray[(left+right)/2]);

    while (left < right){
        if (curArray[left] > refValue){
            if (curArray[right] < refValue){

etc.

A pointer to the first element of an array is sufficient to access the whole array.

指向数组第一个元素的指针足以访问整个数组。