Structs, Pointers, and Arrays? [SOLVED]

Go To Last Post
18 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello, folks -

 

As seems more and more usual, I am nearly over my head in code details that neither K&R or the internet seem to provide much insight to. [Either the examples or so complex that I get lost, or they use malloc(), which I know nothing about.] Here is my situation, using some simplified code. I have a struct (declared in a header):

typedef struct {
    uint8_t A;
    uint8_t B;
    uint8_t C;
    } LEDblock_t;

I create several versions of this struct in the corresponding c file:

LEDblock_t led_1 = { 1, 2, 3 };
LEDblock_t led_2 = { 3, 2, 1 };
LEDblock_t led_3 = { 2, 1, 3 };

Now, what I want to do is create an array of pointers, one pointer to each of the LEDblock_t structs. So, I think that what I need to do is:

LEDblock_t * ptr_array [] = { ?, ?, ? };

First point, here, is that I don't know whether I need to say that it is an array of structs (e.g., struct LEDblock_t ...). Its an array of pointers, and it should not care whether those pointers point to structs or chars, right?

 

Next, I don’t quite know how to populate the array. Do I use led_1, since that is a pointer? Do I use &led_1 since that is the address of led_1? [But, then again, isn't led_t = &led_t?] Or something else?

 

And, lets say that I do:

struct LEDblock_t * OnePointer;

How do I assign the value of OnePointer to be one of the array of pointers? Is it just (or is something more needed)?:

OnePointer = ptr_array[0];

Is it correct that, once I have this pointer, the elements of the struct are accessed in the usual way for a pointer referenced struct?

 

Many thanks to everyone who has been so patient with my questions!

 

Cheers
Jim

This topic has a solution.

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Sun. Mar 11, 2018 - 05:58 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This might give some tips (good or bad):

 

http://www.c4learn.com/c-programming/c-pointer-array-structure/

 

There are various topics to click on he left

 

 

When in the dark remember-the future looks brighter than ever.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That is certainly a less convoluted series of examples. Might well help.

 

Thanks

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The initializers for your array of pointers to struct need to be &led_1, &led_2, etc.

 

That is the "address of the led_1 struct", "address of the led_2 struct" and so on.

 

'&' is the "address of" operator in C

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Jim,

 

This may help clarify it for you:

typedef struct {
    uint8_t A;
    uint8_t B;
    uint8_t C;
    } LEDblock_t;

// if all you need is an array of structs, like this:
LEDblock_t array[] = {
	{ 1, 2, 3 },	// index 0
	{ 3, 2, 1 },	// index 1
	{ 2, 1, 3 },	// index 2
};

// if you need the LEDs explicity and a pointer array:
LEDblock_t led_1 = { 1, 2, 3 };
LEDblock_t led_2 = { 3, 2, 1 };
LEDblock_t led_3 = { 2, 1, 3 };

LEDblock_t *ptr_array[] = {&led_1,  &led_2, &led_3};

 

EDIT:

 

and to access them:

array[1].B = 123;

ptr_array[1]->B = 123;

 

Greg Muth

Portland, OR, US

Xplained/Pro/Mini Boards mostly

 

Make Xmega Great Again!

 

Last Edited: Sat. Mar 10, 2018 - 10:08 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Cliff -

 

is not led_1 the address of the struct named led_1? 

 

Greg -

 

Thanks for the great suggestions!

 

Appreciate all the help!

Jim

 

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Not the same for struct as arrays where just "name" is the same as &name[0]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:
is not led_1 the address of the struct named led_1? 

Quick answer, structures are aggregate types, led_1 returns an aggregate value, &led_1 returns the address of the struct.  Cliff can expound on this later.

 

​EDIT: Guess Cliff is still up.

"I may make you feel but I can't make you think" - Jethro Tull - Thick As A Brick

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

Last Edited: Sat. Mar 10, 2018 - 11:50 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you Cliff and Larry! An important fact just learned.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Sun. Mar 11, 2018 - 01:42 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am gobsmacked.    K&R is very clearly written.    It explains the "difficult" concept of struct,  pointers  etc.

 

I would just sit down with K&R and a nice cup of tea.     Switch off any PC, radio, ...

 

I guarantee that you will understand these topics within an hour or two.     Far more effectively than any Video, Tutorial,  ... and definitely better than the typical 1000 page "C textbook".

 

Brian Kernighan is still alive and well.    Dennis Ritchie died in 2011.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The explanations in K&R are clear but I get lost in the details of the particular examples that are used in that section (counting the number of occurrences of C keywords in text).  There is so much in that particular example having to with scanning and testing that the struct lessons get completely lost to me. K&R also has the problem, for me, of using abstract variable names even when the variable has a well defined function. For example, they always seem to use "ptr" for pointer, and a, b, and c for variables, even if a is is more clearly named page_count. Maybe this abstraction is a "higher ideal" for them or maybe they want to minimize "issues" with cross-language use (eg, English to German, lets say).

 

Greg's concise example really shows me what I have been missing (of course, it helped that he made his example very close to my original question). 

 

Thanks, again, everyone.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A follow-on question, please.

 

I plan to use various struct members of the array as "initial values" for several working structs, also of type LEDblock. To copy the initial value into the working struct, I need a pointer to the struct as a whole. Following on from Greg's example in #5, is array[0] the pointer to the first struct in that array? And array[1] the pointer to the second struct? Or, do I perhaps need &array[0]? Maybe this pushes me toward an array that is explicitly an array of pointers (Greg's ptr_array) ?

 

Thanks

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Sun. Mar 11, 2018 - 03:19 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Right first time. The array is an array of pointers (struct pointers in fact) so array[1] is a pointer to the second struct and so on so you use array[1]->B and similar.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But, in that FIRST example of #5, it is an array of structs, not an array of pointers to structs! The second example that Greg gave was, indeed, an array of pointers. So, if a pointer to one of the structs in the array of structs is needed, would it not be &array[0]?

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Sun. Mar 11, 2018 - 05:09 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@ka7ehk wrote:

 

So, if a pointer to one of the structs in the array of structs is needed, would it not be &array[0]?

 

Yes.

Greg Muth

Portland, OR, US

Xplained/Pro/Mini Boards mostly

 

Make Xmega Great Again!

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks, yet again!

 

Jim

 

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As I've been reading on a mobile I was simply reacting to posts about the code in #1. Reply #13 applies to that.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks, Cliff. Understand, now.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net