Passing array directly to sub or function

Go To Last Post
18 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just trying to pass a byte array to a function or sub without actually creating a byte array. Examples:

 

This works

uint8_t ba[] = {txCmdRemain, 0x08, 0x00};

txBA(ba, 3);

 

This doesn't

txBA({txCmdRemain, 0x08, 0x00}, 3);

 

Error cannot convert '<brace-enclosed initializer list>' to 'uint8_t* {aka unsigned char*}' for argument '1' to 'void txBA(uint8_t*, uint16_t)' Build C:\my projects\rids\Atmel Studio\BT Remote Control\main.cpp 506 35

Sandy
Steril-Aire

Last Edited: Wed. Mar 14, 2018 - 04:39 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't think that you can arbitrarily use a brace-enclosed list as a surrogate for an array anywhere other than initialization. And, an array can only be initialized once.

 

In this case, you also have to look at how the function parameter list is defined. If it works with txBA(ba, 3); then a pointer is being expected, not values.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Tue. Mar 13, 2018 - 06:19 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You aren't giving enough information.  Specifically, the definition of parameter 1 of your function call.

 

Show a small complete test program that demonstrates your symptoms.  Tell toolchain and version and build options -- in this case it could be the applied language standard.  Tell what you expect to happen; tell what >>is<< happening.  In this case, that probably means what you are trying to accomplish by lying about the type of parameter.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

C does not allow that - do you come from a Python world by any chance?

 

PS forgot to ask - what's wrong with creating ba[] anyway? the data has to be created SOMEWHERE in memory so why not in a location called "ba"? That could be global or stack/local, the former probably "costs" less to initialise. If the use of RAM is a concern (becaue the array contents are "const") then apply __flash to it and pass into a "__flash uint8_t *" pointer.

Last Edited: Tue. Mar 13, 2018 - 06:22 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've cobbled together an example for using a pointer to pass your array along to your function:

#include <stdio.h>
#include <inttypes.h>

uint8_t asdf[] = {3, 7, 5, 3, 4, 213, 8, 9, 0, 2, 1};

void foo( uint8_t * arrr, uint8_t index) {
	uint8_t cnt;
	
	for( cnt=0; cnt< 10; ++cnt) {
		printf( "\n%2d). The number is: %4u", cnt, arrr[ cnt]);
		if( arrr[cnt] == index) {
			printf( " Bingo");
		}
	}
}

int main ( void) {
	foo( asdf, 213);
}

I put the source code in the file adsf.c. If you have gcc installed on your PC you can compile it with:

 gcc asdf.c

The default output of any compiled program in C (Without further parameters) is always "a.out"

On my Linux box I have to set the executable flag before I may execute is as a program:

paul@dualcore ~ $ chmod +x a.out            // Set executable Flag.
paul@dualcore ~ $ ./a.out                   // Execute the program.
 0). The number is:    3                    // Program Output.
 1). The number is:    7
 2). The number is:    5
 3). The number is:    3
 4). The number is:    4
 5). The number is:  213 Bingo
 6). The number is:    8
 7). The number is:    9
 8). The number is:    0
 9). The number is:    2

 

Paul van der Hoeven.
Bunch of old projects with AVR's:
http://www.hoevendesign.com

Last Edited: Wed. Mar 14, 2018 - 02:41 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As clawson says, the 'C' programming language does not allow this.

 

Note that this is standard 'C' - nothing to do with AVR.

 

 

EDIT

 

although his PS does stray into areas that are implementation-specific.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Wed. Mar 14, 2018 - 02:58 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Paulvdh wrote:

uint8_t asdf[] = {3, 7, 5, 3, 4, 213, 8, 9, 0, 2, 1};

