## need a pair of eyes - pointer

7 posts / 0 new
Author
Message

Guys, I have two functions.  The first function, "retrieve_small_leaf1" return the pointer to the last value in a array of Nodes that is not 0.  Note, the array is sorted!

The Second function is supposed to return the second last value.  Because the list is sorted it will not be zero.

The functions are thus:

```static struct Node * retrieve_small_leaf1(struct Node * array){

for(int i = SubTrees; ((i != 0) && (array->value == 0)); i--, array--);

return array;
}

static struct Node * retrieve_small_leaf2(struct Node * array){

for(int i = SubTrees; ((i != 0) && (array->value == 0)); i--, array--);

printf("\n\rtest %c", array->marker);
array--;
printf("\n\rtest %c\n\r", array->marker);

return array;
}```

Function one returns correctly.

Function two however returns garbage.

I'm doing a quick pointer decrement in function two that should point to next value.

EDITED, I'm passing the end pointer to the array, i'm trying to find the next non zero value in the array.  For example, the array could be, 12, 10, 5, 4, 4, 1, 0, 0, 0.

function one should return 1.

function two should return 4.

This topic has a solution.
Last Edited: Wed. Nov 27, 2019 - 07:38 PM

run the following test:

```static struct Node * retrieve_small_leaf2(struct Node * array){

for(int i = SubTrees; ((i != 0) && (array->value == 0)); i--, array--);

printf("\n\rtest %p", array);
array--;
printf("\n\rtest %p\n\r", array);

return array;
}```

and I got the resut: The structure of Node is thus:

struct Node{

unsigned char type;

unsigned int  value;

unsigned char marker;

struct Node * left;

struct Node * right;

};

So it decremented 14Hex and not 14 Decimal.  Why?

You can use an online compiler-

https://godbolt.org/z/yP46XJ

https://godbolt.org/z/pSrRXW

Then everyone can play along.

Last Edited: Wed. Nov 27, 2019 - 07:17 PM

Once again, your program worked for me. This is in Visual Studio; it prints:

test 5
test 4

```#include "stdafx.h"

struct Node {
unsigned char type;
unsigned int  value;
unsigned char marker;
struct Node * left;
struct Node * right;
};

struct Node NodeArray[] = {
{ 0, 12, '0', NULL, NULL },
{ 0, 10, '1', NULL, NULL },
{ 0, 5,  '2', NULL, NULL },
{ 0, 4,  '3', NULL, NULL },
{ 0, 4,  '4', NULL, NULL },
{ 0, 1,  '5', NULL, NULL },
{ 0, 0,  '6', NULL, NULL },
{ 0, 0,  '7', NULL, NULL },
};

const int SubTrees = sizeof NodeArray / sizeof(*NodeArray);

static struct Node * retrieve_small_leaf1(struct Node * array)
{

for (int i = SubTrees; ((i != 0) && (array->value == 0)); i--, array--);

return array;
}

static struct Node * retrieve_small_leaf2(struct Node * array)
{

for (int i = SubTrees; ((i != 0) && (array->value == 0)); i--, array--);

printf("\n\rtest %c", array->marker);
array--;
printf("\n\rtest %c\n\r", array->marker);

return array;
}

int main(int argc, char* argv[])
{
struct Node *ar = retrieve_small_leaf1(&NodeArray[SubTrees - 1]);
retrieve_small_leaf2(ar);

return 0;
}
```

Fianawarrior wrote:

So it decremented 14Hex and not 14 Decimal.  Why?

Alignment probably eg. the int and pointers are aligned to 4 byte boundary.

Try it as

struct Node{
unsigned char type;

unsigned char marker;

unsigned int  value;
struct Node * left;
struct Node * right;
};

see if it makes any difference

(I would just print out  sizeof(struct Node))

This reply has been marked as the solution.