What happens if I increment an array variable?

2020-01-29 17:08发布

I know that it isn't safe to change a pointer's address if it lays on the heap because freeing it later would cause some trouble, but is it safe to do that if the pointer is declared on the stack?

I'm talking about something like this:

char arr[] = "one two three";
arr++;
//or arr--;

I hope I got that right by referring to a char array as a pointer.

5条回答
女痞
2楼-- · 2020-01-29 17:47

It's not where the pointer lives (heap or stack), but where the memory the pointer points to lives.

Memory on the stack is cleaned up automatically, you have to remember (keep pointers to) memory on the heap, because it's your responsibility to clean it up.

查看更多
趁早两清
3楼-- · 2020-01-29 17:56

As written, your code won't work because the operand of ++ must be a modifiable lvalue, and array expressions are not modifiable lvalues.

What you can do is something like this:

char arr[] = "one two three";
char *ptr = arr;  // ptr points to the leading 'o'
...
ptr++; // ptr now points to 'n'

As far as safety is concerned, you can still run into problems if the result of incrementing or decrementing ptr causes it to point to memory outside of the array, which may or may not be safe to access or modify.

查看更多
够拽才男人
4楼-- · 2020-01-29 18:02

you cannot change the address of an array. It will give a compile time error. have a look: http://codepad.org/skBHMxU0

EDIT:
the comments made me realize your true intent: something like:

char *ptr = "one two three";
ptr++;

There is no problem with it. the string "one two three" is a constant, and you can freely modify ptr, but note you might have troubles later finding the start of this string again... [but memory leak will not occur]

As a thumb rule, you are responsible for the memory you specifically allocated using malloc/new, and the compiler is responsible for the rest.

查看更多
不美不萌又怎样
5楼-- · 2020-01-29 18:03

You can not increment an array variable / array name, however you can access any element of the array by using array name / array variable. That is the reason why pointers came in to picture,. Array addresses are unmodifiable For example,

int k[3]={1,4,3};
printf("%d", *(k+1));  // compiles without any warning o/p is 4
printf("%d", *k++); //Will throw an error, Trying to modify an unmodifiable value

here in above snippet, Line 2: We are not incrementing array variable, however we are fetching the value of 1st indexed element in the array by using array address.

查看更多
Fickle 薄情
6楼-- · 2020-01-29 18:05

The line:

char arr[] = "one two three";

creates an array (which means its location is FIXED), it is not the same thing as a pointer as a pointers location can be moved. The array is default-initialized with the contents "one two three"; You can change the contents of the array as log as it doesn't grow in size, but you can't move arr.

arr++;

would thus be an error. You could, however, do:

char* ptr = arr;
ptr++;

to get to the second character of the arr array.

查看更多
登录 后发表回答