generic question about #include and pointers

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

I often see postings that include examples such as :

#include 
#include 

#include "lcd.h"
#include "buttons.h

...
.......
..
.....

char *p1, *p2;

I've always wondered what the purpose of the first 2 "blank" include lines is.....I've been assuming this just a lazy way of noting there are other includes (without bothering to type it all out), or do they serve some programming purpose that I've been missing out on?  I've never seen an app note or suggestion to do this, yet I see it in some postings.

 

I also see that defining two pointers using:  

char  *p1, *p2;  // is correct & is thus the "preferred way" ...but

char* p1, p2;  // is not correct.  Yet, why do I often see: 

 

char* p1;  //being used rather than:

char *p1; // both of these are allowed

Of course C ignores spaces/whitespace, so both of these are fine--just wondering why not use the (preferred) form that works in all situations?

 

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

I think the blank #includes are just a result of a bug in the old forum software - it had a problem with the '<' and '>' brackets

 

As for the pointers question, see: http://c-faq.com/decl/charstarws...

 

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: 1

avrcandies wrote:
I've always wondered what the purpose of the first 2 "blank" include lines is.....I've been assuming this just a lazy way of noting there are other includes (without bothering to type it all out), or do they serve some programming purpose that I've been missing out on?  I've never seen an app note or suggestion to do this, yet I see it in some postings.
This is an example of how not to migrate forum database to a new engine if you have no idea what you are doing. Basically they were proper includes with brackets like this <> (for example #include <stdio.h>), but migration tool though they were HTML tags and striped them off.

 

avrcandies wrote:

char* p1, p2;  // is not correct.  Yet, why do I often see: 

It has nothing to do with whites paces, p2 in this case is decaled as a char, not a pointer to a char.

 

Always write * next to the variable name, since it belongs only to that variable.

NOTE: I no longer actively read this forum. Please ask your question on www.eevblog.com/forum if you want my answer.

Last Edited: Sun. Aug 9, 2015 - 11:08 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Always write * next to the variable name, since it belongs only to that variable.

That's always been my way & understanding ....but  I often see examples with char* being used...it happens often enough for wondering WHY someone would even do that?  Maybe its just crowdcopying a "bad decision".

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

avrcandies wrote:
WHY someone would even do that?
Some coding style guides suggest using "char*", since it specifies a full type, but then they tell not to define multiple variables on the same line, which is a good idea anyway on many cases.

NOTE: I no longer actively read this forum. Please ask your question on www.eevblog.com/forum if you want my answer.

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

This is simply a matter of style - like some people write

if (condition)

 

but others prefer

if(condition)

 

I prefer the latter - for similar reasons to Alex's preference for char* - but it actually makes no difference whatsoever.

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

Hmm ... I got an error saying "Comment field required" - but my comment had been accepted!?

 

frown

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: Mon. Aug 10, 2015 - 05:56 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

awneil wrote:
I prefer the latter - for similar reasons to Alex's preference for char* - but it actually makes no difference whatsoever.
I prefer the first one for conditions and loops, otherwise there is too much stuff sticking together.

 

But I always use "char *variable"  and just to be safe, I try to not define multiple variables of complex types on the same line.

NOTE: I no longer actively read this forum. Please ask your question on www.eevblog.com/forum if you want my answer.

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

alexru wrote:
I prefer the first one for conditions and loops, otherwise there is too much stuff sticking together.

I would actually write it as

if( condition )

as you say, with no whitespace at all, there is too much stuff sticking together - and it gets hard to read.

 

But there do seem to be an awful lot of people with a strange aversion to whitespace who seem to "like" everything mashed together.

 

surprise

 

Quote:
I try to not define multiple variables of complex types on the same line.

Absolutely!

 

Although I would further avoid multiple variables of any type on the same line!

 

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: Mon. Aug 10, 2015 - 06:13 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The logic behind using "char* p;" instead of "char *p;" is ok for that case,

but it fails for for declaring arrays of anything.

That said, in most cases where "char*" is used, 'tain't important enough to fight about.

"SCSI is NOT magic. There are *fundamental technical
reasons* why it is necessary to sacrifice a young
goat to your SCSI chain now and then." -- John Woods

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

skeeve wrote:
'tain't important enough to fight about.

Absolutely - it is purely a matter of style.

 

But as for brace positions ...

 

cheeky

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

char* p1, p2;  // is not correct. 

It's one of the easiest errors to make and happens (inadvertently) all the time. Luckily the compiler should warn when the "non pointer" is used out of context.

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

My problem with 'if (' is you cannot search for the if as easily as 'if('

 

multiple declarations on the same line went out in the 80's when we got function prototypes.

It's not that hard to put each decl on a separate line,

Keith Vasilakes

Firmware engineer

Minnesota

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

keith v wrote:
My problem with 'if (' is you cannot search for the if as easily as 'if('

True - I hadn't thought of that as another advantage!

 

Quote:
It's not that hard to put each decl on a separate line

Absolutely.

 

It costs virtually nothing to do, but it does bring definite benefits.

 

 

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

My problem with 'if (' is you cannot search for the if as easily as 'if('

Tell me more about that?!?

 

What editor do you use that is not as happy searching "if (" as "if(" ??

 

Personally I surround all keywords and operators with white space in code.

 

('\s' is regex for "white space" if required).

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

This one rarely puts white space before a left paren.

It happens occasionally if I am editing someone else's code or in a complicated expression.

Mostly it's a matter of what I am used to.

Complicated expressions are often broken up using explicit intermediates.

 

Other people's white space is rarely worth whining about.

The primary exception is the GNU coding standard's brace formatting.

"SCSI is NOT magic. There are *fundamental technical
reasons* why it is necessary to sacrifice a young
goat to your SCSI chain now and then." -- John Woods

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

i always prefer one space after if, while, for, switch, etc before the open parenthesis .. it's not a function call. The compiler doesn't care of course but it's a visual cue as to the semantics .. plus I personally just think it's prettier. I personally omit the curly braces if there's only a single statement following if the if, however if it's an if/else if/else and any of those need curly braces, then they all get curly braces for consistency. Of course, if the indentation ever get's screwed up this can cause confusion and some prefer to always use curly braces even for one liners. I always put the opening brace on the same line ... except for when the if condition has to be more than one line, then I put the brace on it's own line so that it's visually easier to see where the conditional expression ends and the block of code following begins.

 

I guess in any case it's best to be consistent in style, and if you're working on code using a particular style to maintain that style throughout.

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

Style Wars can be pretty vicious.    I suggest that you just use what you are happiest with.

 

You can always run indent.exe on any foreign code to format in the way that you like.

 

Yes,   I do exactly this with any code that I do not understand.    I could (and often) do format code posted by readers that are allergic to the <space> key.    OTOH,   I reckon that a reader should make her question "attractive" in the first place  (if they want the best range of readers).

 

David.

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

badcode wrote:
I guess in any case it's best to be consistent in style

+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...