1

I found this code in the internet for adding two numbers using pointers. couldn't understand how it is working? Any help would be appreciated.

#include <stdio.h>
#include <conio.h>
int main()
{
      int  a,b,sum;
      char *p;
      printf("Enter 2 values : ");
      scanf("%d%d",&a,&b);
      p = (char *)a; // Using pointers
      sum = (int)&p[b];
      printf("sum = %d",sum);
      getch(); 
      return 0;
}
5
  • 6
    It isn't working; it invokes undefined behaviour Commented Apr 17, 2015 at 18:38
  • Its working fine in my computer I m using Dev IDE. might be because of scanf("%lu%lu,&a,&b) change it to scanf("%d,%d",&a,&b) Commented Apr 17, 2015 at 18:39
  • its a pointer related question thats why added c++ tag Commented Apr 17, 2015 at 18:42
  • It's perfectly good C code. It doesn't contain anything C++ specific. Never, ever, never abuse pointers like this. Commented Apr 17, 2015 at 18:44
  • ok got it remember next time while posting question Commented Apr 17, 2015 at 18:46

3 Answers 3

5

The following line interprets the value in a as an address:

p = (char *)a; 

&p[b] is the address of the b th element of the array starting at p. So, as each element of the array has a size of 1, it's a char pointer pointing at address p+b. As p contains a, it's the address at p+a.

Finally, the following line converts back the pointer to an int:

 sum = (int)&p[b];    

But needless to say: it's a weird construct.

Additional remarks:

Please note that there are limitations, according to the C++ standard:

5.2.10/5: A value of integral type (...) can be explicitly converted to a pointer.

5.2.10/4: A pointer can be explicitly converted to any integral type large enough to hold it.

So better verify that sizeof(int) >= sizeof(char*).

Finally, although this addition will work on most implementations, this is not a guaranteed behaviour on all CPU architectures, because the mapping function between integers and pointers is implementation-defined:

A pointer converted to an integer of sufficient size (if any such exists on the implementation) and back to the same pointer type will have its original value; mappings between pointers and integers are otherwise implementation-defined.

Sign up to request clarification or add additional context in comments.

1 Comment

table?? it should be array right ? and what about the size??
2

First a is converted to a pointer with the same value. It doesn't point to anything really, it's just the same value.

The expression p[b] will add b to p and refer to the value at that position.

Then the address of the p[b] element is taken and convert to an integer.

2 Comments

so is does the size factor doesnt come into play when finding the sum i.e. sum = (int)&p[b]
The size of what? The size of int and char*: If the pointers are 64-bit, the int type is still probably 32-bit, so that will be the limiting factor. The size of p[b]: Since p is char*, p[b] refers to p+b*sizeof(char). If p was int*, the p[b] would refer to p+b*sizeof(int).
2

As commented, it is valid, but horrible code - just a party trick.

p = (char *)a;

p takes the value of a entered as a supposed address.

sum = (int)&p[b];

the address of the bth element of a char array is at p + b.

Since p == a (numerically), the correct sum is obtained.

To take a worked example, enter 46 and 11.

p = (char *)a;            // p = 46
sum = (int)&p[b];         // the address of p[b] = 46 + 11 = 57

Note: nowhere is *p or p[b] written or read, and size does not matter - except for the char array, where pointer arithmetic is in units of 1.

2 Comments

Thanks, after seeing this I understood I just magically interpolated the line p = (char*)a; as p = (char*)&a;
@JonathanMee that's how I saw it at first, and considered swapping the order of a and b declarations to break it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.