理解C:指针和结构 [英] Understanding C: Pointers and Structs
问题描述
我想更好地了解c和我有我的地方使用*和&放一个很难理解;字符。而刚刚结构体的一般。这里是一个有点code的:
无效word_not(lc3_word_t * R,lc3_word_t A){
INT * PTR;
* PTR =再现性;
&放大器; ptr的[0] = 1;
的printf(这为R在现货0:%D,PTR [0]);
}
lc3_word_t是这样定义的结构:
结构lc3_word_t__ {
BIT B15;
BIT B14;
BIT B13;
BIT B12;
BIT B11;
BIT B10;
BIT B9;
B8比特;
B7位;
BIT B6;
BIT B5;
BIT B4;
BIT B3;
BIT B2;
BIT B1;
b0位;
};
这code没有做任何事情,它编译但一旦我运行它,我得到一个分段故障错误。我只是想了解如何读取和写入一个结构,并使用指针。感谢:)
新的code:
无效word_not(lc3_word_t * R,lc3_word_t A){
INT * PTR;
PTR =再现性;
ptr-> B0 = 1;
的printf(这是数:%d,&ptr- GT; B0);
}
下面是指针的快速纲要(我使用它们,至少):
INT I;
INT * P; //我宣布指针带星号旁边的类型,而不是名称;
//它不是传统的,但为int *似乎是完整的数据类型给我。我= 17; //我现在持有的价值17(显然)
P =&放;我; // P中就拥有我的地址(安培; X为您提供了x的地址)
* P = 3; //东西指向P(在本例中,我)现在持有价值3
//将* x操作符是排序和放大器的反向; x操作符
的printf(%I \\ N,I); //会打印出3,因为我们改变了我的价值(通过* P)
和与结构配对:
typedef结构
{
unsigned char型一;
unsigned char型 - [R;
unsigned char型克;
unsigned char型B:
}颜色;颜色为c;
彩色* P;P =和C; //就像上次一样code
P-> G = 255; //设置结构255的G成员
//这个工作,因为编译器知道颜色* p指向一个颜色
//请注意,我们没有使用P [X]来获得的成员 - 这就是数组
最后,用数组:
int类型的[] = {1,2,7,4};
INT * P;P = A; //注意缺乏和放大器; (地址)算
//我们并不需要它,因为数组表现得像内部指针
//替换地,P =&放大器;一个[0];将给予相同的结果P [2] = 3; //设置有七回它应该是什么
//注意缺少*(取消引用)运算符
//我们并不需要它,因为[]运营商废除的我们
//或者,我们也可以使用*(P + 2)= 3;
希望这清除了一些事情 - 不要犹豫,要求更多的细节,如果有什么事,我已经离开了。干杯!
I'm trying to better understand c, and I'm having a hard time understanding where I use the * and & characters. And just struct's in general. Here's a bit of code:
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 is a struct defined like this:
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;
};
This code doesn't do anything, it compiles but once I run it I get a "Segmentation fault" error. I'm just trying to understand how to read and write to a struct and using pointers. Thanks :)
New Code:
void word_not(lc3_word_t *R, lc3_word_t A) {
int* ptr;
ptr = &R;
ptr->b0 = 1;
printf("this is: %d", ptr->b0);
}
Here's a quick rundown of pointers (as I use them, at least):
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)
And paired with structs:
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
And finally, with 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;"
Hope this clears some things up - and don't hesitate to ask for more details if there's anything I've left out. Cheers!
这篇关于理解C:指针和结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!