June 28th, 1999, 10:42 AM
Hello,
I have a question. I have an array of structures that I allocate in main() and then pass the address of the pointer to a function. In the function I use the address to change on of the attributes in one of the arrays. When I return to the main() the attribute that I changed is incorrect - unititliaized ?? In the sample code below you see that I have tryed this will an integer (just to prove that my brain is working) and a simple array using malloc (this works) and then with my array of structs (this fails). Any comments would be appreciated. Sorry that we are not using C++ ... so sad ... I am writting for NT and Unix ... bla bla bla
// the structures
struct filenames
{
char szName[NAME_LEN];
int nIndex;
};
struct control
{
int nCurrentRecord;
int nTotalRecords;
};
// the function prototype
void TestFunction(struct filenames **, struct control *, int *);
// main
main()
{
// the simple int that we will pass
int nTest = 0;
int nTemp = 0;
// these are the pointer that we pass
struct control *pControl = NULL;
struct filenames *pFilenames = NULL;
// allocate memory for the array of structs
pFilenames = (struct filenames *)calloc( 1, sizeof(struct filenames));
// alocate memory for an array
pControl = (struct control *)malloc(sizeof(struct control));
// set the initial data
nTest = 99;
pControl->nCurrentRecord = 567;
pFilenames->nIndex = 9876;
// call the TestFunction passing the address of the array of structs, a pointer to a struct and the address of the int
TestFunction(&pFilenames, pControl, &nTest);
// on return we have access the data that has changed
// this should be 88 ... and it is
nTemp = nTest;
// this should be 4567 ... and it is
nTemp = pControl->nCurrentRecord;
// this should be 123 ... and it is not - uninitailized
nTemp = pFilenames->nIndex;
}
void TestFunction(struct filenames **pFilenames, struct control *pControl, int *nTest)
{
// change the value of nTest using the address of the integer
*nTest = 88;
// change the value of nIndex using the dereferenced address of the array of structs
(*(*pFilenames)).nIndex = 123;
// change the value of nCurrentRecord using the pointer passed in
pControl->nCurrentRecord = 4567;
return;
}
I have a question. I have an array of structures that I allocate in main() and then pass the address of the pointer to a function. In the function I use the address to change on of the attributes in one of the arrays. When I return to the main() the attribute that I changed is incorrect - unititliaized ?? In the sample code below you see that I have tryed this will an integer (just to prove that my brain is working) and a simple array using malloc (this works) and then with my array of structs (this fails). Any comments would be appreciated. Sorry that we are not using C++ ... so sad ... I am writting for NT and Unix ... bla bla bla
// the structures
struct filenames
{
char szName[NAME_LEN];
int nIndex;
};
struct control
{
int nCurrentRecord;
int nTotalRecords;
};
// the function prototype
void TestFunction(struct filenames **, struct control *, int *);
// main
main()
{
// the simple int that we will pass
int nTest = 0;
int nTemp = 0;
// these are the pointer that we pass
struct control *pControl = NULL;
struct filenames *pFilenames = NULL;
// allocate memory for the array of structs
pFilenames = (struct filenames *)calloc( 1, sizeof(struct filenames));
// alocate memory for an array
pControl = (struct control *)malloc(sizeof(struct control));
// set the initial data
nTest = 99;
pControl->nCurrentRecord = 567;
pFilenames->nIndex = 9876;
// call the TestFunction passing the address of the array of structs, a pointer to a struct and the address of the int
TestFunction(&pFilenames, pControl, &nTest);
// on return we have access the data that has changed
// this should be 88 ... and it is
nTemp = nTest;
// this should be 4567 ... and it is
nTemp = pControl->nCurrentRecord;
// this should be 123 ... and it is not - uninitailized
nTemp = pFilenames->nIndex;
}
void TestFunction(struct filenames **pFilenames, struct control *pControl, int *nTest)
{
// change the value of nTest using the address of the integer
*nTest = 88;
// change the value of nIndex using the dereferenced address of the array of structs
(*(*pFilenames)).nIndex = 123;
// change the value of nCurrentRecord using the pointer passed in
pControl->nCurrentRecord = 4567;
return;
}