Casorezzo, Milan, Italy
Offline
Newbie
Karma: 0
Posts: 22
|
 |
« on: January 22, 2013, 07:22:32 pm » |
I apologize for the topic... I have two questions for you: 1) what does # 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...
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 332
Posts: 36396
Seattle, WA USA
|
 |
« Reply #1 on: January 22, 2013, 07:39:16 pm » |
#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. 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
Faraday Member
Karma: 37
Posts: 6042
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
|
 |
« Reply #2 on: January 22, 2013, 07:42:34 pm » |
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: 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. #define Led 13 digitalWrite(led,HIGH); delay(1000); digitalWrite(led,LOW); delay(1000);
|
|
|
|
|
Logged
|
|
|
|
|
Casorezzo, Milan, Italy
Offline
Newbie
Karma: 0
Posts: 22
|
 |
« Reply #3 on: January 23, 2013, 07:54:22 am » |
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? 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
Edison Member
Karma: 35
Posts: 1277
|
 |
« Reply #4 on: January 23, 2013, 08:05:45 am » |
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
Brattain Member
Karma: 142
Posts: 19338
I don't think you connected the grounds, Dave.
|
 |
« Reply #5 on: January 23, 2013, 08:07:23 am » |
could you please make an example in using char*...? 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.
|
|
|
|
Temple, Texas
Offline
Sr. Member
Karma: 14
Posts: 354
|
 |
« Reply #6 on: January 23, 2013, 09:32:42 am » |
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 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
God Member
Karma: 9
Posts: 839
|
 |
« Reply #7 on: January 23, 2013, 09:47:21 am » |
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
Edison Member
Karma: 35
Posts: 1277
|
 |
« Reply #8 on: January 23, 2013, 09:51:26 am » |
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
Tesla Member
Karma: 98
Posts: 6769
-
|
 |
« Reply #9 on: January 23, 2013, 11:31:42 am » |
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
|
|
|
|
|
Casorezzo, Milan, Italy
Offline
Newbie
Karma: 0
Posts: 22
|
 |
« Reply #10 on: January 23, 2013, 07:36:17 pm » |
Here's an idea. There are a million and one books which teach C++ programming, some of them free.
Read one.  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
Sr. Member
Karma: 14
Posts: 354
|
 |
« Reply #11 on: January 23, 2013, 07:58:05 pm » |
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
Edison Member
Karma: 50
Posts: 2156
|
 |
« Reply #12 on: January 23, 2013, 10:32:41 pm » |
pinMode( LEDPIN, OUTPUT); // OUTPUT is probably a #define'd variable too
defined constant, not variable.
|
|
|
|
|
Logged
|
|
|
|
|
Casorezzo, Milan, Italy
Offline
Newbie
Karma: 0
Posts: 22
|
 |
« Reply #13 on: January 24, 2013, 07:49:12 am » |
Thank you all!
|
|
|
|
|
Logged
|
|
|
|
|
Temple, Texas
Offline
Sr. Member
Karma: 14
Posts: 354
|
 |
« Reply #14 on: January 24, 2013, 08:01:57 am » |
pinMode( LEDPIN, OUTPUT); // OUTPUT is probably a #define'd variable too
defined constant, not variable. Corrected, thanks!
|
|
|
|
|
Logged
|
|
|
|
|
|