Passing an array as argument to a function
-
Arrays of pointers are just as trivial as arrays of anything, if you understand how to address them.
fearless_ wrote:
I`m getting an `illegal indirection` error
Since we cannot see your screen we also cannot guess where that occurs. Please format your code properly and explain exactly where the error occurs.
I get illegal indirection at this spot
*ScreenLettersP_s[LetterVertexDataincrement]->position.x = i * letterwidth + ScreenLetterGroups[i].x
-
I get illegal indirection at this spot
*ScreenLettersP_s[LetterVertexDataincrement]->position.x = i * letterwidth + ScreenLetterGroups[i].x
The variable
ScreenLettersP_s
is an array of pointers, so the referenceScreenLettersP_s[LetterVertexDataincrement]
is one of the actual pointers. The leading asterisk onScreenLettersP_s
means an extra level of indirection which is not required (or valid). -
The variable
ScreenLettersP_s
is an array of pointers, so the referenceScreenLettersP_s[LetterVertexDataincrement]
is one of the actual pointers. The leading asterisk onScreenLettersP_s
means an extra level of indirection which is not required (or valid).so this is specific for structures only. because if I change *data[i] = i * 2; to data[i] = i * 2; in the k5054s example I will be editing the save entry rather than the save data.
-
so this is specific for structures only. because if I change *data[i] = i * 2; to data[i] = i * 2; in the k5054s example I will be editing the save entry rather than the save data.
No. Pointers are pointers whatever they point at, be it an array or a structure. Think about a piece of memory as a sequence of cells. So a pointer to any cell allows you to access all the following cells in order, by using an index (pointer plus offset). If you (the programmer) have decided that the area you point to should be treated as if it contains different sized blocks (aka a structure), that does not affect the physical properties of the memory. It merely allows the compiler to calculate the distance between the elements of the structure. And an array of pointers is much the same thing. If you have trouble visualising multi levels of indirection, then always go for a single level. If you have an array of pointers, then create a temporary one and allocate an array entry to it like:
CUSTOMVERTEX ** ScreenLettersP_s = new CUSTOMVERTEX* [NumberOfTextBuffers]; // an array of struct pointers
CUSTOMVERTEX* pTemp = ScreenLettersP_s[0]; // get the first pointer in the array
pTemp-> // now access the struct items. -
No. Pointers are pointers whatever they point at, be it an array or a structure. Think about a piece of memory as a sequence of cells. So a pointer to any cell allows you to access all the following cells in order, by using an index (pointer plus offset). If you (the programmer) have decided that the area you point to should be treated as if it contains different sized blocks (aka a structure), that does not affect the physical properties of the memory. It merely allows the compiler to calculate the distance between the elements of the structure. And an array of pointers is much the same thing. If you have trouble visualising multi levels of indirection, then always go for a single level. If you have an array of pointers, then create a temporary one and allocate an array entry to it like:
CUSTOMVERTEX ** ScreenLettersP_s = new CUSTOMVERTEX* [NumberOfTextBuffers]; // an array of struct pointers
CUSTOMVERTEX* pTemp = ScreenLettersP_s[0]; // get the first pointer in the array
pTemp-> // now access the struct items.I understand. for the record that`s directx 9.
-
I understand. for the record that`s directx 9.
fearless_ wrote:
directx 9
I have not used DirectX, but I have used plenty of other Windows' functions that use structures, arrays of structures, and even arrays of structures that contain other unstructured structures. In the latter case, the presence or absence of certain items depends on settings elsewhere.
-
fearless_ wrote:
directx 9
I have not used DirectX, but I have used plenty of other Windows' functions that use structures, arrays of structures, and even arrays of structures that contain other unstructured structures. In the latter case, the presence or absence of certain items depends on settings elsewhere.
You have a vast experience, I appreciate all your help.
-
You have a vast experience, I appreciate all your help.
-
Hi What is the syntax when you want to pass an array as argument? I`m looking for syntax for both function call and function definition.
Just as a conclusion to everything that has been said in this thread, if you want to pass an array as argument you have to declare it as pointer in the function definition.
-
Just as a conclusion to everything that has been said in this thread, if you want to pass an array as argument you have to declare it as pointer in the function definition.