Page 1 of 1

pointers to strings

Posted: Wed Dec 31, 2003 1:18 pm
by slacker
if i have a string like char str[5];
and a pointer like char *pntr;

Code: Select all

*pntr=str[0];
*pntr++;
why doesn't the pointer point to str[1]?

Re:pointers to strings

Posted: Wed Dec 31, 2003 2:17 pm
by rdragon
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
review pointers ;)

Re:pointers to strings

Posted: Wed Dec 31, 2003 2:43 pm
by df
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]?
the *ptr++ means increment value pointed to by ptr, rather than incrememnt pointer.. you are incrementing data...

:) review basic pointer tutorial please at earliest conveniance :)

Re:pointers to strings

Posted: Thu Jan 01, 2004 5:50 pm
by proxy
the *ptr++ means increment value pointed to by ptr, rather than incrememnt pointer.. you are incrementing data...
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.

you can think of the ++ on the right side of an object as being implemented like this: (lets say that x is an int)

Code: Select all

int _same_as_post_increment(int *x) {
int temp = *x;
*x = temp + 1;
return temp;
}
this is also why the pre-increment operator is more correct when you want to increrment as a stand alone operation or if you want to increment before it is used in the statement.

for proof, i have countlessly done memory moves like this:

Code: Select all

for(i = 0; i < size; ++i) {
    *ptr1++ = *ptr2++;
}
this will copy size elements from ptr2 to ptr1.

proxy

Re:pointers to strings

Posted: Fri Jan 02, 2004 12:38 am
by BI lazy
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.

Re:pointers to strings

Posted: Fri Jan 02, 2004 1:51 am
by Pype.Clicker
that's one of the reason that explains why i do not like to write *x++ code, and that i get scared when i see it in other people's code.

- if you want to move a pointer to the next element, do ptr++.
- if you want to increment the element under a pointer, use (*ptr)++ or

Code: Select all

ptr[0]++;
- if you want to do a memcopy, use memcpy ...

Re:pointers to strings

Posted: Fri Jan 02, 2004 8:53 am
by Whatever5k
Pype.Clicker wrote: - if you want to do a memcopy, use memcpy ...
Hehe, dito...

Re:pointers to strings

Posted: Sun Jan 04, 2004 12:35 pm
by proxy
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.
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).

the ++ _always has higher priority than assignment_, i'm looking at my C standard book right now. the memcopy works because the ++ is returning the original value, not the incremented value.

if you still dont beleive me, look at this table showing operator precidence, not much is higher than ++..

http://www.cppreference.com/operator_precedence.html

Re:pointers to strings

Posted: Sun Jan 04, 2004 12:39 pm
by proxy
*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.
just to clear up what actually happens here..

1. store ADDRESS x in a temp var
2. increment the ADRESS x holds by the size of its data type.
3. move 1 to memory location, temp var points to.

proxy

Re:pointers to strings

Posted: Sun Jan 04, 2004 1:03 pm
by BI lazy
You are sure you are to tell me what to do and what not?

Be honest: does it change the result as I describe it?

Nitpicking this is...

Re:pointers to strings

Posted: Sun Jan 04, 2004 1:13 pm
by proxy
sorry if i came off offensive, didn't intend to be. I realize that this is nitpicking, but this is also a public forum, which makes giving correct information slightly more important to me.

a classic example, of why the operators if different is this:

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);
   }
the output would be as follows:
   loop = 1, count = 1
   loop = 1; count = 2
it is a subtle differnce, but it is there :)

Now, fortunately for us, with basic types (char, int, etc) gcc is smart enough to re-order the increment to a point where no temp is needed (even with optimizations off it seems to do this), but the fact remains, according to the standard, the ++ always goes first.

Anyway, I appologize if i came off obnoxious or somthing like that :)

proxy