explain the below program.....

Ashokkumar01cbe

Broken In
this is a program to add two numbers without using arithmetic operators. I found this program in the web ,but i am unable to understand ,please explain me
the 7th and 8th steps ie..how the p and sum values are assigned.

#include"stdio.h"
#include"conio.h"

void main()
{
int a,b,sum;
char *p;
clrscr();
printf("Enter 2 values : ");
scanf("%d%d",&a,&b);
p = (char *)a; //7th step
sum = (int)&p; //8th step
printf("\nSum : %d",sum);
getch();
}
:confused:
 

gameranand

Living to Play
You see I discussed it one of my friend Saleem and were able to explain it.

In this program we are type casting two times. Now we are taking two variables from user and then you are typecasting one of them to character to which pointer p is pointing to (step 7). Now in next step we are now typecasting character pointer in integer and forcing it to store b. So what it basically does is that it adds the two and show you the result.

I know that I can be confusing so if you face problem understanding it feel free to ask.
 

gameranand

Living to Play
This Program gives errors and warning if complied in a g++ compiler so its a wrong program. If anyone wants explanation then report back and I'll try to make you understand.
 

gameranand

Living to Play
I tried compiling in Fedora and it gave errors although it ran fine on codeblocks.

Let me explain. You are typecasting a integer into a character. Now integer occupys 2 bytes while char occupys 1 byte. So its not efficient. Try running it in linux and you'll get errors.
 

nbaztec

Master KOD3R
Ok, this is retarded.

Anyone found giving that example as h/w should be shot in the loins.

First the explanation part, and to understand that, you need to have a brief idea of what pointers are:
char *p = "Hello" is stored as: |H|e|l|l|o|
here p points to the first memory location which can be any long type integer. Suppose it's 1337. Then p+1 = 'e', p+2 = 'l', etc. It's just relative addressing from there on.
If this were an int the increments would be 2 words, albeit (p+1) would give the next int location, nonetheless.

So here's what this retard did:

1. Input 2 integers: say 1337 & 3
2. Point to location 1337 (p of type char*)
3. Get address of p -> Here's the catch: array[index] can also be written as *(array+index) since the array name points to the first element of the array.
So, tada! you get &(*array+index), which after cancelling reference & deference comes out to be: (array + index) = 1337 + 3
4. This is cheating (apart from being retarded), he used arithmetic operator, albeit implicitly.

@gameranand, the code is perfectly valid, unless you're reading the value of the memory. It should not crash on gcc.


If I were to do it, I'd do it the PROPER way:

Code:
int main(int argv, char **argc)
{
	int a=1337,b=10,sum;
	short _a = 0, _b = 0, c = 0;
	short mask=1;
	short s=0;
	unsigned int bits = 2147483648;
	while(bits >>= 1)
	{
		_a = a & mask;
		_b = b & mask;
		s |= ((_a ^ _b) ^ c) & mask ;
		mask <<= 1;
		c |= ((_a & _b) | (c & (_a ^ _b))) ? mask: 0;
	}
	printf("%d\n", s);
	return 1;
}
 
Last edited:

gameranand

Living to Play
Well but this program clearly gave two errors and 2 warning in Fedora. Though it ran on Codeblocks.
 

RahulB

Journeyman
this is a program to add two numbers without using arithmetic operators. I found this program in the web ,but i am unable to understand ,please explain me
the 7th and 8th steps ie..how the p and sum values are assigned.

#include"stdio.h"
#include"conio.h"

void main()
{
int a,b,sum;
char *p;
clrscr();
printf("Enter 2 values : ");
scanf("%d%d",&a,&b);
p = (char *)a; //7th step
sum = (int)&p; //8th step
printf("\nSum : %d",sum);
getch();
}
:confused:


Pains me when people write non-standard code
 
Top Bottom