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

stripping end of a string in C

I need a function to remove ) character from end of a string. for example, hello,world) should be converted to hello,world. I have written this : char *a="hello,world)"; int a_len=strlen(a); *(a+a_len-1)='\0'; printf("%s", a); but nothing is shown…
alireza_fn
  • 884
  • 1
  • 8
  • 21
-2
votes
1 answer

Errors involving pointer functions in arithmetic

Here are my errors: postfix.cpp: In function ‘int main()’: postfix.cpp:32: warning: pointer to a function used in arithmetic postfix.cpp:87: warning: pointer to a function used in arithmetic postfix.cpp:93: warning: pointer to a function used in…
TheNameHobbs
  • 679
  • 7
  • 21
  • 36
-3
votes
2 answers

How pointer arithmetic works

int a=10; int *pa; pa=&a; int b=5; *pa++=b++; printf("%d %d", *pa,b); This gives the output: 6 6 whereas the following code snippet: int a=10; int *pa; pa=&a; int b=5; *pa++=b++; printf("%d %d", a,b); gives the output : 5 6 why is this so…
-3
votes
3 answers

Passing the address of a dereferenced pointer to a string offset

Assuming there is a function like this int foo (char** str, int x) { char* p = *str + x; foo2(&p); // declared as int foo2 (char** ); } (oversimplified of course, the real function is recursive and much more complicated) I've tried to do…
Edenia
  • 2,312
  • 1
  • 16
  • 33
-3
votes
3 answers

Does (p+x)-x always result in p for pointer p and integer x in gcc linux x86-64 C++

Suppose we have: char* p; int x; As recently discussed in another question, arithmetic including comparison operations on invalid pointers can generate unexpected behavior in gcc linux x86-64 C++. This new question is specifically about the…
personal_cloud
  • 3,943
  • 3
  • 28
  • 38
-3
votes
1 answer

Getting a pointer to a memory address from a different pointer C

I'm writing my own memory allocater. I'm getting a pointer to mapped memory from mmap. From there, I want to be able to get a pointer to a different part of that mapped memory (like x bytes away from the current pointer). How can I do this? Memory…
-3
votes
1 answer

Pointer Arithmetic with a pointer to a class array

I'm having issues with this code for a bag ADT. I'm using a a pointer array made of a separate class that stores a string and an int. ArrayBag::ArrayBag(int length){ list = new ReceiptArray [length]; size = length; n= 0; } int…
Eli D.
  • 5
  • 1
  • 4
-3
votes
4 answers

What is the answer when integer added to string constant in C language?

char c=8+"ab"; //output is c=112 with warning int c=8+"ab"; //output is c=4195952 with warning char c=8+"a"; //output is c=112 (same as many caharacters in string constants) with warning How is c value computed here? Please explain.
-3
votes
1 answer

Pointer Arithmetics and Char array

I am having trouble understanding the following code. Although I tried to debug it for some time it seems that I can't understand the exact mechanics of the code and I am feeling stuck. Any help will be deeply appreciated. edit: Well my problem is…
palser
  • 3
  • 2
-3
votes
1 answer

Pointer access beyond allocated memory does'nt cause segfault

I'm teaching myself C, and I do not understand why the following code do not break with a segmentation fault. printf("loading \n"); conn->db = malloc(sizeof(struct Database)); int rc = fread(&conn->db->max_rows,sizeof(int) , 1, conn->file); rc =…
tumasgiu
  • 325
  • 3
  • 11
-3
votes
3 answers

C++ vector reference conversion

The following code refuses to compile. The compiler is complaining of conversion from the vector reference to an int pointer, but I don't understand why. Is there some kind of a solution to this problem? bool inSortedVectorHelper(int* front, int*…
Tyler
  • 27
  • 1
  • 5
-3
votes
1 answer

Why am I able to access array elements "outside" of its range?

I have a strange dilemma of sorts. I have been working with dynamically created arrays and pointer arithmetic in C/C++ for the past couple of weeks and find it very interesting. However, I noticed something "bad" that I'm kind of scared of. Here's…
nixhex
  • 15
  • 2
-3
votes
1 answer

Tweaking with unassigned memory

I am trying to do following: char c[] = "programming"; char *p; *(c-1)='l'; *(c-2)='l'; *(c-3)='l'; *(c-4)='l'; *(c-5)='l'; p=&c[0]; cout<<*(c-1); This prints l only if I omit p=&c[0]; why is that so? There is no relation apparently between p…
-3
votes
4 answers

How is this output coming?

#include #include int main() { char str_a[20]; char *pointer; char *pointer2; strcpy(str_a, "Hello, world!\n"); pointer = str_a; printf(pointer); pointer2 = pointer + 2; printf(pointer2); strcpy(pointer2, "y you…
-4
votes
2 answers

Why can void pointers be subtracted but not added?

Why does printf("%ld\n", (void *)0 - (void *)0); compile, but printf("%ld\n", (void *)0 + (void *)0); does not?
Sapphire_Brick
  • 1,560
  • 12
  • 26
1 2 3
47
48