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
-4
votes
3 answers

Pointer arithmetic of unspecified type with known size

void * ptr = NULL; // array of unspecified 13-byte type for (int i = 0; i < 10; i++) { printf("%i ", ((char (*) [13]) ptr) + i); } putchar('\n'); Output: 0 13 26 39 52 65 78 91 104 117 So this works (at least on GCC) but how can I declare a…
Salt
  • 13
  • 2
-4
votes
2 answers

My Program not printing array by pointer arithmatic

#include using namespace std; int main() { int a,count=0; cin>>a; int* arr; while(a) { int x= a%10; a=a/10; *(arr+count)=x; count++; } for(int i=0 ;i< count-1; i++) { cout<<*(arr+i); …
-4
votes
1 answer

Displaying an array by using pointer techniques

I want to display the contents of an array with type double that has a length of 5 without using the [ ] indexing . I can only use a for loop and pointer techniques. How can you achieve that ? Can anyone give me a brief explanation of pointers as I…
Takari
  • 29
  • 2
  • 3
  • 8
-4
votes
1 answer

C:Pointer Arithmetic -How does it work?

I'm new to C programming and trying to understand how pointer arithmetic works. The below printf statement prints 2 when the arguments for printf is *(p+2) and 4 with for *p. Could you please explain this behaviour ? #include #include…
user3948547
-4
votes
1 answer

Arithmetic Cuda program compilation error

Am working on a CUDA program which am new to. I encountered the error below, tried fixing but came to a halt. Can anyone take a look and tell me what I might be missing? Any help will be appreciated. Error 5 error : too few arguments in…
-5
votes
1 answer

Subtraction of pointers in array should be index subtraction or address subtraction?

enter image description here Shouldn't the answer be 2nd ques? ptr1 saves the address of arr[0] and ptr2 saves the address of arr[3]. So (ptr2-ptr1) should be ((34(size of float)) -(04)) i.e., subtraction of address not the subtarction of index..
-5
votes
3 answers

What does this print and how can I convert it to pointer arithmetic?

This was an exercise included in an exam by my professor and I can't get my head around it. I thought it had to do with comparing the arrays of chars but that does not seem to be just that. #include int f (char s[], char t[]) { int i,…
-5
votes
3 answers

how is the output of the statement is 'E'

#include main() { printf("ABCDE" + sizeof("ABC");/*please explain the output*/ } Output of the program is E compiled with gcc, please explain
-5
votes
1 answer

Can't understand strlen expression argument result

#include "stdio.h" #include "string.h" int main(){ char p[]="CALIFORNIA\n"; char *x ="HELLO"; printf("%lu\n",strlen(p+sizeof(p)-sizeof(x)+5) ); } I searched all over but couldn't find what happens when the argument of strlen is an…
momo
  • 1,052
  • 5
  • 16
  • 34
-5
votes
1 answer

Directly point to any element of dynamic-length number of elements without pointer arithmetic

Is it possible to write a C++ program that allocates a user-inputted number of elements and gives them user-inputted values, and can then point directly to any element of those that have just been allocated, without having to use pointer arithmetic?…
Måns Nilsson
  • 431
  • 1
  • 4
  • 16
-5
votes
2 answers

Is this custom malloc OK?

I need to write a custom malloc for GPU programming. Will this work correctly? void* malloc(int size, int* bytesUsed, uchar* memory){ int startIdx = (*bytesUsed); (*bytesUsed) += size; return (void*)(memory+startIdx); } I'm new to C…
Elliot Gorokhovsky
  • 3,610
  • 2
  • 31
  • 56
-5
votes
1 answer

Difference between void * and void **

Other than being able to dereference a void**, I don't understand the following: void * foo, **bar; foo++;//error bar++;//no error Why doesn't the first work but the second does? What's the difference?
shinzou
  • 5,850
  • 10
  • 60
  • 124
-6
votes
1 answer

C++ Pointer arithmetic. No Operator "+" Matches these operands

I'm trying to preform a Deep Copy of one class to another. Using VS2015. below on *(clsOriginalToCopy + lngIndex); is where I get the error, I am at a loss. for (lngIndex = 0; lngIndex < lngSize; lngIndex += 1) { *(this + lngIndex) =…
Adam Schneider
  • 275
  • 1
  • 5
  • 18
1 2 3
47
48