difference b/w, wordcnt++ and wordcnt+1

Thread Starter

engrrehmat

Joined Dec 17, 2010
26
what is the difference between wordcnt++ and wordcnt+1, both are increment, but i am confused with their operations.
any one kindly make me understand how they both works and what is the output, the output is different i have tried.
 

tshuck

Joined Oct 18, 2012
3,534
what is the difference between wordcnt++ and wordcnt+1, both are increment, but i am confused with their operations.
any one kindly make me understand how they both works and what is the output, the output is different i have tried.
wordcnt++ is a post increment and will result in the value changing after the line it is on. Moreover, it returns a new object with the copied value, meaning it will be slower compared with ++wordcnt(pre-increment), wich returns the current(incremented) object's value...

wordcnt+1 returns the value of...well, exactly that. it doesn't affect the value left in wordcnt.

This is most likely your problem, if you want specific help, post a code snippet...
 

Thread Starter

engrrehmat

Joined Dec 17, 2010
26
in this program, it works fine, but when i replace wordcnt+1 with wordcnt++, it doesnt count the exact number of words.

int main()
{
int charcnt=0;
int wordcnt=0;
char ch;

printf("type in a phrase:");
while((ch=getche())!='\r')
{
charcnt++;
if(ch==' ')
wordcnt++;
}
printf("\nis:%d",charcnt);
printf("\nis:%d",wordcnt++);
getch();
}
 

tshuck

Joined Oct 18, 2012
3,534
You need to use CODE tags, it is in the toolbar and looks like '#':
Rich (BB code):
printf("\nis:%d",wordcnt++);
Like I said, this is a post-increment, wordcnt doesn't get incremented until after it's value is printed....
 

Papabravo

Joined Feb 24, 2006
21,159
The post increment operator is more general than the binary(two operands, not base 2) addition operator. If the object is a pointer to char, the value of the pointer is increased by 1. If it is a pointer to int then it is increased by sizeof(int). If it is a structure then it will increase by the sizeof(struct).
 

WBahn

Joined Mar 31, 2012
29,979
The post increment operator is more general than the binary(two operands, not base 2) addition operator. If the object is a pointer to char, the value of the pointer is increased by 1. If it is a pointer to int then it is increased by sizeof(int). If it is a structure then it will increase by the sizeof(struct).
No, they are equivalent in that sense.

If p is a pointer to a value of type double, then p++, p+=1, and p=p+1 all have the same effect. The sum of a pointer and an integer evaluates to the value of the pointer incremented by the value of the integer (which can be negative) multiplied by the size of the object pointed to by p. This is why you can't do pinter arithmetic with void pointers.

A compiler is free to translate array dereferences of the form a to the form *(a+b), thus, because addition is commutative, the following is perfectly valid code:

int a[3];
int i;

i=0;
i[a] = 0[a] = 3;
(++i)[a+1] = 4;
 

takao21203

Joined Apr 28, 2012
3,702
This is why you can't do pinter arithmetic with void pointers

1. Why would you want to? Without a typecast the compiler can not know or assume what this pointer is about.

The standard things work in C in a well understood way but if you want to do something obscure, often you have to use many brackets to control the order, and you'll need typecasting as well.

There might be a way to define the behaviour in a regular way but it might be akward and even more obscure.

At first, you need to understand perfectly micro step for micro step what you want to do, what is really going to happen, and why.

Microstep is somehow similar to assembler- an atomic action that can not be split up or divided up further.

Over the years some people would understand a little about the internals of a C compiler. The MSDN documentation is quite good even if not extensive that much- it mentions the correct technical terms.

If you spell a complex C line, it will result in quite a lot atomic operations. They will of course happen in some order, and depending on the nature of the spell, different order may give a different result (or not).

If often helps to break up C expressions into multiple lines to find a mistake.
 

tshuck

Joined Oct 18, 2012
3,534
This is why you can't do pinter arithmetic with void pointers

1. Why would you want to? Without a typecast the compiler can not know or assume what this pointer is about.
That was the point. WBahn was saying that an increment on a pointer will change the object pointer to point at the correct location for the(possible) next object of that type because the pointer type is known...

As far as the rest of that....why?:confused:
 

takao21203

Joined Apr 28, 2012
3,702
That was the point. WBahn was saying that an increment on a pointer will change the object pointer to point at the correct location for the(possible) next object of that type because the pointer type is known...

As far as the rest of that....why?:confused:
I always consider threads like this one as interesting.

How little did I understand when I started to use interpreted BASIC.
How little terminology did I know. Next to nothing.
How primitive my programs were.

So if you look the MSDN reference, it can not really be said all this would be obvious to almost anyone.
 

WBahn

Joined Mar 31, 2012
29,979
This is why you can't do pinter arithmetic with void pointers

1. Why would you want to? Without a typecast the compiler can not know or assume what this pointer is about.
You shouldn't, that's the point. But many people (myself included, at one point) assume that a void pointer is just a pointer that can point to anything -- sort of some kind of catch all generic pointer. It is not unreasonable, then, to further assume that, being the most basic general catchall type of pointer, that a void pointer simply works directly with byte addresses. Many people encounter void pointers about the same time they encounter sizeof() and learn that the sizeof(int) is not the same on all machines. So it is easy to assume that a void pointer is an implementation independent pointer that you can always count on the behavior of. It's only after you understand pointers and how to use them quite a bit more thoroughly that you understand what a void pointer is and what it is not.
 
Top