简体   繁体   English

更改指针地址

[英]Change the address of a pointer

I have an Array of integers called bufferA[] and a pointer *ptr which points to the first integer in this Array bufferA[0].我有一个名为 bufferA[] 的整数数组和一个指针 *ptr,它指向这个数组 bufferA[0] 中的第一个整数。 Now, i would like to Change the pointer to point to the second value bufferA[1].现在,我想将指针更改为指向第二个值 bufferA[1]。 As i debbug the Code i can see that the address of the first integer in this Array is 0x1702 and now i would like to Change the pointer so it Points to 0x1704 which is the address of bufferA[1].在调试代码时,我可以看到该数组中第一个整数的地址是 0x1702,现在我想更改指针,使其指向 0x1704,这是 bufferA[1] 的地址。 There is probably some way to make this without the pointer and just read the values of the Array, but this Array is passed from the ADC-module to the DMA-module and instead of just taking the Arrays (which makes storing them in the DMA useless) i would like to just take the address of the first value and change it up to read the following values.可能有一些方法可以在没有指针的情况下只读取数组的值,但是这个数组是从 ADC 模块传递到 DMA 模块而不是仅仅获取数组(这使得将它们存储在 DMA 中)没用)我只想获取第一个值的地址并将其更改为读取以下值。 i hope this somehow explains my Problem...我希望这能以某种方式解释我的问题......

How about ptr = ptr + 1; ptr = ptr + 1;怎么样ptr = ptr + 1; ? ? (Or equivalently in this context ptr += 1; or ptr++; or ++ptr; ?) The C compiler knows how many bytes are taken up by the item that ptr points to, because of its declared type, and will automatically increment the pointer by the right number of bytes. (或者在这个上下文中等价于ptr += 1;ptr++;++ptr; ?)C 编译器知道ptr指向的项占用了多少字节,因为它声明了类型,并且会自动增加正确字节数的指针。

Arrays in C are always passed by reference, there is no copy operation of you pass it as an argument to a function! C 中的数组总是通过引用传递,没有将它作为参数传递给函数的复制操作! (This is a first gotcha of beginner C programmers) (这是初学者 C 程序员的第一个陷阱)

Read something about pointers in C, first, but in short:首先阅读有关 C 中指针的内容,但简而言之:

int a = 5;    // declares a integer-type of value 5
int* pta;     // declares a pointer-to-integer type
pta = &a      // assigns the **address** of a to pta (e.g. 0x01)
int b = *pta  // assingns the **value** of the address location that pta is pointing to into b
              // (i.e. the contents of memory address 0x01, which is 5)
*pta = 4      // assigns 4 to the content of the memory address that pta is pointing to.

// now b is 5, because we assigned the value of the address that pta was pointing to
// and a is 4, because we changed the value of the address that pta was pointing to
// - which was the address where variable a was stored

The variable bufferA is itself a pointer to the address of the first element.变量bufferA本身就是一个指向第一个元素地址的指针。 bufferA + 1 gives you the address of the second element, because arrays are stored in memory sequentially. bufferA + 1为您提供第二个元素的地址,因为数组按顺序存储在内存中。

Or if you want a more readable format:或者,如果您想要更易读的格式:

&bufferA[0]  is the same as  bufferA         (address of the first element(e.g 0x11) 
&bufferA[1]  is the same as  bufferA + 1     (address of the second element(e.g 0x12) 
buffer[2]    is the same as  *(buffer + 2)   (value at the address of the third element)

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

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