Pointer to structure problem...
-
Hiya I have to structure and wanted to make a pointer to it WITH 10 INSTANCES. So I have: struct s_Test { char name[30]; } struct s_Test* Test[10]; void main() { memcpy( Test[0]->name,"Paul",30 ); // system error here return; } My problem happens when I go to access an element of the structure. I gives me the system error that the app must close. Any ideas how to access them properly?? Thanks;
-
Hiya I have to structure and wanted to make a pointer to it WITH 10 INSTANCES. So I have: struct s_Test { char name[30]; } struct s_Test* Test[10]; void main() { memcpy( Test[0]->name,"Paul",30 ); // system error here return; } My problem happens when I go to access an element of the structure. I gives me the system error that the app must close. Any ideas how to access them properly?? Thanks;
Try to use a
std:vector
ofstd:string
. That works. If you really cant use C++ and MUST continue to use C, then this one works:#include "stdafx.h"
#include < memory.h >struct s_Test
{
char name[30];
};int main(int argc, char* argv[])
{
struct s_Test Test[10];
memcpy( Test[0].name,"Paul",30 );
return 0;
}
My opinions may have changed, but not the fact that I am right.
-
Hiya I have to structure and wanted to make a pointer to it WITH 10 INSTANCES. So I have: struct s_Test { char name[30]; } struct s_Test* Test[10]; void main() { memcpy( Test[0]->name,"Paul",30 ); // system error here return; } My problem happens when I go to access an element of the structure. I gives me the system error that the app must close. Any ideas how to access them properly?? Thanks;
- The source buffer for memcpy is too small. It is just 5 characters ("Paul"+zero), but you specify 30. This will result in a memory access error. A valid value for memcpy source len would be strlen("Paul")+1. 2) Declare the Test array as array and not as pointer array. So instead of struct s_Test* Test[10]; do a struct s_Test Test[10]; 3) If you have done step 2 you don't need a dereferenced access to the name member variable any more. So the memcpy would look like:
memcpy(Test[0].name,"Paul",strlen("Paul")+1);
:-D -Dominik
_outp(0x64, 0xAD);
and__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do?? ;) - The source buffer for memcpy is too small. It is just 5 characters ("Paul"+zero), but you specify 30. This will result in a memory access error. A valid value for memcpy source len would be strlen("Paul")+1. 2) Declare the Test array as array and not as pointer array. So instead of struct s_Test* Test[10]; do a struct s_Test Test[10]; 3) If you have done step 2 you don't need a dereferenced access to the name member variable any more. So the memcpy would look like:
-
Hiya I have to structure and wanted to make a pointer to it WITH 10 INSTANCES. So I have: struct s_Test { char name[30]; } struct s_Test* Test[10]; void main() { memcpy( Test[0]->name,"Paul",30 ); // system error here return; } My problem happens when I go to access an element of the structure. I gives me the system error that the app must close. Any ideas how to access them properly?? Thanks;
.. and you rightly deserve an error message! The variable Test has been defined as a pointer to the structure:
struct s_Test* Test[10]
Therefore the memory location for your structure has not been allocated as yet.try:
.. .. struct s_Test Test[10]; void main() { // use the appropriate string Function. strcpy( Test[0].name, "Paul" ); return; }
"Wise men talk because they have something to say; fools, because they have to say something."
Plato -
Try to use a
std:vector
ofstd:string
. That works. If you really cant use C++ and MUST continue to use C, then this one works:#include "stdafx.h"
#include < memory.h >struct s_Test
{
char name[30];
};int main(int argc, char* argv[])
{
struct s_Test Test[10];
memcpy( Test[0].name,"Paul",30 );
return 0;
}
My opinions may have changed, but not the fact that I am right.
Meeeep. System error. Copying 30 characters from a buffer which is only 5 characters ("Paul"+zero) long. :-D :beer: -Dominik
_outp(0x64, 0xAD);
and__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do?? ;) -
Meeeep. System error. Copying 30 characters from a buffer which is only 5 characters ("Paul"+zero) long. :-D :beer: -Dominik
_outp(0x64, 0xAD);
and__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do?? ;)OOOPS! :omg: But it did compile.....:-O That type of errors is the reason why I prefer
std::vector
andstd::string
.
My opinions may have changed, but not the fact that I am right.
-
- The source buffer for memcpy is too small. It is just 5 characters ("Paul"+zero), but you specify 30. This will result in a memory access error. A valid value for memcpy source len would be strlen("Paul")+1. 2) Declare the Test array as array and not as pointer array. So instead of struct s_Test* Test[10]; do a struct s_Test Test[10]; 3) If you have done step 2 you don't need a dereferenced access to the name member variable any more. So the memcpy would look like:
memcpy(Test[0].name,"Paul",strlen("Paul")+1);
:-D -Dominik
_outp(0x64, 0xAD);
and__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do?? ;)Thanks for all of those suggestions and help. I know I am being awkward but I really want to get it working using a pointer to the structure. I could do it the normal way no problem but I only want to allocate the memory as I need it. I don't want it to be a static array. Is it not possible to have a pointer to a structure with instances aswell??
- The source buffer for memcpy is too small. It is just 5 characters ("Paul"+zero), but you specify 30. This will result in a memory access error. A valid value for memcpy source len would be strlen("Paul")+1. 2) Declare the Test array as array and not as pointer array. So instead of struct s_Test* Test[10]; do a struct s_Test Test[10]; 3) If you have done step 2 you don't need a dereferenced access to the name member variable any more. So the memcpy would look like:
-
Thanks for all of those suggestions and help. I know I am being awkward but I really want to get it working using a pointer to the structure. I could do it the normal way no problem but I only want to allocate the memory as I need it. I don't want it to be a static array. Is it not possible to have a pointer to a structure with instances aswell??
Sure, this is possible too:
struct s_Test
{
char name[30];
};s_Test *Test;
void main()
{
Test = new s_Test[10];memcpy(Test\[0\].name,"Paul",strlen("Paul")+1); // Do something more delete \[\]Test; Test = NULL; return;
}
:-D -Dominik
_outp(0x64, 0xAD);
and__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do?? ;) -
Thanks for all of those suggestions and help. I know I am being awkward but I really want to get it working using a pointer to the structure. I could do it the normal way no problem but I only want to allocate the memory as I need it. I don't want it to be a static array. Is it not possible to have a pointer to a structure with instances aswell??
If you need a table, you need a table. How do you understand a pointer to a structure with instances as well ? To be able to point at something, you need that the something exist in the memory. It is therefor impossible to have a table without allocating some memory for it. ~RaGE();
-
.. and you rightly deserve an error message! The variable Test has been defined as a pointer to the structure:
struct s_Test* Test[10]
Therefore the memory location for your structure has not been allocated as yet.try:
.. .. struct s_Test Test[10]; void main() { // use the appropriate string Function. strcpy( Test[0].name, "Paul" ); return; }
"Wise men talk because they have something to say; fools, because they have to say something."
Platoyup Plato.. what you said is correct ! there the array is infact a array of pointers to "struct s_Test " rather then a array of "struct s_Test ". "Think big, think fast, think ahead. Ideas are no one's monopoly"
-
If you need a table, you need a table. How do you understand a pointer to a structure with instances as well ? To be able to point at something, you need that the something exist in the memory. It is therefor impossible to have a table without allocating some memory for it. ~RaGE();
Thanks, have it sorted and know the reason y it can't just do it like that. thanks for all ur help. grahamoj
-
Thanks for all of those suggestions and help. I know I am being awkward but I really want to get it working using a pointer to the structure. I could do it the normal way no problem but I only want to allocate the memory as I need it. I don't want it to be a static array. Is it not possible to have a pointer to a structure with instances aswell??
What you really need is a vector or list of structures but this is a little too complicated to start off with. You can look up vector or list in the help for examples if you want. I assume that you will not know how many structures you will have at compile time and that is why you want a dynamic array. You can create a large array of pointers to your structure. This will take 4 bytes per element. Then allocate your structures when you need them. Have an integer keep track of the current number of structures. Try this: struct s_Test { char name[30]; } struct s_Test* Test[10]; int nTestCount = 0; void main() { // Add a struct and initialize Test[nTestCount] = new Test; strcpy( Test[nTestCount]->name,"Paul"); nTestCount++; // Add a struct and initialize Test[nTestCount] = new Test; strcpy( Test[nTestCount]->name,"John"); nTestCount++; for(int i=0; i < nTestCount;i++) { printf("%s\n",Test[i]->name); } return; }