Even quicker pointer question

I have an unsigned long that I want to put into a byte-array.

unsigned long start_time = millis();

#define TIME_POS 14
byte storage[28];

Can I store and retrieve it like this:

// store
*(unsigned long *) &storage[TIME_POS] = start_time;

unsigned long last_start_time = *(unsigned long *) &storage[TIME_POS];


(I am fully aware of that the byte order may differ, but for the time being I will only use Atmega168 and Atmega8-based Arduinos for this.)

I think the retrieve will work, but I’d test it to be sure.

I think I know what you’re doing. Here is some code I put together for storing configuration data in the EEPROM. It uses a union, which I thought was the strangest beast in the C language until I found a use for it:

#include <avr/eeprom.h>
#include <avr/pgmspace.h>

char crlf[] = "\r\n";

struct config_s
  int reset;
  int fire;
  byte password_length;
  byte password[MAX_PASSWORD_LENGTH];

union config_union
  byte array[sizeof(struct config_s)];
  struct config_s config;

#define MAX_CONFIG_BYTES sizeof(struct config_s)

union config_union parameter;

void fetch_config()
  byte i;

  for (i=0; i < MAX_CONFIG_BYTES; i++)
    parameter.array[i] = eeprom_read_byte((unsigned char *) i);

void write_config()
  byte i;

  for (i=0; i < MAX_CONFIG_BYTES; i++)
    eeprom_write_byte((unsigned char *) i, parameter.array[i]);
  Serial.print("settings saved to EEPROM\r\n");

void setup()

void print_current_config()
  Serial.print("Current settings:\r\n");
  Serial.print("release pulsewidth: ");
  Serial.print(parameter.config.fire, DEC);
  Serial.print(" closed pulsewidth: ");
  Serial.print(parameter.config.reset, DEC);
  Serial.print("     DTMF password: ");
  // Serial.print(parameter.config.password);

Note that loop() and a lot of other code is missing; this just illustrates the use of a struct to hold all the configuration data I store in EEPROM, the use of a union to simplify getting that data in and out of EEPROM, and of course the EEPROM reading/writing functions.


Also, there's an EEPROM library that simplifies the call you have to make to write to the EEPROM.

I'm not storing in the EEPROM, but thanks for answering anyway.

My pointers seem to work, and my millisecond time seem to be passed into the byte-array fine.