Questions tagged [pointer-arithmetic]

You can perform a limited number of arithmetic operations on pointers. These operations are: increment, decrement, addition, subtraction, comparison and assignment.

You can perform a limited number of arithmetic operations on pointers. These operations are:

  • Increment and decrement
  • Addition and subtraction
  • Comparison
  • Assignment

The increment (++) operator increases the value of a pointer by the size of the data object the pointer refers to. For example, if the pointer refers to the second element in an array, the ++ makes the pointer refer to the third element in the array.

The decrement (--) operator decreases the value of a pointer by the size of the data object the pointer refers to. For example, if the pointer refers to the second element in an array, the -- makes the pointer refer to the first element in the array.

You can add an integer to a pointer but you cannot add a pointer to a pointer.

If the pointer p points to the first element in an array, the following expression causes the pointer to point to the third element in the same array:

p = p + 2;

If you have two pointers that point to the same array, you can subtract one pointer from the other. This operation yields the number of elements in the array that separate the two addresses that the pointers refer to.

You can compare two pointers with the following operators: ==, !=, <, >, <=, and >=.

Pointer comparisons are defined only when the pointers point to elements of the same array. Pointer comparisons using the == and != operators can be performed even when the pointers point to elements of different arrays.

You can assign to a pointer the address of a data object, the value of another compatible pointer or the NULL pointer.

Source:http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=%2Fcom.ibm.vacpp6m.doc%2Flanguage%2Fref%2Fclrc03ptarith.htm

718 questions
-2
votes
2 answers

Segmentation Fault (core Dumped) when working with pointers in C

So im currently trying to get my program to read a user input ( a double), and store said input in the heap using pointers in C. I allocate 40 bytes to the heap, and the program runs fine up until the program prompts me to enter an input to be…
-2
votes
2 answers

Sum of array digits using pointer arithmetic

I need to write sum of digits for all array elements using pointer arithmetic. Program should print: 0 1 2 5 8 16 14 #include void sumDigit(int arr[], int n) { int *p = arr, m, sum; while (p < arr + n) { sum=0; m = (*p) %…
user17936920
-2
votes
2 answers

printing strings with pointer arithmetics

I'm trying to print random words with the use of rand(). I think that I've made a mistake on the pointer arithmetic since I get a weird output. #include #include #include int main () { srand(time(0)); …
-2
votes
1 answer

How can I use different computational operations to get the results of two numbers in C++?

I am trying to get the results of two different numbers using "+","-", and mod"%" operators. However, I am running into trouble in coding it. I am using a textfile of numbers that look like this: 57854879876656543423468 and 654589876578097433579, to…
-2
votes
2 answers

What does this assignment to a string literal do?

This is my code. I want to know what is happening here. Can anybody describe the code? char *string; string = "Le Casa de Papel"; *(str+1) = 'a'; return 0;
asd
  • 51
  • 4
-2
votes
4 answers

How to access second member of struct via pointer?

I have seen the first address of struct is simultaneously the first address of first member of that struct. Now what I would like to understand is, why I need always double pointer to move around in the struct: #include #include…
milanHrabos
  • 2,010
  • 3
  • 11
  • 45
-2
votes
2 answers

How to understand pointer behavior for this code snippet

I know this code doesn't make much sense but I just wanted to know how the pointers in this code are working. int main() { int a=2; int *b = &a; void* c = (void*)b; printf("\n%d %d %d %d %d %d",a,&a,*b,b,c,*(int*)(c+1)); …
-2
votes
2 answers

Pointer can point to memory are that is not allocated?

Consider the following code: int *p; p = malloc(1);//p can point to 1 byte memory area Why can p point to many memory areas like below? printf("%p %p %p %p %p",p,p+1,p+2,p+3,p+4);
T1412
  • 1
-2
votes
6 answers

C Pointer Arithmetic for Unusual Architectures

I'm trying to get a better understanding of the C standard. In particular I am interested in how pointer arithmetic might work in an implementation for an unusual machine architecture. Suppose I have a processor with 64 bit wide registers that is…
Dschumanji
  • 125
  • 7
-2
votes
1 answer

Fast Pointer Increment/Decrement in Delphi

I need to inc/dec Pointers in the fastest possibile way. I noticed that the following two function increase a Pointer by the same value, with the same final result, but the second is 4 times faster that the first. procedure First(P: Pointer); var …
DelphianBoy
  • 45
  • 1
  • 5
-2
votes
1 answer

What does a pointer *(x+i) do?

I believe I roughly understood what pointers do, but I don't get what a pointer *(x+i) does. So for example *(x+1). Does it point to x and increase it's value by 1 or what? Also I found this example: *(x+1) = 5 What does that do? Thanks for your…
Mimi
  • 29
  • 2
-2
votes
1 answer

adding unsigned int to unsigned int*

My development environment consists of the g++ cross compiler for ARM OMAP Sitata. I discovered an unusual nuance of simple pointer arithmetic, when adding an unsigned int to an unsigned int*, as follows: unsigned int* dst_base_addr; unsigned int*…
usysinc
  • 17
  • 2
-2
votes
3 answers

Why address of i+2 is not 653064?

I'm learning pointers in C. I'm having confusion in Pointer arithmetic. Have a look at below program : #include int main() { int a[] = 2,3,4,5,6; int *i=a; printf("value of i = %d\n", i); ( *just for the sake of simplicity I have…
user3788135
-2
votes
2 answers

Value difference between two pointers is not making sense

I'm not getting the output. Why it is happening? #include int main(void){ int a[3][3]; int *p, *q; p=a[0]; q=a[1]; printf("%d\n",sizeof(int)); printf("%d\n",q-p); printf("%d %d\n",q,p); return…
Anirban Nag 'tintinmj'
  • 5,572
  • 6
  • 39
  • 59
-2
votes
2 answers

Pointer arithmetic (pointer substraction with casts)

Why is the j at the end of the execution an "1"? Compiled with following flags gcc -m32 xxx.c on a 64 bit Unix machine. #include int main(int argc, char **argv) { int *q = (int *)2; char *r = (char *)1; int j; q++; r++; j =…
plusplusC
  • 23
  • 3