0

This is the function prototype I have:

UINT8_t CTargetList::GetListOfTgts( CHAR_t Callsign[cTGT_MAX_CALLSIGN_CHAR],
   sTarget_t* ListOfTgtForCallsign[bNO_OF_TARGETS] );

Now when I create the structure to pass to this function

sTarget_t* TgtList[bNO_OF_TARGETS];
UINT8_t bRetTgts = list1.GetListOfTgts( "C1",&TgtList[bNO_OF_TARGETS]);

The structure is created correctly but when I trace the function, ListOfTgtForCallsign must be the array of pointers. Instead, ListOfTgtForCallsign only contains one pointer instead of bNO_OF_TARGETS. I want the list of pointers TgtList to be passed correctly as the ListOfTgtForCallsign parameter. What am I doing wrong?

user2633954
  • 93
  • 1
  • 9

3 Answers3

2

First of all, TgtList is an array of uninitialized pointers. Second, you are passing the address of the one beyond the last element of the array.

This is how you should call it.

UINT8_t bRetTgts = list1.GetListOfTgts( "C1", TgtList);
Marius Bancila
  • 16,053
  • 9
  • 49
  • 91
1

in c++ arrays are kinda pointers. You are passing a pointer to one after last element (bNO_OF_TARGETS) witch can also be used as array (that's why it passes compilation). guess you need simply:

sTarget_t* TgtList[bNO_OF_TARGETS];
UINT8_t bRetTgts = list1.GetListOfTgts( "C1", TgtList);
Community
  • 1
  • 1
fsw
  • 3,595
  • 3
  • 20
  • 34
  • 1
    the last element of the array is at index `bNO_OF_TARGETS-1`. what he is passing is the first element beyond the last – Marius Bancila Sep 18 '13 at 07:44
  • Thank you for the response but unfortunately this does not solve my problem. Logically your answer make sense and it should work. I can't understand why it didn't. Does the bNO_OF_TARGETS in the parameter of the function not have some influence regarding this matter? – user2633954 Sep 18 '13 at 07:48
  • Wouldn't it just be easier to pass the array by a reference? – user2633954 Sep 18 '13 at 07:57
  • array is a pointer to its first element. so if you do it like in my snippet you are passing it as reference. @MariusBancila is right you probably need to initialize all elements to point to sTarget_t. – fsw Sep 18 '13 at 08:13
  • Arrays decay to pointers in some expressions but they are __not__ pointers. – Blastfurnace Sep 18 '13 at 08:23
  • @Blastfurnace : "beeing" is a fuzzy term. aren't they behaving exactly like static pointers? as far as i am concerned "int a[5];" is assembly-wise equivalent of "int const *a=new int[5];" and "a[3];" is "*(a+3);", or isn't it? – fsw Sep 18 '13 at 08:35
  • 1
    The convenience of array subscripting (address arithmetic) confuses the issue but arrays really aren't pointers. See: [Arrays are Pointers?](http://stackoverflow.com/questions/3959705/arrays-are-pointers), [Is array name a pointer in C?](http://stackoverflow.com/questions/1641957/is-array-name-a-pointer-in-c), [comp.lang.c FAQ](http://www.c-faq.com/aryptr/aryptrequiv.html) – Blastfurnace Sep 18 '13 at 08:40
  • Simple example, given the two definitions of `a` in your comment, the result of `sizeof(a)` is different for `int a[5]` and `int *a`. Or given `int a[5], *b;` you do `b = a` but not `a = b`. They simply aren't the same type. – Blastfurnace Sep 18 '13 at 08:50
  • @Blastfurnace thanks a lot, this was interesting. fixed answer. – fsw Sep 18 '13 at 08:59
1

in list1.GetListOfTgts( "C1", &TgtList[bNO_OF_TARGETS] ); you pass address of an out-of-bounds element.

It should be just list1.GetListOfTgts( "C1", TgtList );

Also specifying the array sizes in the function prototype doesn't do what you probably want it to do.

Karadur
  • 1,226
  • 9
  • 16
  • Yes I changed it and it should fixed the problem but it doesn't. Could the array sizes in the function prototype cause this? – user2633954 Sep 18 '13 at 07:53