void foo( uint8_t * arrr, uint8_t index) {
:
:

If you're not so happy with mixing up arrays and pointers (which is perfectly valid in 'C'), you could also write that function as:

void foo( uint8_t arrr[], uint8_t index) {

 

Again, this is standard 'C' - nothing to do with AVR.

 

http://c-faq.com/aryptr/index.html

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
C does not allow that

awneil wrote:
As clawson says, the 'C' programming language does not allow this.

Did you both mean "C" as in "ancient C"? wink

 

txBA( (uint8_t[]){txCmdRemain,0x08,0x00}, 3);

 

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
Again, this is standard 'C' - nothing to do with AVR.
  So? He had a problem, I answered. I'll leave it implicitly up to a moderator if they want to move it to a "general programming" part instead of the "megaAVR and tinyAVR" part of this forum (I had to look that title up).

awneil wrote:
If you're not so happy with mixing up arrays and pointers
How could they ever be mixed up? They are the same.

 

To OP:

you may want to read a good book about C and / or C++. Kerninghan & Ritchie is the all age classic.

This forum has a section on books and resources for learning C / C++.

The Iso Cpp organisation has some books available: https://isocpp.org/search/google?q=books

And there are of course plenty of online courses

Writing some programs on a PC like I showed also makes it easyer to  learn the language itself, before you put it in an AVR.

Paul van der Hoeven.
Bunch of old projects with AVR's:
http://www.hoevendesign.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Paulvdh wrote:
He had a problem, I answered.

Now who's taking everything as personal criticism?

 

cheeky

 

But, seriously, that was a note for the OP - not you; as it's posted in the AVR-specific forum, that suggests that the OP may not appreciate that it is not AVR-specific.

 

 

How could they ever be mixed up? 

Well, lots of people do seem to have difficulty with this - which, I guess, is why there's that entire section in the C FAQ ...

 

They are the same.

YEs, They are equivalent - so the OP is free to use whichever they feel most comfortable with.

 

 

To OP:

you may want to read a good book about C and / or C++. Kerninghan & Ritchie is the all age classic.

This forum has a section on books and resources for learning C / C++.

+1

 

And here's some more 'C' learning & reference materials: http://blog.antronics.co.uk/2011...

 

Writing some programs on a PC like I showed also makes it easier to  learn the language itself, before you put it in an AVR.

+1

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sternst wrote:
Did you both mean "C" as in "ancient C"?

Yes - so how "modern" does it need to be to support this?

 

(I note that the OP is actually talking C++)

 

EDIT

 

To answer my own question:

 

The GCC Documentation wrote:

6.26 Compound Literals

A compound literal looks like a cast of a brace-enclosed aggregate initializer list. Its value is an object of the type specified in the cast, containing the elements specified in the initializer. Unlike the result of a cast, a compound literal is an lvalue. ISO C99 and later support compound literals. As an extension, GCC supports compound literals also in C90 mode and in C++, although as explained below, the C++ semantics are somewhat different.

 

https://gcc.gnu.org/onlinedocs/gcc/Compound-Literals.html

 

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Wed. Mar 14, 2018 - 03:37 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
Yes - so how "modern" does it need to be to support this?
C99

awneil wrote:
(I note that the OP is actually talking C++)
I missed that.

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

awneil wrote:
I note that the OP is actually talking C++

sternst wrote:
I missed that.

So did I.  So >>do<< I.  aw's crystal ball is a newer model than mine?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:
So did I. So >>do<< I. aw's crystal ball is a newer model than mine?

Sandy K. wrote:
Error cannot convert '' to 'uint8_t* {aka unsigned char*}' for argument '1' to 'void txBA(uint8_t*, uint16_t)' Build C:\my projects\rids\Atmel Studio\BT Remote Control\main.cpp 506 35

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

Yes - so how "modern" does it need to be to support this?

sternst wrote:
C99

Or, as an extension, in GCC for C90 - see my update in #11

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Everyone and thanks for your input. This morning was the 1st notification of any response. I had no idea there was 14 replies.

I should have mentioned that I am using Atmel Studio, C++.

 

Clawson, function wise there is no difference in "ba[] = xyz" or "txHex(xyz)". sometimes I write code just because of the way it looks. I figured if the array wasn't going to be used in the current sub, why not just pass it straight on to the next call. I am spoiled with vb.net interpreter, it allows such things along with returning arrays witch doesn't seem to be clean or easy in C++.

 

Pig, for the moment I accept that the interpreter didn't like what what I was trying to do. I have moved on. What I was trying to do was pass a fixed character array directly to a sub. The sub I was calling was expecting a character array to be passed, something like "void txHex(unsigned char c[])". The error I received is on my #1 post '<brace-enclosed initializer list>'.

 

Cat, I should have mentioned C++. For now I will avoid trying to do and chalk it up as a poor thing to do in C++.

 

Thank you for your response.

Sandy
Steril-Aire

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So have you understood the difference between an array, and a brace-enclosed initializer list? Or a Compound Literal?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi awneil

I would have to say more no then yes. I looked at your link and then did some poking around. I also did some poking around in vb.net world and they don't exist. It will eventually hit me and the light will turn on.

 

Thank You

Sandy

Sandy
Steril-Aire