Using a pointer to increment

Hello,

I have probably a simple question about pointers, but I don't understand. If I declare a pointer

char * ptr

If I use this pointer to increment, is there a limitation on the size that I can increment? Can I increment higher than 65535? Or, because the pointer is pointing to a char is there some limitation on how high I can increment? Do I need to make this a unsigned long int *?

ptr+=65536 //will this work, can I go higher?

Thanks,

-ren

I don't know what the bit width of a pointer is on this platform, but even if the math worked out (it may well), you don't have 64 KiB of space to deal with! You have about 2 KiB of RAM (UNO), so in the best case, if you have a string at the lowest possible memory address, there's no point in attempting to reference anything beyond pointer + 2048...

Yes, I understand what you are saying about size. It is difficult for me to explain, but the pointer is being used a placeholder, a value in space. The point is never reached. So, without hardware limitations would it be possible to increment this pointer to a high value such as that? There shouldn't be a limitation, correct?

Pointers in context with a Harvard type processor (AVR, PIC, etc, using seperate program and data memories) is quite different then standard Von Neuman type processors (PCs, etc, using single memory space). Pointers can point to functions as well as data which are two different memories with two different widths and vastly different size in a AVR chip. So I think it can be a problem trying to answer your question without a more specific example of how you want to use a pointer in your sketch.

Lefty

I replaced the incremental pointer with an actual unsigned long int, seems to have solved my problem. However, I am still curious about one thing. When you create a pointer to increment, does the pointer increment the amount of bytes for that variable type? If I created char * ptr, that would increment 1 byte with the following statement ptr+=1, right? So what happens if you have a unsigned long *ptrlong? Would that increment 4 bytes with a statement ptrlong+=1?

Thanks,

-ren

does the pointer increment the amount of bytes for that variable type?

Yes.

I'm not sure about ptrlong+=1 but ptrlong++ will add the size of the data type being pointed to.


Rob

Thanks!

renasis:
I have probably a simple question about pointers, but I don't understand. If I declare a pointer

char * ptr

If I use this pointer to increment, is there a limitation on the size that I can increment? Can I increment higher than 65535? Or, because the pointer is pointing to a char is there some limitation on how high I can increment?

It's irrelevant what (data type) it points to. A pointer points to an address. On the Arduino pointers are 2 bytes, see:

void setup ()
{
  Serial.begin (115200);
  Serial.println();
  char * ptr;
  Serial.println (sizeof ptr, DEC); 
}

void loop () {}

Output:

2

renasis:
ptr+=65536 //will this work, can I go higher?

It depends how you define "work". Try this out:

void setup ()
{
  Serial.begin (115200);
  Serial.println();
  char * ptr = "dolphins";
  Serial.println ((int) ptr, DEC);
  ptr += 65536;
  Serial.println ((int) ptr, DEC);
}

void loop () {}

Results:

256
256

I supposed it "worked" but it didn't achieve much. Since the pointer is 2 bytes long adding 65536 wraps around to the same number.

So what happens if you have a unsigned long *ptrlong? Would that increment 4 bytes with a statement ptrlong+=1?

Try it and see, huh?

void setup ()
{
  Serial.begin (115200);
  Serial.println();
  long * ptr = NULL;
  Serial.println ((int) ptr, DEC);
  ptr++;
  Serial.println ((int) ptr, DEC);
  ptr += 1;
  Serial.println ((int) ptr, DEC);
}

void loop () {}

Results:

0
4
8

What are you trying to accomplish? Usually pointer arithmetic is used in conjunction with array dereferencing.

I have to agree with skyjumper here, adding big numbers to pointers is usually not a good idea. Incrementing by 1 can be a way of stepping through an array, but that's about it.