Pages: [1] 2   Go Down
Author Topic: Trivial questions  (Read 1090 times)
0 Members and 1 Guest are viewing this topic.
Casorezzo, Milan, Italy
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I apologize for the topic...
I have two questions for you:
1) what does
Quote
# define...
means!?
is it a non-ordinary manner in defining variables?

2) what is the difference between char and char* in defining a variable?...

thank you for the help...i wasn't able to find answers anywhere else... smiley-sad 
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50888
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

#define is used to create a name/value pair. It does not define a variable.

#define LED_PIN 13
creates a name, LED_PIN, and a value, 13. Wherever the name appears later, the value will be substituted before the compiler runs.

Quote
2) what is the difference between char and char* in defining a variable?...
A char is a variable that can hold one character. A char * is a pointer to a memory location that can hold one or more chars. It must be made to actually point to some memory before it can be used.
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 76
Posts: 7305
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The #define is not declaring or defining a variable. It is simply a substitution rule.

#define foo bar

then when foo shows up in subsequent code, it gets replaced by bar. Normally only complete match is replaced so foobar won't be replaced by barbar. Also foo inside a text string is not replaced.

This command is called a preprocessor directive. The substitution occurs before the code is compiled.
There are a lot of good use for this. If you want to blink an LED, you can do:
Code:
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);

But that would suck if you want to blink a different pin. You have to change pin number in multiple lines and pray not to make a mistake. But this will work much better.
Code:
#define Led 13
digitalWrite(led,HIGH);
delay(1000);
digitalWrite(led,LOW);
delay(1000);
Logged


Casorezzo, Milan, Italy
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
But that would suck if you want to blink a different pin. You have to change pin number in multiple lines and pray not to make a mistake. But this will work much better.

why not simply define a variable int led=13...? memory usage?

Quote
A char is a variable that can hold one character. A char * is a pointer to a memory location that can hold one or more chars. It must be made to actually point to some memory before it can be used.

could you please make an example in using char*...?
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 76
Posts: 2247
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
char array[] = { 'h', 'i', '\0' };

char data = array[ 0 ];

char *ptr = &data;

*ptr = array[ 1 ];

The first line creates an array of text.

Next a char variable is assigned the 'h' in the array's first element.

After that the pointer of 'data' is copied to a char* called 'ptr'

Then by dereferencing the pointer we can change the value in 'data' to the array's second element.

Also note, the highlighted '*' has a different meaning to the one below it. The top one declares a pointer type, the bottom dereferences a pointer.

« Last Edit: January 23, 2013, 08:07:25 am by pYro_65 » Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26631
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
could you please make an example in using char*...?
Code:
char myChar = 'a';
char* myPointer = &myChar;  // now myPointer holds the address of myChar

*myPointer = 'c';  // Now, myChar contains the character 'c'
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 361
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
But that would suck if you want to blink a different pin. You have to change pin number in multiple lines and pray not to make a mistake. But this will work much better.

why not simply define a variable int led=13...? memory usage?

Well it's not really "variable".... so,

Another way to achieve similar is to declare it as a constant
Code:
  const int Led=13;

This prevents you from assigning another value to it during the execution of your program, and allows the compiler (not preprocessor) to optimize/substitute it as it sees fit.

There are pros/cons of this versus #define, though I feel that the const (and its cousin "inline") are preferred in most cases. 

John

John
Logged

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3080
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's an idea.  There are a million and one books which teach C++ programming,  some of them free.

Read one.
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 76
Posts: 2247
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

consts and #defines are useful but there is a better way, which will ensure the compiler considers it a compile time constant.
Use an enum, they are effectively more constant than a variable marked const. Sometimes the compiler cannot always guarantee a variable is a compile time constant, whereas an enum is by nature.
Logged


UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sometimes the compiler cannot always guarantee a variable is a compile time constant, whereas an enum is by nature.

Can you explain that? I know that const-ness can be cast away or lost in some situations, but I would have thought that a const instance of an Enum type can lose its const-ness in exactly the same way that a const instance of an int type can.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Casorezzo, Milan, Italy
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Here's an idea.  There are a million and one books which teach C++ programming,  some of them free.

Read one.

 smiley-razz I'm actually trying to get one!

So, #define, just operates a substitution: #define var 11, means that every time i want to call a specific connection to "11" (pins number or just a number inside an equation) i'll insert var...
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 361
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes and it is a very common convention that #defined variables are uppercase

#define LEDPIN 11

...
pinMode( LEDPIN, OUTPUT);  // OUTPUT is probably a #define'd constant too
...
« Last Edit: January 24, 2013, 08:01:43 am by johncc » Logged

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3447
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

pinMode( LEDPIN, OUTPUT);  // OUTPUT is probably a #define'd variable too

defined constant, not variable.
Logged

Casorezzo, Milan, Italy
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you all!
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 361
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

pinMode( LEDPIN, OUTPUT);  // OUTPUT is probably a #define'd variable too

defined constant, not variable.

Corrected, thanks!
Logged

Pages: [1] 2   Go Up
Jump to: