Variable types with * prefix

Hello again,

recently I am a bit more active in learning C++ stuff but some details I do not understand at all:

For example some variable types are used to have a * at the beginning or end of their name and are used both ways in the same code. This short code fragment is just to show what I mean.

     if (win_w != mcu_w)
     {
      uint16_t *cImg;
      int p = 0;
      cImg = pImg + win_w;
      for (int h = 1; h < win_h; h++)
      {
        p += mcu_w;
        for (int w = 0; w < win_w; w++)
        {
          *cImg = *(pImg + w + p);
          cImg++;
        }
      }
     }

I often see this with char variables or fields.

can somebody give me a short lesson and explain please ?

thanks

Google c pointers.

1 Like

thanks, I just did not know what to search for :flushed_face:

And it might be confusing as it’s the same symbol as for the multiplication.

So make sure you spend a bit of time reading about basic types in C++ and pointers, the various operators and the precedence

It can become cryptic

  int arr[4] = {1, 2, 3, 4};
  int* p = arr;
  int y = *p++ * *++p; 

Or

  int arr[5] = {10, 20, 30, 40, 50};
  int* p = arr;
  int y = *p++ * *p++;

EDIT: I should have added that this is to illustrate how pointer arithmetic combined with pre/post-increment can produce code that’s syntactically valid but unreadable, error-prone, and (in this case) even leading to undefined behavior.

…yes it’s confusing a lot but below the line it’s just about stored values at specific memory adresses and how to access them. That’s what I got into my brain memory today.

It should not be a programmer's hobby to deliver the most cryptic and obfuscated code possible…

OP is likely to see cases where pointers are used to run through a c-string. in that case it is important to know that a c-string always end with the character ‘\0’. A binary zero. Therfore you can do:

while(*strptr) {
   do_something_with_char_tgat_srtptr_points_to;
   strptr++:
};

(@ptillisch why do I not get a multi line code block???)

A post was merged into an existing topic: Why do I not get a multi line code block?

