I have a pointer going to a data structure. The data structure has a member that is a double array.
For the region my pointer points to, if I want to assign an element of the double array a value, how do I go about doing it?
I have a pointer going to a data structure. The data structure has a member that is a double array.
For the region my pointer points to, if I want to assign an element of the double array a value, how do I go about doing it?
Are you talking about something like this?
Code:struct SomeRandomStruct { int data[10][10]; }; //... later in the main program... SomeRandomStruct* mystruct = new SomeRandomStruct; mystruct->data[0][0] = 5;
Yes, like that. When I said double array, I actually meant an array of a double variable. Hehe...Sorry for the confusion.
I've been getting by using more of this though:
Although, they do the same thing, right?Code:struct randomStruct { double data[9000]; }; //... later in the main program... struct randomStruct *pointer = new randomStruct; (*pointer).data[0] = 4.0;
Another question, is it possible to use dynamic storage allocation for the members of your data structure? And if so (which I'm rather positive it is), how would one go about implementing it?
There is no 'new' keyword in C.
But I think you could do something like this:
How you would figure out the size, will differ though.Code:#include <stdio.h> #include <stdlib.h> int main() { int size = 100; struct randomStruct{ double *data; }; struct randomStruct randomStruct1; randomStruct1.data = malloc(size *sizeof(double)); if(randomStruct1.data){ printf("size of struct: %d\n", sizeof(*randomStruct1.data)); } randomStruct1.data[0] = 1.234; randomStruct1.data[1] = 5.678; printf("Content of randomStruct1.data[0] and [1]: %f, %f\n", randomStruct1.data[0], randomStruct1.data[1]); return 0; }
If you don't need dynamic allocation you could do like this instead:
Code:struct randomStruct{ double data[10]; }; struct randomStruct randomStruct1; randomStruct1.data[0] = 1.234; randomStruct1.data[1] = 5.678; printf("Content of randomStruct1.data[0] and [1]: %f, %f\n", randomStruct1.data[0], randomStruct1.data[1]);
BTW, if you use malloc, don't forget to free it when your done with it.
Really? My program crashes if I don't have '= new struct randomStruct' in the declaration. How...peculiar. I thought there was no 'new' keyword either, but I noticed it in scwizzo's post, so I tried it and it fixed the first error I had.
So, to make malloc work with the member, I would have to make the member a pointer then? And I can still declare a pointer that points to the structure, right? So...
Would that still work?Code:struct randomStruct { double *data }; randomStruct //...Later on int size = 9000; struct randomStruct *randomStruct1; (*randomStruct1).data = malloc(size *sizeof(double))
That implies that you are compiling as C++ instead of compiling as C.Originally Posted by Flamefury
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Now you have a pointer of the type randomStruct, you would still need it to point at an actual randomStruct.
So for example:
Should work, I think.Code:struct randomStruct randomStruct1; struct randomStruct *randomStructPtr; randomStructPtr = &randomStruct1; (*randomStructPtr).data = malloc(size *sizeof(double));
That would explain why I just failed the compiler test for handing my assignment in. Hmm.
This is the gist of what I have right now:
And it isn't co-operating without a 'new randomStruct'. How do I make it work under C terms?Code:struct randomStruct *randomCreate() { struct randomStruct *myStruct; return myStruct; } int main (void) { struct randomStruct *p = randomCreate(); }
Subsonics, I'll give that a shot.
Thank you for everyone's help so far! I really appreciate it.
What is it you are trying to do exactly? Right now you are assigning the address of an empty struct pointer to *p. IMO it's no different to just doing this:
Code:struct randomStruct *p;
I'll start talking about my assignment because it's getting hard to keep to general terms.
I want to create a program that manipulates polynomials. The polyCreate() function returns a pointer to (what I would assume to be) a zero-ed polynomial data structure, which will later have values added to it, multiplied, etc.
Right now, I have in my data structure one member, the coefficients, and I use it as an array to keep track of what degree the x value is that it multiplies by. So, in my polyCreate() function, I create the pointer and allocate the memory, then I zero all the values inside the coefficients array of the pointer's variable.
Although, if I try to call the polyCreate() function, my program crashes on startup. I just attempted to create a specific variable that the pointer points to, but it still doesn't work...
If you do like that you will get a pointer to the same datastructure each time you call polyCreate(). Structs are no different than any other data types in that respect. There are no constructors in C.
Try doing the same with a char for example, like this.
Then you'll see that both calls to charCreate, returns the same address to the same variable.Code:#include <stdio.h> char *charCreate(){ char b = 'a'; char *a = &b; return a; } int main (void) { char *c = charCreate(); char *d = charCreate(); printf("Address of c and d: %p, %p\nValue of c and d: %c, %c\n", c, d, *c, *d); return 0; }
I see. I had my suspicions it worked that way, and now I know for sure. Perhaps that's the reason my program is crashing?
In that case, what can I do to change the address that the pointer goes to?
Seems pointless to work on your program, when you're using the wrong compiler. Get the compiler right, *first*. Then you can start making progress on the program.
Your program should have options to set which compiler (C or C++), it will use. Get those options set and be sure to keep your file extension for your program dot c and *NOT* dot cpp.
"new" can be your test, also. If it immediately causes an error, then you should be using the C compiler. If not, you're still using the wrong (C++) compiler.