0

I wrote the following code, there is some difference exist but I can't figure it out.

typedef struct {
    char* ch;   
    int length;
}Hstring;
int Strlength(Hstring* s) {
    return s->length; // if I use s.length there, error occurs.
}
int Strlength(Hstring s) {
    return s.length; // if I use s->length there, error occurs.
}

So what the differences between the two types? Will I get the same result? and why those errors occur?

yupe
  • 117
  • 1
  • 10
  • 3
    Practically? There's really no difference between the two functions. And if you wonder about the difference between `.` and `->` to access structure members (which seems to be your big problem), note that one argument is a *pointer* and the other is *not*. And reread whatever text-book you have. – Some programmer dude Oct 24 '19 at 07:16
  • 2
    Also see [Arrow operator (->) usage in C](https://stackoverflow.com/questions/2575048/arrow-operator-usage-in-c/2575050), and a ton of similar questions here on SO. – Some programmer dude Oct 24 '19 at 07:18
  • @Some programmer dude OK, it looks like they are rules needed to be remembered. but I still have a little question, what the meaning of the name of a struct variable(is it a pointer to itself or meaningless)? – yupe Oct 24 '19 at 07:34

4 Answers4

3

To add to the previous answers that state that dot (.) is for 'normal' variables and arrow (->) is for pointers, note that arrow is a syntactic equivalent to a pointer de-reference followed by a dot, provided for convenience (since it's such a common operation).

Hstring* s;
s->length;   // this is equivalent to...
(*s).length; // ...this

The parentheses are needed since dot has a higher precedence than star. Without them, you'd be a) using a dot with a pointer and b) trying to de-reference the integer length field, both of which would be invalid.

Hstring* s;
*s.length;   // this is equivalent to...
*(s.length); // ...this (not what you want at all)
Edd Inglis
  • 1,067
  • 10
  • 22
2

The difference is dot (.) and arrow (->) operators.

You can only use the dot (.) operator with a structure or union variable to access its members.

You can only use the arrow (->) operator with a pointer variable to access members of the structure or union the pointer points to.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
kiran Biradar
  • 12,700
  • 3
  • 19
  • 44
1

As already mentioned, the . operator is for accessing members of a struct, whereas the -> operator is for accessing members of a struct pointer.

However, another important difference between your two functions is that in Strlength(Hstring* s) the parameter is passed by reference, implying that the function operates on the "original" memory location of the data structure and therefore can modify its contents.

In contrast, in Strlength(Hstring s) the parameter is passed by value, implying that the function operates on a copy of the original structure and changes made within the function will not be visible outside the function.

See also this answer.

Gerd
  • 2,568
  • 1
  • 7
  • 20
0

*s is a pointer, you may reference the members using operator ->, if there's no *, it's just a variable, you may reference the members using operator (dot) .

lenik
  • 23,228
  • 4
  • 34
  • 43