Hi UKHeliBob,
Problem solved by Ptillish. He took it to a new thread (as we are now stealing someone else's thread).
iProblem was indeed the markdown switch in wrong position...

It should not indeed

Some of the cryptic way to write dates from C times when we did not have optimizers and the developers had to help the compiler be smart..

Nowadays it’s not as necessary.

UFFF :kissing_face: Mr. @van_der_decken motivated me to try learning what Pointers are. I think I got it half way around and in few words a pointer is a variable that shows the physical location in the memory where the data represented by another variable is located. is this right ?

So far so good. I said half way around because I still don´t understand what the real purpose is to use pointers to find a value in the memory. For my sense this is the job of the compiler and I don´t care if the value of a variable “thisBox” is located at the memory address 0xADCD or 0xEFFF. I just use “thisBox” to refer to a value that I stored before. There was only one condition mentioned in all the tutorials I read that makes sense to me:

“Instead of passing a variable which is storing a big amount of data to a function it is better to pass a pointer to the location of this variable to prevent that the big amount of data gets copied to a second memory location for processing by the function”

For now this is the only reason for me to use a pointer instead of just a variable but it never applied to my small simple projects.

Actually I am not a programmer and about 45 years ago I decided to NOT study informatic and studied electronic telecomunications. (and I still don´t regret :shushing_face: ) When I started playing around with arduino processors I was impressed of what a lot of things I can do with them and how powerfull they are to input-process-output data and to control devices. I did not notice that I am programming a C++ sketch on the lowest possible level. I actually want to keep it like this and just control motors, displays, lights etc. by defining my simple custom conditions supported by switches, sensors or just simple math algorithm. Until now their was no need to know about classes, objects, structures, pointers and whatever C++ elements. And for my personal (ignorant) debug method, if some sketch gives me strange errors on compiling I try to find a similar example where the syntax is correct and modify my code to match the correct syntax. …and last step of fix a problem is … you already know, consult lovely expert people on the forums. Is there anything bad with this ???

PD.: I am 63 years old an ended my 35 years career as a systems administration and maintenance engineer.

That's the case for "easier" languages like JavaScipt and Python, which also have garbage collection. Even in the "big amount of data" case, the underlying runtime keeps track of addresses, and even moves things around if necessary.

But C has no such runtime; the underlying mechanisms are exposed to you -- namely stack and heap -- and you get to manage them. There are also global variables, which never move. When you use local variables, those are located on the stack, and you generally don't need to know those addresses either.

Arduino examples generally avoid the heap, which provides dynamic memory through new and (older C-style) malloc -- both return a pointer -- but it's definitely happening behind the scenes with libraries. String might be the poster child for this.

very interesting but too slow, my steppermotor allready runs for long time and the hands of my arduino clock move around like pointers and allways point to the right hour

If you can do without, do without.
pointers can really mess up things.
But remember: any time you print a message println("hello world"); a pointer will be used.
and indeed, you do not need to know.
To prevent copying large data chuncks, it is best to use references, not pointers...

Programming in C++ without learning about classes, pointers, or references, understanding types and statements is like owning a full toolbox but only ever using the screwdriver. You can build plenty of things and get good results, but you’re ignoring the hammer, the wrench, and the saw that let you tackle tougher jobs or do them more efficiently. The tools are all there, waiting, and the language was designed to give you the choice of staying simple or reaching deeper when you need to.

On microcontrollers this gets very real, because memory addresses don’t just hold numbers, they can represent hardware devices. A pointer can be aimed at the address of a pin register to turn LEDs on or off, or at a timer register to change how fast a PWM signal runs. The same idea is used for UART, SPI, and I²C peripherals, because they all expose their control and data registers at fixed memory addresses.

Pointers also underpin arrays and strings in C. Every time you write array[i], the compiler is really turning that into a pointer plus an offset. Dynamic memory management is built entirely around them, since malloc and free return and manipulate pointers.

They’re essential for efficiency on small MCUs. Passing around a pointer as noted instead of a big structure avoids wasting cycles and RAM. And they make callbacks possible: when you attach an interrupt service routine in Arduino, the library stores a pointer to your function and calls it later when the event happens.

That’s why even if you don’t need them every day, pointers are like those extra tools in your box: when the project gets tricky, they’re what lets you really control the machine instead of just working around its surface.

see Chapter 5 - Pointers and Arrays in The C CProgramming Language

https://seriouscomputerist.atariverse.com/media/pdf/book/C%20Programming%20Language%20-%202nd%20Edition%20(OCR).pdf#page=83

@gcjr, your link gives a 404.

1 Like

There is a lot of simple and interesting things, that can be done without pointers. But there is also a lot of more advanced things, that pointers make easier, or even make possible. You may not need this, but I am using it everyday.

Pointers can point somewhere and it can be changed in runtime, so they may point to many different things. And Compiler cannot resolve such needs at compile time.

I am building vocabulary on runtime, each word may have different length and may have associated many different data to it. I do not know, how many words will be needed until the program is already running. So I am using pointer to the last word, and each word have pointer to previous word, the first word have no prevoius, so its pointer to previous word is NULL.

When user enter some word, I se pointer to last known word, compare it with the users one and if they are the same, I know, what to do.

If they differs, I simply change my pointer to previous word and do the comparing again.

If I not find a match before I reach the NULL pointer, then I need add the new word into vocabulery. So I store it somewhere in yet unused memory, set its poiter to point to the last known word and I change the pointer for last word to point to the new word.

You may try to think how would you solve the problem with adding new words based on user input to vocabulery, without using pointers (or their equvalents).

1 Like

in Algorithms + Data Structures = Programs Niklaus Wirth described algorthms for organizing dynamic data in linked-lists and trees using pointers.

my jaw dropped when i read about pointers in The C Programming Language because they allow memory mapped hardware addresses to be specified and that hardware to be directly accessed in a higher level program. We had been writing assembler code linked into Pascal to do the same

corrected link - site is replacing '%20' with "%2520"

Thanks for fixing post #15; both do work now.

That link should be https://en.wikipedia.org/wiki/Algorithms_%2B_Data_Structures_%3D_Programs. Not sure if this is caused by a bug in the forum software, hovering over your original link shows correctly at the bottom of firefox without 25s but quoting as shown above shows two additional 25s and that is the actual link that is used.

1 Like

I know that @sterretje already noticed, but for the benefit of other readers I'll mention that there is a dedicated topic for discussion of the link bug here: