newbie question: passing struct as argument to function ? 
Author Message
 newbie question: passing struct as argument to function ?

I'm having major difficulties trying to pass a struct-array to a
function as a parameter, modify the parameter, and get the modified
variable out of the function.

------------------
typedef struct {
        short recTypeID;
        SYSTEMTIME sysDate;
        TCHAR comment[COMMENT_LENGTH];//comment

Quote:
} PersonalNotesInfo;

//Function that takes a PersonalNotesInfo-variable as an argument
//and fills that array with data
int GetPersonalNotesListPerPupil(short pupSubID, PersonalNotesInfo
*personalNotesInfoList)
{
        int i, numOfItems;
        ...
        ...
        //Allocating the PersonalNotes list
        numOfItems = CountNumberOfItems();              //Get number of records
        personalNotesInfoList = new PersonalNotesInfo[numOfItems];
        for (i = 0; i < numOfItems; i++)
        {
                // insert data into the parameter
                personalNotesInfoList[i].recTypeID = i;
                ...
        }
        ...
        return numOfItems;

Quote:
}

void main_function()
{
        int i, numOfNotes;      
        PersonalNotesInfo *personalNotesInfoList;

        // Sends the personalNotesInfoList-variable into the function, and

        // want it return initialized and with data
        numOfNotes = GetPersonalNotesListPerPupil(1,
&personalNotesInfoList);
        ...
        ...

Quote:
}

-------------------

In the main_function, the call to GetPersonalNotesListPerPupil gives
the following compil-error:

error C2664: 'GetPersonalNotesListPerPupil' : cannot convert parameter
2 from 'PersonalNotesInfo ** ' to 'PersonalNotesInfo *'

Can someone please tell me what I've messed up here ? I'm starting to
get frustrated here.......

- Ole Bredesen-Vestby



Mon, 14 Jul 2003 17:55:46 GMT  
 newbie question: passing struct as argument to function ?

void main_function()
{
        int i, numOfNotes;      
        PersonalNotesInfo *personalNotesInfoList;

        // Sends the personalNotesInfoList-variable into the function, and

        // want it return initialized and with data
        numOfNotes = GetPersonalNotesListPerPupil(1,
&personalNotesInfoList);
        ...
        ...

Quote:
}

-------------------

Hi Ole,

In the declaration of your GetPersonalNotesListPerPupil function you require a
PersonalNotesList pointer. In your main_function you pass a pointer to a pointer
because you write &personalNotesInfoList. THat means you pass the address of the
pointer. But you only need to pass the pointer itself. Thus, write only

        numOfNotes = GetPersonalNotesListPerPupil(1,personalNotesInfoList);

That should work.

Phoenix

.



Mon, 14 Jul 2003 18:39:52 GMT  
 newbie question: passing struct as argument to function ?
Thanx, the error message went away, but the changes I do to the
pointer in the GetPersonalNotesListPerPupil function is not present
when the function returns to main_function.

- Ole

Quote:

>Hi Ole,

>In the declaration of your GetPersonalNotesListPerPupil function you require a
>PersonalNotesList pointer. In your main_function you pass a pointer to a pointer
>because you write &personalNotesInfoList. THat means you pass the address of the
>pointer. But you only need to pass the pointer itself. Thus, write only

>    numOfNotes = GetPersonalNotesListPerPupil(1,personalNotesInfoList);

>That should work.

>Phoenix

>.



Mon, 14 Jul 2003 19:02:16 GMT  
 newbie question: passing struct as argument to function ?
Hello Ole,
i think ur not intializing the pointer.
PersonalNotesInfo *personalNotesInfoList;
after this intialize the pointer
//
personalNotesInfoList = new PersonalNotesInfo();
//
OR  do as follow
Declare like this
///
PersonalNotesInfo personalNotesInfoList;
///
 and pass the address of this to function
////
 numOfNotes = GetPersonalNotesListPerPupil(1, &personalNotesInfoList);
///

Vinod


Quote:
> Thanx, the error message went away, but the changes I do to the
> pointer in the GetPersonalNotesListPerPupil function is not present
> when the function returns to main_function.

> - Ole

> >Hi Ole,

> >In the declaration of your GetPersonalNotesListPerPupil function you
require a
> >PersonalNotesList pointer. In your main_function you pass a pointer to a
pointer
> >because you write &personalNotesInfoList. THat means you pass the address
of the
> >pointer. But you only need to pass the pointer itself. Thus, write only

> > numOfNotes = GetPersonalNotesListPerPupil(1,personalNotesInfoList);

> >That should work.

> >Phoenix

> >.



Mon, 14 Jul 2003 22:31:59 GMT  
 newbie question: passing struct as argument to function ?

Quote:

>I'm having major difficulties trying to pass a struct-array to a
>function as a parameter, modify the parameter, and get the modified
>variable out of the function.

>------------------
>typedef struct {
>    short recTypeID;
>    SYSTEMTIME sysDate;
>    TCHAR comment[COMMENT_LENGTH];//comment
>} PersonalNotesInfo;

>//Function that takes a PersonalNotesInfo-variable as an argument
>//and fills that array with data
>int GetPersonalNotesListPerPupil(short pupSubID, PersonalNotesInfo
>*personalNotesInfoList)
>{
>    int i, numOfItems;
>    ...
>    ...
>    //Allocating the PersonalNotes list
>    numOfItems = CountNumberOfItems();              //Get number of records
>    personalNotesInfoList = new PersonalNotesInfo[numOfItems];
>    for (i = 0; i < numOfItems; i++)
>    {
>            // insert data into the parameter
>            personalNotesInfoList[i].recTypeID = i;
>            ...
>    }
>    ...
>    return numOfItems;
>}

>void main_function()
>{
>    int i, numOfNotes;      
>    PersonalNotesInfo *personalNotesInfoList;

>    // Sends the personalNotesInfoList-variable into the function, and

>    // want it return initialized and with data
>    numOfNotes = GetPersonalNotesListPerPupil(1,
>&personalNotesInfoList);
>    ...
>    ...
>}
>-------------------

>In the main_function, the call to GetPersonalNotesListPerPupil gives
>the following compil-error:

>error C2664: 'GetPersonalNotesListPerPupil' : cannot convert parameter
>2 from 'PersonalNotesInfo ** ' to 'PersonalNotesInfo *'

>Can someone please tell me what I've messed up here ? I'm starting to
>get frustrated here.......

>- Ole Bredesen-Vestby

You correctly recognize the need to send the address of the pointer to
the function so the function can update the pointer directly.
However, &personalNotesInfoList is of type personalNotesInfo**, not of
type personalNotesInfo* which you specify in the prototype and
definition.

        1.  Change both the prototype and definition to
personalNotesInfo**.  (You may want to change the parameter name in
the function to avoid confusing it with the pointer in main.  You that
is, the compiler has no such confusion).

        2.  Change every reference to personalNotesInfoList in the
function to *personalNotesInfoList to dereference the ** you received
as a parameter back to the originals pointer in main.  Code of the
form personalNotesInfoList[i] will need to change to
(*personalNotesInfoList)[i] due to operator precedence.

<<Remove the del for email>>



Thu, 17 Jul 2003 01:59:39 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Passing a struct as a function argument/pthreads question

2. Passing function pointer with arguments as an argument?

3. newbie question: returning structs from functions

4. How to pass a member function as inetrnal function pointer argument

5. passing struct pointer as argument

6. passing struct* types as arguments

7. Newbie question: Pointer to an array and passing it into a function

8. Newbie - function pointers to variable-argument-number function

9. pointer-to-struct as function arguments (??!)

10. struct definition in function argument

11. Unknown struct as function argument.

12. How to pass data types as function argument ?

 

 
Powered by phpBB® Forum Software