locked
compilation error trying to pass address to structure into method RRS feed

  • Question

  • I think I am missing something basic here but am getting the compilation error conversion from data(*)[2] to non scaler type data requested.

    I have   a simple structure defined called data followed by

    .h file

    data indata[2];

    update_data(data[2]);

    in source file

    trying to call update_data and pass in address to structure

    update_data(&indata);//compilation error here,

    is there a problem with the function prototype?

     

    Friday, January 27, 2012 5:24 PM

Answers

  • Il 27/01/2012 18:24, svt44cobra66 ha scritto:


    .h file

    data indata[2];

    update_data(data[2]);

    in source file

    trying to call update_data and pass in address to structure

    update_data(&indata);//compilation error here,

    Did you try the simpler update_data(indata) ?

    The name of an array corresponds to its address (or the address of its first element).

    Giovanni

    • Proposed as answer by rp_suman Friday, January 27, 2012 6:09 PM
    • Marked as answer by Rob Pan Monday, February 6, 2012 9:23 AM
    Friday, January 27, 2012 5:29 PM
  • On 1/30/2012 12:11 PM, svt44cobra66 wrote:

    thanks for the response, if I use update_data(indata) I get (cannot convert data to data*)

    Can you show the exact text of the error, rather than your free interpretation of it?

    I get the error

    undefined reference to method update_data(data *ptrdata);

    Again, can you show the exact text of the error?

    My guess is, you've changed the signature in the function declaration (in the header), but not in the function implementation (in the source file). So you are calling a function that hasn't actually been implemented (some other function, with a different signature, has been implemented instead), so the linker complains.


    Igor Tandetnik

    • Marked as answer by Rob Pan Monday, February 6, 2012 9:23 AM
    Monday, January 30, 2012 5:33 PM
  • svt44cobra66 wrote:

    I have a method in update data called update_data2 that I am trying to  pass the
    same data into and tried  

    update_data2(&ptr[2])

    This doesn't make any sense. &ptr[2] attempts to take an address of the  element of ptr array having an index of 2 - but there is no such  element. The array has two elements, with indexes of 0 and 1.

    as the call to with the prototype as

    void update_data2(struct alldata ptr[2])

    Make it update_data2(ptr)


    Igor Tandetnik

    • Marked as answer by Rob Pan Monday, February 6, 2012 9:23 AM
    Sunday, February 5, 2012 5:08 AM

All replies

  • Il 27/01/2012 18:24, svt44cobra66 ha scritto:


    .h file

    data indata[2];

    update_data(data[2]);

    in source file

    trying to call update_data and pass in address to structure

    update_data(&indata);//compilation error here,

    Did you try the simpler update_data(indata) ?

    The name of an array corresponds to its address (or the address of its first element).

    Giovanni

    • Proposed as answer by rp_suman Friday, January 27, 2012 6:09 PM
    • Marked as answer by Rob Pan Monday, February 6, 2012 9:23 AM
    Friday, January 27, 2012 5:29 PM
  • thanks for the response, when I try update_data(indata) I get the error undefined reference to update_data(data*).  When I try update_data(&data), I now get the error, can't convert data (*)[2] to data*for arg 1 to voide update_data(indata); So I think for in data it assumes data (*)[2] as the type for indata.  It almost seems like the function prototype is not correct but I have it as void update_data(data[2]);
    Friday, January 27, 2012 6:11 PM
  • On 1/27/2012 1:11 PM, svt44cobra66 wrote:

    thanks for the response, when I try update_data(indata) I get the error undefined reference to update_data(data*).

    Can you show the exact text of this error?


    Igor Tandetnik

    Friday, January 27, 2012 6:44 PM
  • thanks for the response, if I use update_data(indata) I get (cannot convert data to data*) and this is if I set the prototype as  void update_data(data *ptrdata), as I am trying to pass just the pointer to the data structure.

    Then if I change the call to update_data(&data);

    I get the error

    undefined reference to method update_data(data *ptrdata);

    any ideas?

     

    Monday, January 30, 2012 5:11 PM
  • On 1/30/2012 12:11 PM, svt44cobra66 wrote:

    thanks for the response, if I use update_data(indata) I get (cannot convert data to data*)

    Can you show the exact text of the error, rather than your free interpretation of it?

    I get the error

    undefined reference to method update_data(data *ptrdata);

    Again, can you show the exact text of the error?

    My guess is, you've changed the signature in the function declaration (in the header), but not in the function implementation (in the source file). So you are calling a function that hasn't actually been implemented (some other function, with a different signature, has been implemented instead), so the linker complains.


    Igor Tandetnik

    • Marked as answer by Rob Pan Monday, February 6, 2012 9:23 AM
    Monday, January 30, 2012 5:33 PM
  • Arrays are actually always passed by pointer.so if you have some thing like struct data indata[2];

    you can use any of the following

    void update_data(struct xyz *stval)

    or

    void update_data(struct xyz stva[3])

    and you can call like update_data(indata) // take care of size 

     

     

    Thanks



    Rupesh Shukla
    Tuesday, January 31, 2012 2:13 AM
  • This is related to the other post, probably should have been combined into one, anyhow

    thanks for the reply, I was able to get this to work, it is actually a single dim structure array with 2 elements but ran into another issue.  I am able to de-reference the pointer to get the structure data in the method update_data using void update_data (struct alldata ptr[2]) as suggested above, but I have a method in update data called update_data2 that I am trying to pass the same data into and tried

    update_data2(&ptr[2]) as the call to with the prototype as

    void update_data2(struct alldata ptr[2])

    but in update_data2 when I access the data I get zeros, like the pointer to the structure did not get passed in? 

     

    Saturday, February 4, 2012 7:30 PM
  • svt44cobra66 wrote:

    I have a method in update data called update_data2 that I am trying to  pass the
    same data into and tried  

    update_data2(&ptr[2])

    This doesn't make any sense. &ptr[2] attempts to take an address of the  element of ptr array having an index of 2 - but there is no such  element. The array has two elements, with indexes of 0 and 1.

    as the call to with the prototype as

    void update_data2(struct alldata ptr[2])

    Make it update_data2(ptr)


    Igor Tandetnik

    • Marked as answer by Rob Pan Monday, February 6, 2012 9:23 AM
    Sunday, February 5, 2012 5:08 AM
  • thanks that seemed to work,  so for update_data2(ptr)  it is just passing the pointer to the 2 element array which is what I was trying to do.
    Sunday, February 5, 2012 5:04 PM