Copying 2D array contents into another 2D array - Page 1
Forum section: electronics / general

author: rill17
author_rank: Contributor
timestamp: May 01, 2017, 04:46:22 pm
content: Hi, i need to copy the contents of a 2D array into another 2D array of the same size on a microcontroller. i tried an iterative approach using for loops as shown in the code below but it doesn't work. Any ideas?int a = 0;for( a=0; a<7; a++);{int b = 0;for (b=0; b<256; b++);{pulse_b= pulse;}}

author: Farley
author_rank: Regular Contributor
timestamp: May 01, 2017, 04:56:54 pm
content: QuoteRemove the semicolons at the ends of the quoted lines.Also, check the logic of what you're attempting. Research 2d arrays.

author: RoGeorge
author_rank: Super Contributor
timestamp: May 01, 2017, 05:06:00 pm
content: For max speed I would use sizeoff and memcpy.Carefull with memcpy, it is fast, but tricky.

author: sokoloff
author_rank: Super Contributor
timestamp: May 01, 2017, 05:09:00 pm
content: Once you remove the semicolons as Farley notes, you will find that your code is copying each of the 7 elements (0 through 6) from the first array into the second array, 256 times.(Inside the inner loop, you do the same assignment 256 times.)For two-D arrays, you'll need the inner statement to look something like:Code:pulse_alternate = pulse ;orCode:pulse_alternate[a*DIM + b] = pulse[a*DIM + b];where DIM is the size of the array along the "b" dimension (in this case, 256).Edit: Added code blocks to make clear what I was writing.

author: ynfo
author_rank: Contributor
timestamp: May 01, 2017, 05:42:21 pm
content: There are no 2D arrays in your code!2D arrays have 2 indices e.g. a

author: timothyaag
author_rank: Contributor
timestamp: May 01, 2017, 05:54:48 pm
content: He's not using a code block, but if you quote him you can see he's actually doing this:Code:pulse_b = pulse;

author: wraper
author_rank: Unknown
timestamp: May 01, 2017, 05:58:18 pm
content: Quote from: Farley on May 01, 2017, 04:56:54 pmThisQuoteWhat he posted was crippled by the forum:Quote from: timothyaag on May 01, 2017, 05:54:48 pm+1

author: brucehoult
author_rank: Super Contributor
timestamp: May 01, 2017, 06:05:19 pm
content: Quote from: rill17 on May 01, 2017, 04:46:22 pmDon't do this:Code:int a = 0;for( a=0; a<7; a++);Do this:Code:for(int a=0; a<7; a++);I suspect you started with that, and the compiler told you there was a problem that a (and b) were not defined in theCode:pulse_b = pulse;line.But you didn't understand what the compiler told you and "fixed" it in entirely the wrong way.

author: rill17
author_rank: Contributor
timestamp: May 01, 2017, 06:11:23 pm
content: yeah the code i am using is without the for loop semicolons and was modified due to the post shortcuts, the actual code is as followsCode:int a = 0;for( a=0; a<7; a++){int b = 0;for (b=0; b<256; b++){pulse_b = pulse;}}ill try the memcpy and your other suggestions and report back, thanks

author: wraper
author_rank: Unknown
timestamp: May 01, 2017, 06:12:38 pm
content: Quote from: brucehoult on May 01, 2017, 06:05:19 pmFirst of allfor(int a=0; a<7; a++); will not work with many compilers.Secondly, don't do this:int a;for(a=0; a<7; a++);{...}Do this:int a;for(a=0; a<7; a++){...}

author: rill17
author_rank: Contributor
timestamp: May 01, 2017, 06:40:01 pm
content: for anyone following, the simplest and most efficient way i found which works is by ditching the for loops and using memcpy. the following code works:Code:int i;for(i = 0; i<7; i++)memcpy(&pulse_b, &pulse, sizeof(pulse));}still cant figure out why the for loops didn't work though, thanks for the help

author: sokoloff
author_rank: Super Contributor
timestamp: May 01, 2017, 06:54:27 pm
content: Don't stop here. (IMO)Now that you have a way that works, power through and figure out why the "other" way that should have worked didn't.Post your new (but broken) code here in a code block and we'll help you figure it out.

author: MK14
author_rank: Super Contributor
timestamp: May 01, 2017, 08:06:48 pm
content: Quote from: rill17 on May 01, 2017, 06:40:01 pm(as others are trying to explain)The for loop, continually repeats the NEXT statement block.So if you go for (..............) ;The statement block is empty, so it does NOTHING. i.e. the semi-colon ends the statement block.for (i = 0; i < 1000000; i++);x = x + 1;would just increase x by ONE not a million.for (i = 0; i < 1000000; i++)x = x + 1;or better stillfor (i = 0; i < 1000000; i++){x++;}would increase x by a million.

