-3

Can some one explain me the logic with respect to the value of the variable b?

#include <stdio.h>

unsigned char buffer[4] = {1,2,3,4};   /* Declare the buffer */

unsigned char* u8GetHeader(void)
{
    static int count = 0;
    count++;              /* incrementing the value of count for every invoke */
    if(1 == count)
      return &buffer[0];
    else
      return &buffer[1];
}

int main()
{
     unsigned int a , b;
     a = (u8GetHeader())[0];
     b = (u8GetHeader())[1];
     printf("The value of variable a = %d\n",a);
     printf("The value of variable b = %d\n",b);
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
Maddy
  • 503
  • 4
  • 12
  • 21
  • I am getting the value of b as 3 while i was expecting the value as 2 – Maddy Aug 04 '20 at 11:24
  • 2
    How is this different from [your previous question](https://stackoverflow.com/questions/63245419/regarding-the-return-of-a-function-using-array-subscript)? – Some programmer dude Aug 04 '20 at 11:27
  • 1
    You are returning a pointer to a local which has gone out of scope, and when you try to de-reference it you get undefined behavoir – Mike Vine Aug 04 '20 at 11:27
  • 1
    Why were you expecting the value as 2? – Igor G Aug 04 '20 at 11:27
  • 1
    And actually you made the code even worse, since now it's neither valid C *nor* valid C++. – Some programmer dude Aug 04 '20 at 11:27
  • Are you asking about C or C++ ? – Jabberwocky Aug 04 '20 at 11:29
  • BTW this code looks pretty weird. What are you trying to achieve? – Jabberwocky Aug 04 '20 at 11:30
  • Hello All, Lets assume that the buffer is global . Can you please let me know why is the value of b coming as 3 instead of 2 – Maddy Aug 04 '20 at 11:31
  • 1
    As I said in a comment to your previous question (never post the same question multiple times!) use pen and paper to draw out the array as a series of squares. Then use arrows to draw the pointers to it. Then new arrows for the indexing in the `main` function. – Some programmer dude Aug 04 '20 at 11:32
  • IMO you really need to spend some time to refresh [the help pages](http://stackoverflow.com/help), ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also take the [tour] and read about [ask] good questions and [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Aug 04 '20 at 11:33
  • @Jabberwocky, I need to decode a logic in my stack . How does (u8GetHeader())[1]; return 3? – Maddy Aug 04 '20 at 11:34
  • @Someprogrammerdude, Thanks for the information . Can you please re-check the question which i have edited . – Maddy Aug 04 '20 at 11:35

3 Answers3

3

This program is a good candidate for working out on paper first and then stepping through the program to see if it matches your expectations.

Let us assume buffer is a global at address 0x1000.

The first call to u8GetHeader returns the address of buffer[0], which is just buffer or 0x1000. You then index it with 0, so you access the number at 0x1000 which is 1.

The second call returns the address of buffer[1], or 0x1001. You index it with 1, so you access the number at 0x1002, which is 3.

Botje
  • 26,269
  • 3
  • 31
  • 41
2

Why b gives you 3: Since count is static it will increment on each call. With b = (u8GetHeader())[1]; you call the function a second time so &buffer[1] is returned which is a pointer to {2,3,4} and its second value is 3.

RomCoo
  • 1,868
  • 2
  • 23
  • 36
2

Lets modify your program a little:

int main()
{
    unsigned int a , b;
    unsigned int *x, *y;

    x = u8GetHeader();
    y = u8GetHeader();
    a = x[0];
    b = y[1];
    printf("The value of variable a = %d\n",a);
    printf("The value of variable b = %d\n",b);
}

The first time the u8GetHeader function is called it returns a pointer to the first element of the array. This gets assigned to the x variable.

Each time after the first call, the u8GetHeader function will return a pointer to the second element. This gets assigned to the y variable.

Now if we "draw" the array and the pointers to the different elements in it it could look like this:

+---+---+---+---+
| 1 | 2 | 3 | 4 |
+---+---+---+---+
^   ^
|   |
x   y

It should be pretty clear that y is pointing to the value 2. That is y[0] == 2. From this it should also be quite clear that adding 1 to that index (i.e. y[1]) will then get the second element from y, which is the value 3. So y[1] == 3.


Perhaps it is also useful to know that for any array of pointer p and index i, the expression p[i] is exactly equal to *(p + i).

From this we get that y[1] then must be *(y + 1)., and if we add an arrow for y + 1 in the drawing we get:

+---+---+---+---+
| 1 | 2 | 3 | 4 |
+---+---+---+---+
^   ^   ^
|   |   |
x   y   y+1
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621