Syntax for function calls.

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

I am aware of, and have used in the past the convention of:

if I have a function which takes a struct of two integers such as:
 

typedef struct {
    int a, b;
} twoInts;

and have a function which accepts that struct as an argument such as:

 

int addTwoInts(twoInts input) {
    return input.a + input.b;
}

that calling this function can be done in two main ways...

 

twoInts foo = { 10, 20 };
answer = addTwoInts(foo);

 

or, more simply.
 

answer = addTwoInts({20, 20});

1st, is there an actual name for the second method... like...erm... implicit struct creation or something?...
2nd I'm trying this in AtmelStudio 7 and getting syntax errors, "expected expression before '{' token". Which seems odd as I know I've used this kind of syntax before.

 

Thanks in advance for any clarification people can offer on this. 
 

This topic has a solution.
Last Edited: Wed. May 23, 2018 - 04:08 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

To me, it is "just like"

 

void foo (char * MyString);

foo("XYZ");

There is a name for it but I don't remember anything about what it might be called. And, I can't help with your error message.

 

Jim

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

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Try casting it to the data type like this:

answer = addTwoInts((twoInts){20, 20});

 

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

Kal9001 wrote:
Which seems odd as I know I've used this kind of syntax before.
Nope, unless you have used a compiler that offered that syntax as a non-standard extension.

 

It is called "compound literal" and the real syntax is this:

answer = addTwoInts( (twoInts){20, 20} );

Stefan Ernst

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

If you are going to invoke it like that isn't a cleaner syntax simply:

answer = addTwoInts( 20, 20 );

as a result of:

int addTwoInts(int a, int b);

Don't try to over-complicate things if you don't need to.

 

(though I realise this may simply be a trivial example and perhaps the real struct is far more complex?).

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

clawson wrote:

(though I realise this may simply be a trivial example and perhaps the real struct is far more complex?).

LOL that you said "complex" as that was one of the things I thought of, real and imaginary parts of a complex number.

 

But anyway:  How many of us would do a lot of passing-by-value of structures and such in the first place?  Especially complex ones.  Very soon after the mentioned two elements, passing-by-reference looks a lot more attractive.

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

I think the second method is a gnu extension.

For me it works with gnu99, but not c99.

A c99 compound literal has the syntax of casting an initializer list.

That gnu allows the cast to be implicit, I think is an extension.

International Theophysical Year seems to have been forgotten..
Anyone remember the song Jukebox Band?

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

ISTR there was a discussion here recently about "compound literals" ... ?

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

Casting first solved this, great!
I didn't need to do this before, it "just worked", though that was both with Visual studio C++, and C in CodeBlocks, of course in both situations I've not payed much attention to the actual nuances of the different versions of the language or what flags are being used (Visual C++ vs vanilla, or the mingw C compiler in CodeBlocks vs gnu/gcc) trap for new(ish) players I guess?..

As you all no doubt have worked out the example was somewhat contrived, in the real scenario sometimes I want to call the function with an already created struct, perhaps deferenced from somewhere else or sometimes with a struct which didn't exist yet, and it seems messy to have to make the struct before passing it.

Anyway it's been a big help and I now know a proper name for it! Thank you all.

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

Kal9001 wrote:
and it seems messy to have to make the struct before passing it
Really? You are talking about the difference between:

answer = addTwoInts( (twoInts){20, 20} );

or:

twoInts data = {20, 20};
answer = addTwoInts( data );

That's almost the same amount of typing and, for my money at least, clearer to read.

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

Agree.

 

probably amounts the same in terms of generated code & memory usage, too - the compiler will probably optimise-out the intermediate variable.

 

 

EDIT

 

And, as noted, support for and, thus, use of compound literals is not so widespread

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. May 23, 2018 - 09:56 AM