Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

[SOLVED] unable to understand its execution, kindly help

Status
Not open for further replies.

Hiroshi_S

Newbie level 6
Joined
Feb 19, 2015
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
133
C:
#include <stdio.h>
#include <string.h>
struct student
{
    char name[20];
}std;

char *fun(struct student *tempStd) // pointer
{
    strcpy(tempStd->name,"Thomas");
    return tempStd->name;
}

char *fun_one(struct student *tempStd)
{
    strcpy(tempStd->name,"Mike");
    return tempStd->name;
}
int main()
{
    strcpy(std.name,"Mike 2");
    printf("%s%s",fun(&std),fun_one(&std));
    return 0;
}
output : ThomasThomas

If I print as : printf("%s%s",fun_one(&std),fun(&std)) ,then the output is MikeMike.
Please help me to understand this concept.
 

KlausST

Super Moderator
Staff member
Joined
Apr 17, 2014
Messages
18,943
Helped
4,215
Reputation
8,433
Reaction score
4,160
Trophy points
113
Activity points
124,543
Hi,

I'm no C specialist, so see my post as an opinion. In doubt better rely on others..

In my opinion the struct "student" is useless. A struct mainly is used to join some variables to some kind of "dataset".
You may use the array "name" instead. If I'm not mistaken then an array (and a struct) is treated as a pointer and may be passed without "&" and "*".

But what the code is good for: I don't know. Maybe just for learning. Thus I think it's a good idea to give some context or link to a document where this code is from.

Klaus
 

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
14,715
Helped
4,810
Reputation
9,637
Reaction score
4,590
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
125,344
I agree, it is strange code but maybe only for educational purposes.
A struct containing only one item serves no useful purpose unless it is intended to be a base to add other things to it.

Brian.
 

KlausST

Super Moderator
Staff member
Joined
Apr 17, 2014
Messages
18,943
Helped
4,215
Reputation
8,433
Reaction score
4,160
Trophy points
113
Activity points
124,543
Hi,

You learn about structure... but not how it's meant to be. You don't learn it's true benefit.
As (funny) comparison: You learn to use a toothbrush ... for cleaning shoes.

Klaus
 

Hiroshi_S

Newbie level 6
Joined
Feb 19, 2015
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
133
Hi,

You learn about structure... but not how it's meant to be. You don't learn it's true benefit.
As (funny) comparison: You learn to use a toothbrush ... for cleaning shoes.

Klaus
HAHAHA.... its one of the strange code. Well lets not consider this as application oriented rather let us think why this code operates such way. I guess you guys might try to solve and came to output must be 'ThomasMike'. but its output is different. i am curious to know how it works.
 

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
14,715
Helped
4,810
Reputation
9,637
Reaction score
4,590
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
125,344
Remember that defining a struct just allocates space for it to hold it's component parts. It doesn't actually put anything in those spaces, at least in your example.

The code creates instances (copies) of the struct called 'std' and 'TempStd' which are the same design of structure but different copies of it. They both contain an entry called 'name' but it is a different entry in each instance of the struct. Note which is used when the output is printed.

Brian.
 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
47,974
Helped
14,147
Reputation
28,553
Reaction score
12,834
Trophy points
1,393
Location
Bochum, Germany
Activity points
278,280
There are no copies, only pointers to instance "std". The functions are overwriting std.name through the pointers.

The excercise is revealing the order of function evaluation and and that the pointer content is printed after both functions have been evaluated. I'm not completely sure, but I believe that these details belong to the implementation dependend part of the specification. Respectively the observed behaviour must not necessarily be reproduced with different compilers.
 

Hiroshi_S

Newbie level 6
Joined
Feb 19, 2015
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
133
Yes, There is no copies, just the pointers.
And Thanks your support I was able to proceed this. :)

As you can see the code in the function 'fun' and 'fun_one' both of their return type is pointer pointing the location of the value.
as you can see in the 'printf' statement, those function are orderly executed from the left to right
First 'fun_one' which change the value of name as 'Mike' and return its pointer location not the data,
then the function 'fun' is executed where the value of name is changed to 'Thomas' and return the pointer location not the data. Now both the fun and fun_one points towards the same location but the current value of that pointer is 'Thomas'.
So as further execution of the printf goes, it will display two strings which at the pointer location which is 'Thomas'. so 'Thomas' is printed two times as 'ThomasThomas'.

similarly, if i change the position of function call in printf statement as
C:
printf("%s%s",fun_one(&std),fun(&std));
then it will display as 'MikeMike', since 'fun_one' changes the name as 'Mike' so at the pointer location only Mike is stored thats why 'Mikemike' is printed.

huff.... i tried to debug that code by adding printf statement as below.
C:
#include <stdio.h>
#include <string.h>

struct student{
    char name[20];
}std;

char *fun(struct student *tempstd){
    printf("1. %s \n",tempstd->name);
    strcpy(tempstd->name,"MIKE");
    printf("2. %s \n",tempstd->name);
    return tempstd->name;
}

char *fun_one(struct student *tempstd){
    printf("1. %s \n",tempstd->name);
    strcpy(tempstd->name,"THOMAS");
    printf("2. %s \n",tempstd->name);
    return tempstd->name;
}

int main()
{
    printf("Hello World\n");
    strcpy(std.name,"mike2p");
    printf("0.%s%s%s \n",std.name,fun_one(&std),fun(&std));
    return 0;
}
/*output
Hello World                                                                                                                  
1. mike2p                                                                                                                    
2. MIKE                                                                                                                      
1. MIKE                                                                                                                      
2. THOMAS                                                                                                                    
0.THOMASTHOMASTHOMAS*/
well, if you ask me what i learned, its that in printf statement, the exection order is from left to right. and then print it in the order we specified. and the pointer of struct pointers.
I also thought the pritf sequence changes form compiler to compiler, i tried various compiler GCC, Turboc, codeblock ide and online platform too. were all of them gave the same output.

and please correct me if i gone wrong some where.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top