I have an Array of integers called bufferA[] and a pointer *ptr which points to the first integer in this Array bufferA[0]. Now, i would like to Change the pointer to point to the second value 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]. 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. i hope this somehow explains my Problem...
Asked
Active
Viewed 226 times
0
-
so do you want the memory address of the first element of the bufferA [] array? and which language you need? – SwissCodeMen Mar 19 '20 at 21:52
-
I'm writing in c, I would like to know how to set the address of a pointer basically, something like *ptr=&(0x1702) so it points to the first element and then somehow change the address *ptr=&(0x1702 + 2) so it points to the second element – lofgero Mar 20 '20 at 10:21
-
look at this question [Where are addresses of pointers stored in C?](https://stackoverflow.com/questions/25198834/where-are-addresses-of-pointers-stored-in-c?rq=1) – SwissCodeMen Mar 20 '20 at 17:59
-
You're thinking too hard. `ptr = &bufferA[0];`, `ptr = &bufferA[1];`, ... – Raymond Chen Mar 21 '20 at 14:58
-
You could just do `ptr++;` – QuantumDeveloper Mar 21 '20 at 14:59
2 Answers
0
How about 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.

cooperised
- 2,404
- 1
- 15
- 18
0
Arrays in C are always passed by reference, there is no copy operation of you pass it as an argument to a function! (This is a first gotcha of beginner C programmers)
Read something about pointers in C, first, but in short:
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 + 1
gives you the address of the second element, because arrays are stored in memory sequentially.
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)

Papooch
- 1,372
- 11
- 17