传递指针/引用结构到功能 [英] Passing pointers/references to structs into functions

查看:109
本文介绍了传递指针/引用结构到功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是要听起来像一个愚蠢的问题,但我仍然在学习C,所以请多多包涵。 :)

This is going to sound like a silly question, but I'm still learning C, so please bear with me. :)

我正在K&安培的第6章; R(结构),因此到目前为止,通过这本书已经看到了巨大的成功。我决定用巨资结构pretty工作,并因此与点和RECT例子本章前面做了很多的工作。其中一个我想尝试正在改变 canonrect 功能(第2版,第131页)通过指针工作,因此返回的内容无效

I'm working on chapter 6 of K&R (structs), and thus far through the book have seen great success. I decided to work with structs pretty heavily, and therefore did a lot of work early in the chapter with the point and rect examples. One of the things I wanted to try was changing the canonrect function (2nd Edition, p 131) work via pointers, and hence return void.

我有这个工作,但遇到了一个嗝,我希望你们能帮助我。我想 canonRect 以创建一个临时的矩形对象,执行其更改,然后重新分配它传递给临时长方形指针,从而简化了code。

I have this working, but ran into a hiccup I was hoping you guys could help me out with. I wanted canonRect to create a temporary rectangle object, perform its changes, then reassign the pointer it's passed to the temporary rectangle, thus simplifying the code.

但是,如果我这样做,RECT不会改变。相反,我发现自己手动重新填充RECT我通过了,这不工作的领域。

However, if I do that, the rect doesn't change. Instead, I find myself manually repopulating the fields of the rect I'm passed in, which does work.

在code如下:

#include <stdio.h>

#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))

struct point {
    int x;
    int y;
};

struct rect {
    struct point lowerLeft;
    struct point upperRight;
};

// canonicalize coordinates of rectangle
void canonRect(struct rect *r);

int main(void) {
    struct point p1, p2;
    struct rect r;

    p1.x = 10;
    p1.y = 10;
    p2.x = 20;
    p2.y = 40;
    r.lowerLeft = p2; // note that I'm inverting my points intentionally
    r.upperRight = p1;

    printf("Rectangle, lower left: %d, %d; upper right: %d %d\n\n", 
        r.lowerLeft.x, r.lowerLeft.y, r.upperRight.x, r.upperRight.y);

    // can't pass a pointer, only a reference. 
    // (Passing pointers results in illegal indirection compile time errors)
    canonRect(&r); 
    printf("Rectangle, lower left: %d, %d; upper right: %d %d\n\n", 
        r.lowerLeft.x, r.lowerLeft.y, r.upperRight.x, r.upperRight.y);    
}

void canonRect(struct rect *r) {
    struct rect temp;
    temp.lowerLeft.x = min(r->lowerLeft.x, r->upperRight.x);
    temp.lowerLeft.y = min(r->lowerLeft.y, r->upperRight.y);
    temp.upperRight.x = max(r->lowerLeft.x, r->upperRight.x);
    temp.upperRight.y = max(r->lowerLeft.y, r->upperRight.y);

    r = &temp; // doesn't work; my passed-in rect remains the same

    // I wind up doing the following instead, to reassign all 
    // the members of my passed-in rect
    //r->lowerLeft = temp.lowerLeft;
    //r->upperRight = temp.upperRight;
}

所以这里的问题:

So here are the questions:


  1. 为什么 R =&放大器;温度; 不工作? (我觉得这是因为我传递一个参考,而不是一个指针;我在想,引用不可修改,但指针是正确的)

  2. 为什么我可能会得到一个非法的间接编译时错误,如果我尝试在一个指针传递到 canonRect ? (IE,如果我有 canonRect(* R); 的main()

  1. Why does r = &temp; not work? (I think this is because I pass in a reference instead of a pointer; am I correct in thinking that references are not modifiable but pointers are?)
  2. Why might I get an illegal indirection compile-time error if I try to pass in a pointer to canonRect? (IE, if I had canonRect(*r); in main().)

我怀疑我已经知道答案#1,#但是2困扰我 - 我认为这是合法的周围传递指针

I suspect I already know the answer to #1, but #2 perplexes me -- I thought it was legal to pass pointers around.

反正......请原谅的C福利局。

Anyway ... please forgive the C newb.

推荐答案

我想你想做的事是这样的:

void canonRect(struct rect *r) {
    struct rect temp;
    temp.lowerLeft.x = min(r->lowerLeft.x, r->upperRight.x);
    temp.lowerLeft.y = min(r->lowerLeft.y, r->upperRight.y);
    temp.upperRight.x = max(r->lowerLeft.x, r->upperRight.x);
    temp.upperRight.y = max(r->lowerLeft.y, r->upperRight.y);

    *r = temp; 
}

在上面的code你设置* R它的类型是RECT来临时它的类型是矩形的。

In the above code you are setting *r which is of type rect to temp which is of type rect.

回复1:如果你想改变什么r被指向你需要使用一个指针的指针。如果这真的是你想要的东西(见上面,它是不是真的你想要的),那么你就必须确保它指向堆东西。如果你点它不是新或malloc的创建内容,然后它会掉下来的范围,你会被指向一个不再使用该变量的内存。

Re 1: If you want to change what r is pointing to you need to use a pointer to a pointer. If that's really what you want (see above, it is not really what you want) then you'd have to make sure to point it to something on the heap. If you point it to something not created with 'new' or malloc then it will fall out of scope and you will be pointing to memory that is no longer used for that variable.

为什么不与你的code工作R =&放大器;温度

由于r是RECT *类型。这意味着,r是持有一个内存地址是谁的记忆中包含一个矩形的变量。如果你改变什么r为指向,这很好,但变量传递不会改变。

Because r is of rect* type. That means that r is a variable that holds a memory address who's memory contains a rect. If you change what r is pointing to, that's fine but that doesn't change the passed in variable.

2回复:*当一个类型声明不使用的是提领一元操作符。这意味着它将查找什么是指针的地址内侧。所以,通过传递* R你是不是传递指针都没有。在由于R面对的不是一个指针,这是无效的语法。

Re 2: * when not used in a type declaration is the dereference unary operator. This means that it will lookup what is inside the address of the pointer. So by passing *r you are not passing a pointer at all. In face since r is not a pointer, that is invalid syntax.

这篇关于传递指针/引用结构到功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