3

I'm newer in C/C++, I really confuse about a function that executes complex operation. I just want to know how it works ? What does function mean ?

void writetoXX(unsigned int **src, unsigned int reg_offset, unsigned int reg_data){
    *(*src)++ = reg_offset;
    *(*src)++ = reg_data;
}

Any idea ??

Update my understanding:

void writetoXX(unsigned int **src, unsigned int reg_offset, unsigned int reg_data) {
    *(*src)++ = reg_offset;
    *(*src)++ = reg_data;
}

int main() {
    int a[10] = { 0 };

    int *p = a;
    printf("Before !!\n");
    for(int i=0;i<10;i++)
    printf("%d ", a[i]);
    writetoXX(&p, 20, 30);
    printf("After !!! \n");
    for (int i = 0; i<10; i++)
        printf("%d ", a[i]);

    getchar();
    return 0;
}

After run this program, I see that the function writetoXX change value arr[0][0] and arr[0][1] to 20 and 30. It means that the function is used for write data to 2 consecutive blocks.

And output is below:

Before !!
0 0 0 0 0 0 0 0 0 0
After !!
20 30 0 0 0 0 0 0 0 0

DreamInBox
  • 174
  • 2
  • 12
  • There must be a clearer way to write this code that is just as performant. – Steve Apr 04 '19 at 04:36
  • in c++ (as the question is tagged c++) there is: just declare `*c` as a parameter by reference, as in `writetoXX(unsigned int *&pointer,...)` and then write `*(pointer)++ = reg_offset;`. – Luis Colorado Apr 04 '19 at 15:46

2 Answers2

3

src is a pointer to a pointer to an unsigned int, for example this is used for bidimensional arrays. Because of the operator precedence, the routine does:

  • take *src, it is a pointer to an unsigned int
  • (*src)++ increments the pointer, such that it points to the next unsigned int. It is a post increment, so it returns the value before the increment.
  • *(*src)++ is the unsigned int pointed by *src before the increment ++

So in summary, you take * src which is a pointer to unsigned int, you assign the first pointed data to reg_offset, then you increment such pointer, then you assign the next element to reg_data, and finally you increment again the pointer *src.

francesco
  • 7,189
  • 7
  • 22
  • 49
2

Double pointer is a variable which stores address of another pointer variable.

Consider this *(*src)++ = reg_offset;

In one line: The value of reg_offset is stored at current address stored in src and the address in src is incremented.

You can consider this happening in the following order. 1) As you know src is a variable which is storing address of another pointer or memory space.

2) So (*src) whill give address of the pointed variable or memory space.

3) Due to operator precedence, (*src)++ takes place. Which you can read like, after executing the current line, increment the address value stored in src.

4) Now *(*src) Which is the memory space with address stored in (*src). So *(*src) = reg_offset; will copy the value of reg_offset to that address. I have removed ++ in this line just for clarity.

5) When the second line *(*src)++ = reg_data; is about to be executed, the (*src) will be pointing to next address. So reg_data is written to next address.

Please note, next address does not mean current_address+1. It actually means current_address+sizeof(data type). Ifunsigned int is 4 byte in your compiler, then next_address = current_address+4

You can print content of (*src) in between the lines and verify.

MayurK
  • 1,925
  • 14
  • 27