You are not logged in.

#1 2011-03-03 02:15:34

gobeav3rs297
Member
From: Portland Oregon
Registered: 2007-11-06
Posts: 60

simple c array in struct pointer question

i have a two struct like so:

typedef struct {
       uint8_t data[256];
} foo1;

typedef struct {
       uint32_t data_ptr;
} foo2;

...
...

foo1 * ctx;
foo2    ply;

if i want to get the pointer to the data array in ctx and assigned to data_ptr would i do it like so:

ply.data_ptr = &(ctx->data);

Offline

#2 2011-03-03 02:21:50

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: simple c array in struct pointer question

There's a few problems here:

- data_ptr would need to be declared of type pointer in order to legally hold the address of another value.
- Barring that, you've also got a type mismatch. uint8_t != uint32_t
- Barring that, the data member of foo1 is an array, so referring to the member 'data' on its own implies an address, not a value. It would be valid to assign ctx->data to a pointer.

Offline

#3 2011-03-04 07:15:28

edma2
Member
Registered: 2009-08-20
Posts: 66

Re: simple c array in struct pointer question

&(ctx->data) returns a pointer to an array of 256 integers. This type will look something like uint8_t (*data_ptr)[256]. Instead you should do what falconindy suggested above:
ply.data_ptr = ctx->data

To avoid gcc warnings (which you should always avoid), data_ptr needs to be declared the correct type of pointer. Since ctx->data by itself is nothing more than a pointer to an 8-bit wide integer you want (uint8_t *). ((void *) also works)

Offline

#4 2011-03-04 07:55:57

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,296

Re: simple c array in struct pointer question

Also, ctx is a pointer to a foo1 structure.  No where did you actually allocate memory, nor did you cause ctx to actually point at that memory.  I realize this is not a complete program and this may have been implicit -- but it is worth noting.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#5 2011-03-11 17:13:14

vfbsilva
Member
From: Brazil
Registered: 2010-04-08
Posts: 103

Re: simple c array in struct pointer question

I suggest you to give a read on http://pw1.netcom.com/~tjensen/ptr/pointers.htm to clear some concepts and grasp some interesting points.

Offline

Board footer

Powered by FluxBB