retagged by
547 views
0 votes
0 votes

Lines from Dennis Ritchie

1)

The variables named in a structure are called members. A structure member or tag and an ordinary (i.e., non-member) variable can have the same name without conflict, since they can always be distinguished by context. Furthermore, the same member names may occur in different structures, although as a matter of style one would normally use the same names only for closely related objects.

Can anybody elaborate these lines with some examples? 

-------------------------------------------------------------------------------------------------------------------------------

2)

struct addpoint(struct point p1, struct point p2)
{
p1.x += p2.x;
p1.y += p2.y;
return p1;}

Here p2 is only not copying inside p1 and also incrementing value

How copying and incrementing done same time in structures?

retagged by

1 Answer

Best answer
2 votes
2 votes

The variables named in a structure are called members. A structure member or tag and an ordinary (i.e., non-member) variable can have the same name without conflict, since they can always be distinguished by context.

#include<stdio.h>
int a;
int main()

{
    int a;
}

Here identifier 'a' is used twice but without conflicting,since they can always be distinguished by context. this will work for structers also.

 

 Furthermore, the same member names may occur in different structures, although as a matter of style one would normally use the same names only for closely related objects.

#include<stdio.h>
int a;
int main()

{
    struct s1
    {
      int a;
      int c;
    };

    struct s2
    {
      int a;
      int b;   
    };
}

Here identifier 'a' is used twice but with in different structures, therefore no conflict.... 

 

although as a matter of style one would normally use the same names only for closely related objects.

it means we generally doesn't use to avoid confusion


struct addpoint(struct point p1, struct point p2)
{
p1.x += p2.x;
p1.y += p2.y;
return p1;
}

 

Here p2 is only not copying inside p1 and also incrementing value

P2 doesn't copied... it's just accessing the variables like

a + = b ; ===> a = a+b; ===> here a is p1.x and b is p2.x

selected by

Related questions

0 votes
0 votes
0 answers
1
srestha asked Aug 11, 2018
249 views
Bit fields use for padding.It generally contained integer bits(signed and unsigned). But it cannot contain addresses. Does it mean bit field cannot contain a pointer ?Whe...