簡體   English   中英

理解C:指針和結構

[英]Understanding C: Pointers and Structs

我試圖更好地理解c,並且很難理解在哪里使用*和&字符。 和一般的結構。 這是一些代碼:

void word_not(lc3_word_t *R, lc3_word_t A) {
    int *ptr;
    *ptr = &R;
    &ptr[0] = 1;
    printf("this is R at spot 0: %d", ptr[0]);
}  

lc3_word_t是這樣定義的結構:

struct lc3_word_t__ {
  BIT b15;
  BIT b14;
  BIT b13;
  BIT b12;
  BIT b11;
  BIT b10;
  BIT b9;
  BIT b8;
  BIT b7;
  BIT b6;
  BIT b5;
  BIT b4;
  BIT b3;
  BIT b2;
  BIT b1;
  BIT b0;
};

該代碼不執行任何操作,但是可以編譯,但是一旦運行,就會出現“分段錯誤”錯誤。 我只是想了解如何讀寫結構以及如何使用指針。 謝謝 :)


新代碼:

void word_not(lc3_word_t *R, lc3_word_t A) {
    int* ptr;
    ptr = &R;
    ptr->b0 = 1;
    printf("this is: %d", ptr->b0);
}

這是指針的快速總結(至少在我使用它們的時候):

int i;
int* p; //I declare pointers with the asterisk next to the type, not the name;
        //it's not conventional, but int* seems like the full data type to me.

i = 17; //i now holds the value 17 (obviously)
p = &i; //p now holds the address of i (&x gives you the address of x)
*p = 3; //the thing pointed to by p (in our case, i) now holds the value 3
        //the *x operator is sort of the reverse of the &x operator
printf("%i\n", i); //this will print 3, cause we changed the value of i (via *p)

並與結構配對:

typedef struct
{
    unsigned char a;
    unsigned char r;
    unsigned char g;
    unsigned char b;
} Color;

Color c;
Color* p;

p = &c;     //just like the last code
p->g = 255; //set the 'g' member of the struct to 255
            //this works because the compiler knows that Color* p points to a Color
            //note that we don't use p[x] to get at the members - that's for arrays

最后,使用數組:

int a[] = {1, 2, 7, 4};
int* p;

p = a;    //note the lack of the & (address of) operator
          //we don't need it, as arrays behave like pointers internally
          //alternatively, "p = &a[0];" would have given the same result

p[2] = 3; //set that seven back to what it should be
          //note the lack of the * (dereference) operator
          //we don't need it, as the [] operator dereferences for us
          //alternatively, we could have used "*(p+2) = 3;"

希望這可以解決一些問題-如果我遺漏了任何內容,請隨時詢問更多詳細信息。 干杯!

我認為您正在尋找有關C的通用教程(其中有很多)。 只需檢查谷歌。 以下站點提供了很好的信息,可以更好地解釋您的問題。

http://www.cplusplus.com/doc/tutorial/pointers/
http://www.cplusplus.com/doc/tutorial/structures/

它們將幫助您掌握基本語法,並了解什么是運算符以及它們如何工作。 請注意,該站點是C ++,但基本知識在C中是相同的。

首先,第二行應該給您一些有關將指針轉換為int的警告。 我很驚訝的第三行可以編譯。 以最高警告級別進行編譯,並注意警告。

*根據聲明或表達式的不同而做不同的事情。 在聲明中(例如int *ptrlc3_word_t *R ),它僅表示“這是一個指針”。

在表達式中(例如*ptr = &R ),這意味着要取消引用指針,這基本上是像常規變量一樣使用指向的值。

&表示“使用此地址”。 如果某物不是指針,則可以使用它將其變成指針。 如果某個東西已經是指針(例如函數中的Rptr ),則無需再次獲取它的地址。

int *ptr;
*ptr = &R;

在此ptr不會初始化。 它可以指向任何東西。 然后用*取消引用它,並為它分配R的地址。 由於&R的類型為lc3_word_t** (指向指針的指針),而*ptr的類型為int ,因此不應編譯。

&ptr[0] = 1; 也不合法。 在這里,您取ptr[0]的地址並嘗試為其分配1。這也是非法的,因為它是一個右值,但是您可以認為它不能更改變量ptr[0]的位置,因為本質上來說,嘗試更改ptr[0]的地址。

讓我們逐步看一下代碼。

首先,您聲明一個指向int的指針: int *ptr 順便說一下,我喜歡這樣寫int* ptr (在int旁邊加*而不是ptr )來提醒自己指針是該類型的一部分,即ptr的類型是指向int指針。

接下來,將ptr指向的值分配給R的地址。 *取消引用指針(被指向的值), &給人的地址。 這是你的問題。 您混合了各種類型。 將R( lc3_word_t** )的地址分配給* ptr( int )將不起作用。

接下來是&ptr[0] = 1; 這也不是很有意義。 &ptr[0]是ptr第一個元素的地址(作為數組)。 我猜您只需要第一個地址的值,即ptr[0]*ptr

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM