• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

stupid C question

Spencer278

Diamond Member
I have a struct with array pointers to doubles. I want the pointers to point to an array of doubles and my question is how do I access the double in the array?
 
structname.array[x]

I can't remember if C uses [] or () so modify accordingly. .

edit: forums messed up my post
 
Originally posted by: BornStar18
structname.array[x]

I can't remember if C uses [] or () so modify accordingly. .

edit: forums messed up my post

I knew
struct->array[n] will give me the pointer to the array of doubles but from there I'm lost as to how to get a double
 
An array is a pointer automtically. I guess I'm not exactly sure what you're asking. If you define it:

struct{
doubles[100]
} somestruct;

somestruct bah;

you would access the first double number stored as "bah.doubles[0]".

Are you saying that its a (pointer to a (pointer to an array))?
 
Originally posted by: BornStar18
An array is a pointer automtically. I guess I'm not exactly sure what you're asking. If you define it:

struct{
doubles[100]
} somestruct;

somestruct bah;

you would access the first double number stored as "bah.doubles[0]".

Are you saying that its a (pointer to a (pointer to an array))?

I have some thing like
struct MyStruct {
double* StructArray[100];
} foo;
foo.structArray[n] = someArray;

I need to be able to get a double out of someArray given foo
 
Originally posted by: Spencer278
I have some thing like
struct MyStruct {
double* StructArray[100];
} foo;
foo.structArray[n] = someArray;

I need to be able to get a double out of someArray given foo
So you have an array that points to an array then? I guess I've never really contemplated that before. Give me a minute while i think about it.
 
You might want to consider simplifying your data structure if that's possible in the context you're using it (by using a multidimentional array for example)

otherwise I'd guess you might be able to access it using the dereference operator... maybe (&(foo.structArray[n]))[k] I'm really not sure if that'll work, but give it a go.
 

I have some thing like
struct MyStruct {
double* StructArray[100];
} foo;
foo.structArray[n] = someArray;

I think I'm doing something wrong on that line. I want that to mean an array of pointers but I don't think C is seeing it that way.
 
Originally posted by: RaynorWolfcastle
You might want to consider simplifying your data structure if that's possible in the context you're using it (by using a multidimentional array for example)

otherwise I'd guess you might be able to access it using the dereference operator... maybe (&(foo.structArray[n]))[k] I'm really not sure if that'll work, but give it a go.
I'm not so sure your second suggestion will work but I see no reason why your first one wouldn't. I think that's what you should do. This is rather messy and I'm not even sure there's a way to do it.
 
Originally posted by: Spencer278
I have some thing like
struct MyStruct {
double* StructArray[100];
} foo;
foo.structArray[n] = someArray;

I think I'm doing something wrong on that line. I want that to mean an array of pointers but I don't think C is seeing it that way.
Alright, yeah, you can't do that. Since an array variable is already a pointer, you're trying to make a pointer out of a pointer which is just no good. Double dimension your array and be done with it.
 
Originally posted by: Spencer278
I have some thing like
struct MyStruct {
double* StructArray[100];
} foo;
foo.structArray[n] = someArray;

I think I'm doing something wrong on that line. I want that to mean an array of pointers but I don't think C is seeing it that way.

Hm... Long time since I've done C let alone structs.

let's try :

struct MyStruct {
double* DoubleArray[100];
}
MyStruct foo;

double NewArray[1000];

foo.DoubleArray[0] = &NewArray[0];
 
double* foo[10];

foo[0][0] gives you the first element of the array pointed to by the first element of the pointer array foo.
 
Back
Top