Pointers/Structures/Arrays and Functions AAAAH

Okay so I keep running into this same problem and I have tried hard to find a solution with no success. Basically, I can get pointers working with functions, but when I change it to the pointer pointing to an array of structures I will get memory access errors.

Would the function prototype for a function receiving a pointer to an array of structures look like this:

void sample (struct myStruct *sampPoint);

?


When I write the definition for this function and I try to write to the pointers' members I get memory access errors. This is driving me insane. All help is highly appreciated. Thanks.
Last edited on
When I write the definition for this function and I try to write to the pointers' members I get memory access errors.


You could be going out of bounds of the passed array.

The syntax for it is actually quite simple:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
struct S
{
  int a;
  int b;
};

void Foo(S* ptr)
{
  ptr[2].b = ptr[0].a;  // ptr[2] demands there be at least 3 structs in the array
}

int main()
{
  S three[3];
  Foo( three );  // works

  S two[2];
  Foo( two );  // compiles okay, but explodes when run because 'two' isn't big enough

  return 0;
};
Last edited on
Topic archived. No new replies allowed.