Go Down

Topic: Trivial questions (Read 1 time) previous topic - next topic

gianlucalongoni

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... :( 

PaulS

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

liudr

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: [Select]
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: [Select]

#define Led 13
digitalWrite(led,HIGH);
delay(1000);
digitalWrite(led,LOW);
delay(1000);


gianlucalongoni

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*...?

pYro_65

#4
Jan 23, 2013, 02:05 pm Last Edit: Jan 23, 2013, 02:07 pm by pYro_65 Reason: 1
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.


AWOL

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

*myPointer = 'c';  // Now, myChar contains the character 'c'
"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.

johncc


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: [Select]

  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

michinyon

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

Read one.

pYro_65

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.

PeterH


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.
I only provide help via the forum - please do not contact me for private consultancy.

gianlucalongoni

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

Read one.


:P 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...

johncc

#11
Jan 24, 2013, 01:58 am Last Edit: Jan 24, 2013, 02:01 pm by johncc Reason: 1
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
...

Arrch


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


defined constant, not variable.


johncc



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


defined constant, not variable.


Corrected, thanks!

Go Up