Meaning for *p = +*q++

S

sworna vidhya

Hai,

When viewing some queries of this group, i came across a function
which is similar to strcpy. The snip of the code is as follows:

#include <stdio.h>
#include <stdlib.h>

void myfunc(char *p, char *q)
{
while (*p = +*q++)
*p++;
}

void main()
{ char *str;
char *str1 = "apple";

str = NULL;
str = (char *) calloc (sizeof (str),sizeof (char *) * sizeof(str1));
myfunc(str,str1);

printf("\n\t Str : %s \n\t Str1 : %s \n",str,str1);
}

1. How the functionality process here?
while (*p = +*q++)
*p++;

2. Also, i noticed that the same query also contains the same function
can be written in the other way as follows:

void myfunc(char *p, char *q)
{
while (*(p++) = (*q)++);
}

But when i compile i found no error.

While running, i am getting exception with application error. Why i am
getting so?

Kindly clarify my doubts.

Thanks and Regards,
M.Sworna Vidhya
 
S

Stephen Sprunk

sworna vidhya said:
2. Also, i noticed that the same query also contains the same function
can be written in the other way as follows:

void myfunc(char *p, char *q)
{
while (*(p++) = (*q)++);
}

But when i compile i found no error.

While running, i am getting exception with application error. Why i am
getting so?

You need to use *(q++) instead of (*q)++.

S
 
J

Juergen Heinzl

Hai,

When viewing some queries of this group, i came across a function
which is similar to strcpy. The snip of the code is as follows:

#include <stdio.h>
#include <stdlib.h>

void myfunc(char *p, char *q)
{
while (*p = +*q++)
[-]
Useless unary "+" operator -> +*q++ <- here.
*p++;
}

void main()
[-]
main() isn't void.
{ char *str;
char *str1 = "apple";

str = NULL;
str = (char *) calloc (sizeof (str),sizeof (char *) * sizeof(str1));
[-]
Useless cast and calloc (sizeof (pointer to char),
sizeof (pointer to char) * sizeof (pointer to char))
probably isn't what you want, either.
myfunc(str,str1);

printf("\n\t Str : %s \n\t Str1 : %s \n",str,str1);
}

1. How the functionality process here?
while (*p = +*q++)
*p++;
[-]
It's a, as far as the copying is concerned, shorter form of ...
while (*p = *q) {
p++;
q++;
}
.... which is a shorter form of ...
*p = *q;
while (*p != '\0') {
p++;
q++; *p = *q;
}
....
2. Also, i noticed that the same query also contains the same function
can be written in the other way as follows:

void myfunc(char *p, char *q)
{
while (*(p++) = (*q)++);
[-]
(*q)++ means "increment the value at q".
}

But when i compile i found no error.
[-]
Sure, as syntactically what you wrote is fine.

Cheers,
Juergen
 
F

Fred L. Kleinschmidt

sworna said:
Hai,

When viewing some queries of this group, i came across a function
which is similar to strcpy. The snip of the code is as follows:

#include ?stdio.h?
#include ?stdlib.h?

void myfunc(char *p, char *q)
{
while (*p = +*q++)
*p++;
}

void main()

main() returns int, not void
{ char *str;
char *str1 = "apple";

str = NULL;
str = (char *) calloc (sizeof (str),sizeof (char *) * sizeof(str1));

improper use of calloc( number_of_elements, size_of_each_element )
First, the cast just hides the fact you have not included stdlib.h.
Second, the args are wrong. The number of elements in the new str array
is probably not sizeof(str), but rather (strlen(str1) + 1).
The size per element is the size of a char, which is 1.
myfunc(str,str1);

printf("\n\t Str : %s \n\t Str1 : %s \n",str,str1);
}

1. How the functionality process here?
while (*p = +*q++)
*p++;

2. Also, i noticed that the same query also contains the same function
can be written in the other way as follows:

