Setting Value of an Array from Another Array

Hi everyone,

I am wondering what I am missing in my syntax/location to avoid the error: "warning: invalid conversion from 'const byte* {aka const unsigned char*}' to 'byte {aka unsigned char}' [-fpermissive]"

The code in question is the initialization of some global arrays at the head of my code:

const byte SiteNumber = 0;     
const byte SiteIPs[][4] = { {1, 2, 3, 4}, {2, 3, 4, 5}, {3, 4, 5, 6}, {4, 5, 6, 7}, {5, 6, 7, 8} };
const byte MyIP[] = {SiteIPs[SiteNumber]};

The error is for the last line.

SiteIPs is a 2 dimensional array. So SiteIPs[SiteNumber] isn’t anything… It shoukd be SiteIPs[SiteNumber][anAnotherNumber]

But I guess you want MyIP to be an array of 4 as well. Then you should do

const byte MyIP[] = {SiteIPs[SiteNumber][0], SiteIPs[SiteNumber][1], SiteIPs[SiteNumber][2], SiteIPs[SiteNumber][3]};

Although I’m not sure why you would like that as another const…

What septillion said.

An alternative to using an initializer (which can be clunky for a lot of elements) is to do a memcpy in your setup().

memcpy(MyIP, SiteIPs[SiteNumber], sizeof(MyIP));

But if you do this, the compiler will complain if MyIP is const.

Thanks guys. Of all the things I tried, I didn't think about having to supply the value for the second dimension of the array... This appears to be working. For the benefit of others reading, it looks like the proper syntax in my case is:

const byte MyIP[] = {SiteIPs[SiteNumber][4]};

This takes the IP address from a multidimensional array of IP addresses, and initializes MyIP[] to that IP.

Thanks for the help again.

Uhm, no... const byte MyIP[] = {SiteIPs[SiteNumber][4]};

Will make a array with 1 value filled with the first entry of the next site (or an overshoot of the array aka random value if you try to this for site 4)...

septillion: SiteIPs is a 2 dimensional array. So SiteIPs[SiteNumber] isn't anything...

Wouldn’t SiteIPs[SiteNumber] be a pointer to a byte? In fact, it should point to the byte 1 which would be followed consecutively in memory by 2, 3, and 4.

Your right, I should have said "isn't a value" :)

The real question, as always, is not How, but Why: "Why do you want duplicate constants?"

You're better off with

const byte* MyIP = SiteIPs[0];

or, even better,

const byte* const MyIP = SiteIPs[0];

Yep, the why is always overlooked :(

septillion: Although I'm not sure why you would like that as another const...

But yeah, every question a topic starter doesn't like is skipped here...

Hey, there's no reason to insult me. I'm just trying to figure this out like most other folks on the forum. And yes, I did try for a couple of hours to figure out my problem before bothering you guys for help. If you would like to know WHY, all you have to do is ask. I am perfectly open to a different HOW once I've explained what I'm doing. I am perfectly ready to accept that you guys know WAY more than I do about this, that's why I'm here. And I deeply appreciate the time you take to give free advice.

So... all I wanted to do was set an array with a handful of IP addresses (const byte SiteIPs[][4]), and use the variable "const byte SiteNumber" to cherry pick one of those IPs to set the static IP of this particular device. I am integrating several Arduinos in a simple multi-location monitoring system, and I just wanted to be able to paste over one line of code (const byte SiteIPs[][4] = such and such) if I ever needed to update the IP of one of the devices.

This is probably a flagrantly stupid, moronic, imbecilic, Neanderthal way of doing things, and if you will grace me with superior suggestions (in small words, where I can understand), I will implement them straightaway!

I suppose that simply calling the SiteNumber location of SiteIPs when I set up the Ethernet connection (or whatever else) would work just as well, or better.

Seriously, thanks.