简体   繁体   English

基本的C#语法问题

[英]Basic C# Syntax Questions

class f2011fq1d
{
    unsafe public static void Main()
    {
       int a = 2;
       int b = 4;
       int* p;
       int* q;
       int[] ia = { 11, 12, 13 };
       p = &a; q = &b;
       Console.WriteLine(*p + a);
       Console.WriteLine(*q / *p);
       Console.WriteLine(*&a + *&b * 2);

       *p = a + *q;
       Console.WriteLine(a + *q);
       fixed (int* r = ia)
       {
           Console.WriteLine(*r + 3);
       }
    } 
}

In this code, I'm confused about some of the syntax. 在这段代码中,我对一些语法感到困惑。 For example, what does int* p , and p = &a do? 例如, int* pp = &a做什么的? And the last part with the fixed (int* r = ia) , what does that do? fixed (int* r = ia)的最后一部分fixed (int* r = ia) ,那又做什么?

In the source code, it also prints a few values, can someone explain what is being printed? 在源代码中,它还会打印一些值,有人可以解释正在打印的内容吗?

You're using pointers and the 'address of operator' these are features commonly associated with C/C++ but are available in C# code within unsafe code blocks. 您正在使用指针和“运算符地址”这些通常与C / C ++相关的功能,但在unsafe代码块中的C#代码中可用。 I would recommend doing some general research on the concept because understanding the syntax alone will not be enough to effectively use them. 我建议对这个概念进行一些一般性的研究,因为单独理解语法不足以有效地使用它们。 I'll explain a few lines; 我会解释几行;

 int * p; //a pointer (the address of) 4 bytes of memory for an int
 int a = 5; // normal allocation/initialization of int
 p = &a; //sets p  to the address of a. Since p is a pointer, it holds an address
 // ampersand is the 'address of operator', it returns and address. so this assignment works


 p = a // will not compile. int* != int

 p == 5 // will not compile, int* != int

 *p == 5 // true. *pointer dereferences the pointer returning the value
 // found at the address p points to which is 5 so this is true.

 int * q = &a; // another pointer to the memory containing a

 p == q // true, both pointers contain the same value, some address which is usually displayed in hex like 0xFF110AB2

A more common use of pointers would be something like; 指针的更常见用法是类似的;

int *p;
// do something to get a length for an integer array at runtime
p = new int[size]; 
// now I've allocated an array based on information found at run time!

The above operations are very common in C/C++ and completely necessary. 上述操作在C / C ++中非常常见,完全是必要的。 In C# there's no point in doing this. 在C#中,没有必要这样做。 It's being done for you under the covers already, and it will never make mistakes (like failing to free that memory I just allocated). 它已经在幕后为你完成,它永远不会出错(比如没有释放我刚刚分配的内存)。

int* p declares a variable named p that is a pointer to an int. int* p声明一个名为p的变量,它是一个指向int的指针。

p = &a causes p to point to the same location in memory as a . p = &a使p指向内存中与a相同的位置。

Pointer types (C# Programming Guide) is probably a good place to start if you need to understand this kind of stuff. 如果你需要了解这种东西, 指针类型(C#编程指南)可能是一个很好的起点。

With unsafe code, C# allows you to use pointers and addresses such like C. int *p declares a pointer to an int . 对于unsafe代码,C#允许您使用指针和地址,例如C. int *p声明指向int的指针。 p = &a , takes the address of a and stores it in p . p = &a ,利用了地址a并将其存储在p

fixed prevents the garbage collector from moving the data. fixed可防止垃圾收集器移动数据。

See http://msdn.microsoft.com/en-us/library/chfa2zb8(v=vs.100).aspx and http://msdn.microsoft.com/en-us/library/f58wzh21(v=vs.100).aspx 请参阅http://msdn.microsoft.com/en-us/library/chfa2zb8(v=vs.100).aspxhttp://msdn.microsoft.com/en-us/library/f58wzh21(v=vs。 100)的.aspx

In this code, I'm confused about some of the syntax. 在这段代码中,我对一些语法感到困惑。 For example... 例如...

I'll try and address these one at a time ( no pun intended ) 我会尝试一次解决这些问题(没有双关语)

what does int* p int * p是什么

This declares a local variable called 'p' who's type is a pointer to an int. 这声明了一个名为'p'的局部变量,其类型是指向int的指针。 Thus when this variable is assigned to a memory address it will read 4-bytes as if they were an int32. 因此,当将此变量分配给内存地址时,它将读取4个字节,就像它们是int32一样。

... and p = &a ......和p =&a

The '&a' on the right is read as "the address of a", meaning take the memory location we assigned to the local variable 'a'. 右边的'&a'被读作“a的地址”,意思是将我们分配给的内存位置放在局部变量'a'上。 Assigning this to the already declared int pointer, 'p', we can then read 'p' to get the value actually stored in the variable 'a'. 将它分配给已经声明的int指针'p',然后我们可以读'p'来获得实际存储在变量'a'中的值。

And the last part with the fixed (int* r = ia), what does that do? 而固定的最后一部分(int * r = ia),那又做什么?

This is very similar to the assignment of p to the address of a except there is not an "the address of" prefix. 这非常类似于将p分配给a的地址,除了没有“地址”前缀。 This is because the type of the variable 'ia' is an array which is being treated implicitly as if it were a pointer. 这是因为变量'ia'的类型是一个被隐式处理的数组,就像它是一个指针一样。 Thus this statement declares an int pointer 'r' and assigns it to the address of the first item in the array 'ia'. 因此,此语句声明一个int指针'r'并将其分配给数组'ia'中第一个项的地址。

Here are a few more notes: 以下是一些注意事项:

if (*p == 2) ...

A pointer prefixed with the '*' will dereference that pointer and be treated as if it were a variable of the type of pointer. 带有前缀'*'的指针将取消引用该指针,并将其视为指针类型的变量。 Thus in our example dereferencing 'p' who points to the variable 'a' will result in the value stored in 'a', 2. 因此,在我们的示例中,取消引用指向变量'a'的'p'将导致存储在'a'中的值,2。

*p = 100;

Just like reading a value at the address pointed to by 'p', we can also write a new value. 就像在'p'指向的地址读取值一样,我们也可以写一个新值。 Simply dereference the pointer and assign it a new value. 只需取消引用指针并为其指定一个新值。

*&a

This is just intended to confuse someone. 这只是为了让某些人感到困惑。 As explained it takes the address of 'a' by prefixing '&', but then dereferences the address with the '*' prefix. 如上所述,它通过前缀“&”获取“a”的地址,但随后使用“*”前缀取消引用地址。 This simply evaluates to 'a' and both prefixes can be omitted without a change in behavior. 这简单地评估为'a',并且可以省略两个前缀而不改变行为。

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

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