1

So I've been programming for my arduino which is some variant of c++. I have been writing a program to tick through every number of a 4 digit seven segment display. I have been having trouble setting my pins[] to different arrays of different lengths.

For example, displaying one means 2 pins will be on, which are expressed in the array int one[] = {1,2}; and displaying four takes four pins int four[] = {1,2,3,4};.

What I've been trying is:

int pins[] = null;
int one[] = {1,2};
int four[] = {1,2,3,4};
switch(num) {
    case 1: pins = one; break;
    case 4: pins = four; break;
}

However this has been causing problems and isn't letting me upload it because things just break everywhere.

I have a limited knowledge of of c++, only that it's similar to Java, which I know quite a bit about.

I feel like I'm feeding sharks with insufficient protection, and this one bit in my code is bugging me.

My question is how do you initialise an array, but then change it later on?

5
  • The problem with using an array of a given size is that, if you have to change its current size then you will be forced to copy all the elements from the previous array to a new array. To avoid copying array, you have to use a large enough array lets say of size 1000, if that is not a problem. However you might have efficiency problem, trying to traverse the entire array that is part empty. However, you can use vectors. With Vectors or a List, you do not have to know its size when you create it. You just create a vector and then add elements to it as you go.
    – Juniar
    Commented Aug 3, 2014 at 3:03
  • The problem is, even with an array of size 7 (the amount of segments and the maximum amount of pins on at once (digital clock 8)) I still can't reassign because size 2 isn't compatible with size 7. And due to my limited variant-c knowledge, I don't know how to make lists, and I feel they'd take up more power from the arduino chip compared to a, previously mentioned, pointer. But thank you for the response. My knowledge of variant-c is growing!
    – TheBrenny
    Commented Aug 3, 2014 at 3:13
  • 1
    There are several data structures such as: Vectors, List, Linkedlist and Maps. All these data structures were created with the same problem you have in mind. Using a vector is very simple, just create a vector and then add elements to it. The good part is that, it comes with functions that allows you to access the elements or traverse through the entire data and you do not have to worry about resizing it.
    – Juniar
    Commented Aug 3, 2014 at 3:31
  • The part "do not have to worry" is not true on a resource constrained platform like an Arduino. Closer to true is that using any of the collections with automatic resizing is a plan for automatic failure. Can the upvoter comment on how they thought that comment was useful advice?
    – jdr5ca
    Commented Aug 5, 2014 at 3:18
  • Although this question was created specifically for the arduino, the information given in the comment was useful to me as I'm keen on learning variant-c soonish, when I get time. I found that the comment was useful.
    – TheBrenny
    Commented Aug 5, 2014 at 5:32

2 Answers 2

5

As written, that won't compile because pins doesn't have a defined size and assigning an array to another array of a different size is not allowed. However, you can use a pointer to an array to achieve what you appear to be attempting:

int one[] = {1,2};
int four[] = {1,2,3,4};
int* pins = nullptr; // use NULL if your compiler doesn't support C++11.
size_t pinslen = 0;

switch (num) {
case 1:
    pins = one;
    pinslen = sizeof one;
    break;
case 4:
    pins = four;
    pinslen = sizeof four;
    break;
}
7
  • pins is not a pointer to an array, just a pointer to an int. It's pointing to the address of the first element of one of those two arrays.
    – David G
    Commented Aug 3, 2014 at 2:19
  • Yep, in C/C++ pointer to int is same as pointer to int array. The code is correct. Using the pointer, elements of the array are accessed with array syntax, for example: y = pins[3]; That example demonstrates the danger: if pins = four, then pins[3] is okay, if pins = one, the pins[3] does something wrong.
    – jdr5ca
    Commented Aug 3, 2014 at 2:34
  • 1
    @jdr5ca, but pinslen fixes this problem? for(int i = 0; i < pinslen; i++); is correct?
    – TheBrenny
    Commented Aug 3, 2014 at 3:18
  • Okay so after a little googling and work, I found that int* pins = nullptr; didn't work. However int *pins = 0; did... This is strange. I typed the latter many different times but it only works now. Well it works, probs update your answer and I'll re-tick it.
    – TheBrenny
    Commented Aug 3, 2014 at 4:44
  • Possibly your compiler doesn't support C++11 yet. nullptr is a new addition to C++. Commented Aug 3, 2014 at 4:45
1

int one[] = {1,2};, int four[] = {1,2,3,4}; are not the same type, we have int one[2] and int four[4]. They both may decay to int* but you loose the size information and should keep the size on your own.

A possibility is to use std::vector as follow:

std::vector<int> one = {1, 2};
std::vector<int> four = {1, 2, 3, 4};
std::vector<int>* pins = nullptr;
switch (num) {
    case 1: pins = &one; break;
    case 4: pins = &four; break;
}

And for C++03:

const int c_one[] = {1, 2};
const int c_four[] = {1, 2, 3, 4};
std::vector<int> one(c_one, c_one + 2);
std::vector<int> four(c_four, c_four + 4);
std::vector<int>* pins = 0;// or NULL
switch (num) {
    case 1: pins = &one; break;
    case 4: pins = &four; break;
}

Alternatively, you may want to copy in pins and don't use pointer, something like:

const int one[] = {1, 2};
const int four[] = {1, 2, 3, 4};
std::vector<int> pins;
switch (num) {
    case 1: pins.assign(one, one + 2); break;
    case 4: pins.assign(four, four + 4); break;
}
1
  • As I've previously mentioned in a comment, I'm not 100% on variant-c, not even close to 20% confident. I wanted basic things to answer, and the previous answer is the most helpful. However your answer is still an aid in which others can learn from.
    – TheBrenny
    Commented Aug 3, 2014 at 7:57

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.