stupid C question

Spencer278

Diamond Member
Oct 11, 2002
3,637
0
0
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?
 

BornStar

Diamond Member
Oct 30, 2001
4,052
1
0
structname.array[x]

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

edit: forums messed up my post
 

Spencer278

Diamond Member
Oct 11, 2002
3,637
0
0
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
 

BornStar

Diamond Member
Oct 30, 2001
4,052
1
0
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))?
 

Spencer278

Diamond Member
Oct 11, 2002
3,637
0
0
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
 

BornStar

Diamond Member
Oct 30, 2001
4,052
1
0
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.
 

RaynorWolfcastle

Diamond Member
Feb 8, 2001
8,968
16
81
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.
 

Spencer278

Diamond Member
Oct 11, 2002
3,637
0
0

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.
 

BornStar

Diamond Member
Oct 30, 2001
4,052
1
0
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.
 

BornStar

Diamond Member
Oct 30, 2001
4,052
1
0
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.
 

Sahakiel

Golden Member
Oct 19, 2001
1,746
0
86
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];
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
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.