tags:

views:

137

answers:

6

ok so if we have a char *hello - ands the string is "hello"

and i do

char *ptr ;
ptr = hello;

then ptr will be pointing at 'h', correct?

Now i have just done an assignmnet in this and completed it using the following terms

if i wanted to move the pointer to the next chatachter i would just do ptr++. If i wanted to use the value of the pointer for some check, i would use if(*ptr == '\0')...

When i was doing the assignmnets our teacher gave us some pre built methods, and they used stuff like

*string++ = *s++;

ok, so why would we want to do *string (which gets a value) - and combine it with ++

i hope i make sence in explaining what is not clear. Its just i managed ot do the whole assignmnet with ptr++ to move to next element or *ptr to check its value

thanks

+4  A: 

Not quite. In your original question, ptr would be set to point to the same first character as hello, which may not necessarily be h. You may have done:

char *hello = "goodbye";

in which case both hello and ptr will point to that g. However your edit now makes it clear that you meant:

char *hello = "hello";

Your comment on ptr++ is correct. It will move ptr so that it points to the next character and, as per C string handling convention, '\0' marks the end of the string.

The statement:

*string++ = *s++;

is something you often see in string copying code (copying s to string), something like:

char *mystrcpy (char *d, char *s) {
    char *d2 = d;
    while (*s != '\0')
        *d2++ = *s++;
    *d2 = '\0';
    return d;
}

In this case, the line *d2++ = *s++; means:

  • copy the character at s to the memory location d2.
  • increment d2 to point to the next destination character.
  • increment s to point to the next source character.

In other words,

*string++ = *s++;

is functionally identical to:

*string = *s;
string++;
s++;
paxdiablo
made a mistake - i wanted a *char called hello which represented the string "hello"
leo
+5  A: 

The idiom *s++ means "take the value pointed to, and switch to the next one". This way you can do your check operations in a loop. The assignment *p++ = *q++ copies the value of *q to the place pointed by p, and shifts both p and q to the next place, so the next time you execute *p++ = *q++ the next character will be copied behind the first one. And so on.

Vlad
@Vlad, you had your p/q round the wrong way. Hope you don't mind the fix.
paxdiablo
@paxdiablo: thanks!
Vlad
A: 

then ptr will be pointing at h, correct?

Yes. No. See @paxdiablo's answer.

*string++ = *s++;

This statement can be viewed as

*string = *s;
string ++;
s ++;

That means:

  1. Copy the character in s to the location pointed by string.
  2. Move both s and string to the next character.
KennyTM
+1  A: 

so why would we want to do *string(which gets a value) - and combine it with ++

When *string is on the left hand side of the equals, it doesn't get the value, it sets the value.

The statement *string++ = *s++; is equivalent to:

*string = *s;
s++;
string++;

This is because x++ is the postfix increment operator (as opposed to ++x which is the prefix increment operator). The pointer x is updated but the value that x was originally pointing to is used in the expression.

Personally I'd say that the one-liner is more confusing to read and you should generally try to avoid complex expressions with side-effects. But in this case it's a fairly standard idiom in C, so you might as well get used to it.

Mark Byers
so i can achieve the same results with my broken down stages version ?
leo
Yes, you don't have to do it all on one-line - you can split it up. But you need to be very careful to understand how the pre- and postfix increment operators work when dealing with this type of code. It's easy to make an error.
Mark Byers
thanks Mark, i am working hard at this to understand it, at least the C module is more understandable than the Haskell one :D
leo
A: 

then ptr will be pointing at h, correct?

Yes, correct.

ok, so why would we want to do *string(which gets a value)

The '*' doesn't necessarily get a value: it may set a value, depending on which side of the '=' sign it is.

char *ptr;
ptr = "hello";
char firstLetter = *ptr; //get the value: now firstLetter contains 'h'
*ptr = 'w'; //set the value: now ptr is pointing to "wello".

Combining * with ++ means that you do one after the other:

  • On the right-hand side, '*s++' means "get the value to which s is pointing, and then increment the s pointer".
  • On the left-hand side, '*string++' means "set the value to which string is pointing, and then increment the string pointer".

Combining them into a single statement simply a shorter, more compact (but functionally equivalent) way to write:

*string = *s;
string++;
s++;
ChrisW
A: 

Just a quick addition to Mark Byers' answer, who correctly points out the postfix increment ("a++") as opposed to the prefix increment ("++a").

You probably want to have a look at the C operator precedence for the order in which operators are handled in an expression.

You can always force the precedence you want by using parentheses: (a + b) * c != a + b * c.

Patrick