void myfunc(char *p, char *q)
{
while (*(p++) = (*q)++);
^^^^^^
*(q++)

myfunc() is an implementation of strcpy().
 
R

Rakesh Kumar

Juergen said:
void myfunc(char *p, char *q)
{
while (*p = +*q++)

[-]
Useless unary "+" operator -> +*q++ <- here.
*p++;
}

void main()

[-]
main() isn't void.
{ char *str;
char *str1 = "apple";

str = NULL;
str = (char *) calloc (sizeof (str),sizeof (char *) * sizeof(str1));

[-]
Useless cast and calloc (sizeof (pointer to char),
sizeof (pointer to char) * sizeof (pointer to char))
probably isn't what you want, either.
Just a bit curious, without calloc how can we access the memory
address pointed to, by p ( as in 'myfunc' function ) .
 
J

John Bode

[snip]
1. How the functionality process here?
while (*p = +*q++)
*p++;

while (*p = +*q++)
*p++;

Take the value at the location pointed to by q, apply the unary + operator
to that value (i.e., make it positive), write that value to the location
pointed to by p, and increment q. Increment p (*not* the value p points
to). Repeat this as long as the result of *p = +*q++ is not zero (that is,
until *q evaluates to 0).
2. Also, i noticed that the same query also contains the same function
can be written in the other way as follows:

void myfunc(char *p, char *q)
{
while (*(p++) = (*q)++);

You mean *(p++) = *(q++). (*q)++ attempts to increment the value that q
points to, not the pointer itself. Since *q++ evaluates the same as *(q++),
you don't need the parentheses.
}

But when i compile i found no error.

Just because it compiles doesn't mean it's right.
While running, i am getting exception with application error. Why i am
getting so?

Because you are attempting to increment the value that q points to, not q
itself. Instead of walking through the string that q points to, you're
incrementing the first character in it and running past the end of your
target array.

For example, if q points to the string "hello", and p points to a buffer 10
characters long, this is what happens:

1: p[0] = 'h';
2: p[1] = 'i';
3: p[2] = 'j';
4: p[3] = 'k';
5: p[4] = 'l';
6: p[5] = 'm';
7: p[6] = 'n';
8: p[7] = 'o';
9: p[8] = 'p';
10: p[9] = 'q';
11: p[10] = 'r';

At this point you've gone past the end of your target buffer, which is
probably where your app aborts. If you had written it *p++ = *q++, then the
following would happen:

1: p[0] = 'h';
2: p[1] = 'e';
3: p[2] = 'l';
4: p[3] = 'l';
5: p[4] = 'o';
6: p[5] = 0;

at which point the loop terminates, because the result of the expression
*p++ = *q++ is 0.
 
J

Jack Klein

[snip]
1. How the functionality process here?
while (*p = +*q++)
*p++;

while (*p = +*q++)
*p++;

Take the value at the location pointed to by q, apply the unary + operator
to that value (i.e., make it positive), write that value to the location

No, it does not make it positive. The unary + operator does not
change the sign of an arithmetic type, just as the unary - operator
always changes the sign of an arithmetic type. The unary + operator
is a no-op, provided only for uniformity with the unary - operator.

Program:

#include <stdio.h>

int main(void)
{
int pos = 3;
int neg = -3;

printf("With unary plus: +pos = %d, +neg = %d\n",
+pos, +neg);
printf("With unary minus: -pos = %d, -neg = %d\n",
-pos, -neg);
return 0;
}


Output:

With unary plus: +pos = 3, +neg = -3
With unary minus: -pos = -3, -neg = 3
 
B

Ben Pfaff

Jack Klein said:
No, it does not make it positive. The unary + operator does not
change the sign of an arithmetic type, just as the unary - operator
always changes the sign of an arithmetic type. The unary + operator
is a no-op, provided only for uniformity with the unary - operator.

Unary + is a no-op with respective to an expression's value. It
does, however, have the effect of yielding a result that is not
an l-value, which is occasionally useful. The same effect can be
obtained other ways, but unary + is very concise.
 
O

Old Wolf

John Bode said:
while (*p = +*q++)
*p++;

Take the value at the location pointed to by q, apply the unary + operator
to that value (i.e., make it positive),

The unary + operator has no effect; if *q is negative, then so is +*q.
 
J

John Bode

Jack Klein said:
[snip]
1. How the functionality process here?
while (*p = +*q++)
*p++;

while (*p = +*q++)
*p++;

Take the value at the location pointed to by q, apply the unary + operator
to that value (i.e., make it positive), write that value to the location

No, it does not make it positive. The unary + operator does not
change the sign of an arithmetic type, just as the unary - operator
always changes the sign of an arithmetic type. The unary + operator
is a no-op, provided only for uniformity with the unary - operator.

That should learn me to talk about operators I never actually use. You're
absolutely right; unary + is just shorthand for "0 +", which would not
result in a sign change.

Sorry 'bout that.
 
C

Christian Bau

Hai,

When viewing some queries of this group, i came across a function
which is similar to strcpy. The snip of the code is as follows:

#include <stdio.h>
#include <stdlib.h>

void myfunc(char *p, char *q)
{
while (*p = +*q++)
*p++;
}
void myfunc(char *p, char *q)
{
while (*(p++) = (*q)++);
}

But when i compile i found no error.

While running, i am getting exception with application error. Why i am
getting so?

Try to answer the following two questions:

1. What is the difference between *q++ and +*q++ ?
2. What is the difference between *q++ and (*q)++ ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top