author: Nusa
author_rank: Super Contributor
timestamp: May 01, 2017, 08:57:32 pm
content: For two identically sized arrays, it's as simple as:Code:memcpy(pulse_b, pulse, sizeof(pulse));

author: sokoloff
author_rank: Super Contributor
timestamp: May 01, 2017, 09:06:37 pm
content: Also, find the compiler setting that will emit a warning for "empty if body" and "empty loop body".For bonus points, turn on both that and "treat warnings as errors" (assuming you can find the pragmas to disable specific warnings that you cautiously verify as safe.

author: helius
author_rank: Super Contributor
timestamp: May 01, 2017, 09:16:03 pm
content: Quote from: rill17 on May 01, 2017, 06:40:01 pmIf the arrays were declared in the normal way, likeCode:int pulse;then the elements will all be contiguous. You don't need to call memcpy() once per row, but can just call it once for the array.Code:memcpy(pulse_b, pulse, sizeof(pulse));This won't work if the array was really declared as an array of pointers:Code:int *pulse;because the memory need not be contiguous.

author: TNorthover
author_rank: Contributor
timestamp: May 02, 2017, 12:54:25 am
content: Quote from: wraper on May 01, 2017, 06:12:38 pmReally, really shitty compilers, to the extent that I'd not touch the chip with a barge pole if that was all that was available. The standard revision that added support is 18 years old already (more if you count C++), and if that's not supported yet I wouldn't trust them to get anything else right either.

author: helius
author_rank: Super Contributor
timestamp: May 02, 2017, 01:17:15 am
content: Quote from: TNorthover on May 02, 2017, 12:54:25 amCode:> gcc test.ctest.c: In function ‘main’:test.c:11:3: error: ‘for’ loop initial declarations are only allowed in C99 or C11 modefor (int i=0; i<12; ++i)^> cc -vgcc version 4.9.2 (Debian 4.9.2-10)

author: TNorthover
author_rank: Contributor
timestamp: May 02, 2017, 02:08:30 am
content: Quote from: helius on May 02, 2017, 01:17:15 amOn modern systems the tool names dating from early UNIX (cc, cc1, ...) are often wrappers that tell the real compiler to use ancient standards so that old code still compiles. The underlying compiler is quite capable of supporting C99.Newly written code shouldn't be using that (at least not without a -std=something_sane argument).

author: helius
author_rank: Super Contributor
timestamp: May 02, 2017, 02:59:08 am
content: Quote from: TNorthover on May 02, 2017, 02:08:30 amOn this system cc is not a wrapper. It is a symbolic link, as gcc is also.Code:lrwxrwxrwx 1 root root 20 May 1 16:32 /usr/bin/cc -> /etc/alternatives/cclrwxrwxrwx 1 root root 12 May 1 16:32 /etc/alternatives/cc -> /usr/bin/gcclrwxrwxrwx 1 root root 7 Feb 25 2015 /usr/bin/gcc -> gcc-4.9It makes no difference which you type, because all these links act exactly the same.Quote-std=gnu90is the default, which is C89. It stayed that way for a very long time, until GCC 5.1.0 changed it recently.

author: TNorthover
author_rank: Contributor
timestamp: May 02, 2017, 03:21:20 am
content: Quote from: helius on May 02, 2017, 02:59:08 amSame thing, or at least I decided not worth noting the difference unless you're trying to implement this kind of thing. You can either have a real wrapper of some code to look at argv to decide how to behave.(I do believe you that this particular behaviour doesn't change for you, especially as it doesn't for me on Linux. macOS isn't so lucky: cpp is a wrapper that implements a pre-ANSI preprocessor!)QuoteThank goodness. It should have happened a long time ago.

author: brucehoult
author_rank: Super Contributor
timestamp: May 02, 2017, 09:32:49 am
content: Quote from: helius on May 02, 2017, 02:59:08 amBoy are you going to get a surprise when you learn how ccache works. Or busybox.Or, to be more historical, ex and vi, which both link to the same executable, and always have, but behave differently.

author: helius
author_rank: Super Contributor
timestamp: May 02, 2017, 01:47:46 pm
content: Quote from: brucehoult on May 02, 2017, 09:32:49 amNot at all. The sources for gcc aren't exactly a government secret: you can see for yourself that it does not care what argv is when selecting a language standard (within the C family; it does behave differently for ADA et cetera).