user1133150 user1133150 - 1 month ago 18
C Question

Pebble SDK - Arrays of Structs with Strings (and probably pointers too)

Firstly apologies as I think that this question covers a number of topics that are fairly commonly asked, but I've not found this particular combination asked before and I'm going round in circles trying to piece together information / answers.

Anyway...

I'm trying to write an app for my Pebble for use in the gym.

The idea is you can have a bunch of workouts in the watch, select your workout and then you'll be able to cycle through the exercises, see the reps / weights and also start a rest period which will trigger a timer.

I've used a tiny bit of C many moons ago (mainly embedded stuff), but I've lived most of my life in TCL / Python / Javascript and some Java and I'm having a bit of trouble getting my head round the bare bones nature of C.

Long term goal will be to have a companion Android app that you use to set up the workouts, but for now I'd be happy to hard code the data in, so long as the underlying data structures are in the right format.

This is my current route of attack:

#include <pebble.h>

struct Set {
int reps;
int weight;
};

struct Exercise {
char desc[32];
int rest;
int num_sets;
struct Set sets[];
};


struct Workout {
char desc[32];
int num_exercises;
struct Exercise exercises[];
};

struct Workout workouts[3];

strcpy(workouts[0].desc, "Shoulders");
strcpy(workouts[1].desc, "Back");
strcpy(workouts[2].desc, "Chest");


But this failing to compile with the following errors:

../src/c/workouts.h:27:8: error: expected declaration specifiers or '...' before 'workouts'
../src/c/workouts.h:27:26: error: expected declaration specifiers or '...' before string constant
../src/c/workouts.h:28:8: error: expected declaration specifiers or '...' before 'workouts'
../src/c/workouts.h:28:26: error: expected declaration specifiers or '...' before string constant
../src/c/workouts.h:29:8: error: expected declaration specifiers or '...' before 'workouts'
../src/c/workouts.h:29:26: error: expected declaration specifiers or '...' before string constant

Line 27 is the first
strcpy


I guess the other option would be just to have a massive data blob with a load of delimited fields and just churn through it that way:

char workouts[1024]

workouts = "W=Shoulders|Press|8x25kg|6x30kg|4x35kg|40s|..."


That just seems a bit ugly, although might end up being easier if I need to pass data back & forth to the Android app?

Anyway - any help appreciated!

Mark

GPS GPS
Answer

in C all execution must happen within a function. try replacing your lines with strcpy() with this:

int main() {
  strcpy(workouts[0].desc, "Shoulders");
  strcpy(workouts[1].desc, "Back");
  strcpy(workouts[2].desc, "Chest");

  return 0;
}

you might also want to include string.h

Comments