pointers to strings

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
slacker

pointers to strings

Post 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]?
rdragon

Re:pointers to strings

Post 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 ;)
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:pointers to strings

Post 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 :)
-- Stu --
proxy

Re:pointers to strings

Post 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
BI lazy

Re:pointers to strings

Post 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.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:pointers to strings

Post 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 ...
Whatever5k

Re:pointers to strings

Post by Whatever5k »

Pype.Clicker wrote: - if you want to do a memcopy, use memcpy ...
Hehe, dito...
proxy

Re:pointers to strings

Post 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
proxy

Re:pointers to strings

Post 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
BI lazy

Re:pointers to strings

Post 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...
proxy

Re:pointers to strings

Post 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
Post Reply