and a pointer like char *pntr;
Code: Select all
*pntr=str[0];
*pntr++;
Code: Select all
*pntr=str[0];
*pntr++;
slacker wrote: if i have a string like char str[5];
and a pointer like char *pntr;
*pntr=str[0];
*pntr++;
why doesn't the pointer point to str[1]?
Code: Select all
char str[5];
char* ptr;
ptr = str; // or ptr = &str[0];
ptr++;
//now ptr == &str[1]
its a simple matter of data types you have confused.
if you have
char str[5];
and
char* ptr;
the data type of the expression:
str is char*
ptr is char*
str[0] is char
&str[0] is char*
*ptr is char
the *ptr++ means increment value pointed to by ptr, rather than incrememnt pointer.. you are incrementing data...slacker wrote: if i have a string like char str[5];
and a pointer like char *pntr;
*pntr=str[0];
*pntr++;
why doesn't the pointer point to str[1]?
sorry but that statement is terribly wrong. the increment operator has precidence (goes before the dererence operator), however, it returns the value of ptr before it was incremented.the *ptr++ means increment value pointed to by ptr, rather than incrememnt pointer.. you are incrementing data...
Code: Select all
int _same_as_post_increment(int *x) {
int temp = *x;
*x = temp + 1;
return temp;
}
Code: Select all
for(i = 0; i < size; ++i) {
*ptr1++ = *ptr2++;
}
Code: Select all
Code: Select all
ptr[0]++;
Hehe, dito...Pype.Clicker wrote: - if you want to do a memcopy, use memcpy ...
no offence, but you need to read my post more carefully...it clearly states that the ++ operator returns the value of the object _before_ it was incremented (look at my example code i provided for an implementation).no, proxy, in this case, it goes after dereferencing the pointer.
*x++=1;
this means:
1. move 1 to memory location, x points to.
2. increment the ADRESS x holds by the size of its data type.
Otherwise, your nice memory copy funtion wouldn't do the work correctly i s'pose.
just to clear up what actually happens here..*x++=1;
this means:
1. move 1 to memory location, x points to.
2. increment the ADRESS x holds by the size of its data type.
Code: Select all
#include <stdio.h>
main()
{
int count = 0, loop;
loop = ++count; /* same as count = count + 1; loop = count; */
printf("loop = %d, count = %d\n", loop, count);
loop = count++; /* same as loop = count; count = count + 1; */
printf("loop = %d, count = %d\n", loop, count);
}
it is a subtle differnce, but it is thereloop = 1, count = 1
loop = 1; count = 2