• retagged by
936 views
0 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?

1 Answer

Best answer
2 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
Position:
Show:

Related questions

0 0 votes
0 0 answers
554
554 views
srestha asked Aug 11, 2018
554 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...
0 0 votes
0 0 answers
980
980 views
Akhilesh Singla asked Mar 17, 2018
980 views
Hi. I gave GATE 2018 by dropping the year and I was able to solve most of the GATE questions on programming. Before GATE I didn't have any interest in programming. But no...
1 1 vote
1 1 answer
790
790 views
BIPLAB DAS asked Jul 20, 2017
790 views
A function need not return a value;a return statement with no expression causes control,but no useful value,to be returned to the caller,as does "falling off the end" of ...
1 1 vote
1 1 answer
1.3k
1.3k views
BIPLAB DAS asked Jul 18, 2017
1,313 views
#include<stdio.h>main(){ int c,nb,nt,nn; nb=0; nt=0; nn=0; while((c=getchar())!=EOF) if(c==' ') ++nb; else if(c=='\t') ++nt; ...