Variable types with * prefix

Hello good morning everybody

while the experts were discussing how to get the POINT to solve how to POINT to POINTERS I was sleeping and now in 5 minutes I fixed my error in the exercise from @GolamMostafa and now I get the desired output:

void setup()
{
  Serial.begin(9600);
  long int *srcPtr;
  long int *destPtr;
  byte byteCounter = 2;
  long int srcData[] = {12345678, 87654321};
  long int destData[2];


  srcPtr = (long int*)&srcData;
  destPtr = (long int*)&destData;

  do
  {
    *destPtr = *srcPtr;
    destPtr ++ ;
    srcPtr ++;
    byteCounter --;
  }
  while (byteCounter != 0);

  for (int i = 0; i < 2; i++)
  {
    Serial.print(destData[i], DEC);
    Serial.print(' ');
  }

}

void loop()
{
}

I learned that I do not need to increment the address by 4 bytes because the declaration of long int already tells the compiler to increment 4 bytes to point to the next element.

Now my simple low level programmers version would be:

void setup()
{
  Serial.begin(9600);

  int byteCounter = 1;
  long int srcData[] = {12345678, 87654321};
  long int destData[2];

 do
  {
    destData[byteCounter] = srcData[byteCounter];
    byteCounter --;
  }
 while (byteCounter >= 0);

  for (int i = 0; i < 2; i++)
  {
    Serial.print(destData[i], DEC);
    Serial.print(' ');
  }

}

void loop()
{
}

now balance: pointer solution 25 relevant lines vs. array solution 17 relevant lines

now my “stone in my shoe” question is not answered yet:

I think I got it now but for my low level programmers purposes I really prefer my solution because my preference is making something like a mechanical device move or a TFT-display showing nice graphic before wasting time by struggling with Pointer syntax.

Anyway it was a great pleasure to get this excellent programming classes predominantly from @GolamMostafa thanks a lot :slightly_smiling_face:

1 Like

My original declaration was --

 int destData[4];

which you have changed to long int to make the programming easier.

int main(int argc, char** argv) {

Old golden standard for C programs (command line parameters) :slight_smile:

1 Like

I do remember as I played a lot with that arguments --

argc = argument count
argv = argument vector

If I could discover my old exercise books, I could nicely explain the need of **argv pointer to pointer. Probably, those arguments greatly facilitated the parsing of DOS command line.

In C++, a declaration introduces a name and its type to the compiler without necessarily allocating storage. A definition allocates storage for a variable or provides the implementation for a function or class, and thus is also by nature a declaration. Initialization gives an object its first value at the point of definition, while assignment changes the value of an already existing object later.

It is important to note that from the language semantics point of view, initialization and assignment are still different: initialization happens at object creation, while assignment happens afterward. The optimizer can sometimes make the generated code equivalent or even identical for simple types when the assignment follows the definition, but it does not change the semantic distinction.

2 Likes

ogram execution.

I think that this is hair splitting. Everyone knows what those terms mean, but let's give them a definition anyway:

  • Declaring a variable = reserve a piece of memory for it and give tihs memory a name.
  • Defining a variable = telling the compiler that a variable of a certain type (amount of memory) is going to be used but the memory itself is not allocated yet.
  • Assigning the variable = copying some value into variable's memory space.
  • Initializing the variable = assigning it a value for the first time.

I don't think that clarifies anything. Back to your question, what would the following code do?

`byte *ptr;

*ptr = 0x35;`

Most likely crash the controller, since ptr's value is undefined (haven't been allocated yet) and point to, well, whatever was in the memory that ptr occupies before this memory was assigned to ptr. So ptr may point to any location of the memory until it gets properly assigned with some address. So it is a bad idea to do something like this.

But, once again, does this question have a point?

2 Likes

No. Declaring by itself does not allocate the memory unless it’s the definition.

You got it reversed :wink:

It’s a bit of a shortcut because the first assignment otherwise would be also an initialization by your terms.

it’s at definition time, which is also happpen to be the first time

supporting this ... an object file (.o, .elf) typical contain 3 sections: .text for code, .data for initialized variables and .bss which reserves space for uninitialized variables

Sorry for that :confused:

The "position" is the same: as you said, "between" the type and variable. The position of some operators matter, like ++ and --. The difference with those three is with the spaces, which are optional

byte*ptr;  // fourth variant -- do not use

as with

x=a+b+c+d;  // spaces would help readability

and even

x=a++ +b;
x=a+ ++b;
x=a+++b;  // no really, don't do this -- auto-format does the right thing

For complex entities (objects) the difference can also be practical and significant. Objects are initialized by the class's constructor and the code executed there is completely different than the code that's executed during an assignment ... which happens in the function operator=().

1 Like

Indeed

That means:

int y;      //it is a declaration; no memory space is allocated
-------

int y;
y = 0x35;      //variable y is defined; memory space is allocated
---------

int y = 0x35; //variable y is initialized; given a value to y for the first time
----------

y = 0x76; //variable y is assigned a new value

I think so.

No…

In one file, say a .h

extern int y; // declaration no memory allocated

In another file, usually the associated .cpp

int y; // definition memory allocated no initialization 

With possibly later in a function

y = 42; // assignment 

Or

int y = 42; // definition memory allocated with initialization 

It's a definition; memory is allocated. Below is a variable declaration

extern int y;

No memory is allocated; it's your responsibility to allocate it (read: create a variable of the same type with the same name) elsewhere.

See e.g. Difference between Definition and Declaration - GeeksforGeeks