Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics 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.

structure and function

Status
Not open for further replies.

ansh11

Member level 4
Joined
Feb 27, 2018
Messages
71
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
659
Hi

A function definition consists of a return type , a function name, a list of parameters in parentheses

I do not understand what types of function declaration is this in the following code


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
struct student
{
    int mark;
};
 
//What is meaning of this declaration 
struct * newStudent (int mark, struct student *nextstudent)
 
void main ()
{
   //Program code //
}



Can anyone help me What does this function do and What does it pass and get return ?
 

I'm not really a C guy, but that is not a function, it's a structure. Two totally different things. Look it up.
 

-first defined a structure with name as 'student' with only one member 'int mark' as:

struct student
{
int mark;
};

Then a new structure named as 'newStudent ' is defined , but
this is defined as a 'pointer to the new structure' (*newstudent)

This pointer structure contains :
-two members,
*one , with mark(int mark)
*another with an already defined structure of 'student' as a pointer in -'struct student *nextstudent'

which is:

struct * newStudent (int mark, struct student *nextstudent)

You can see that every 'newstudent structure' contains his/her 'mark' and also a 'pointer to the next student'

In essence , this what we call as 'linked list'

again as already pointed out this is not a 'function declaration but a 'structure'
'
 

I don't recognize valid C syntax in line 8.

- - - Updated - - -

This would be e.g. legal C code (although not useful)


Code C - [expand]
1
2
3
4
struct student * newStudent (int mark, struct student *nextstudent)
{
    return nextstudent;
}



The example clarifies that it is a function declaration, also showing usage of the function return value.
 

I'm not really a C guy, but that is not a function, it's a structure. Two totally different things. Look it up.

I was also confused that's why gave thread name structure and unction
 

The original code may be valid if the compiler accepts untypified struct pointers, at least not a standard C feature.

- - - Updated - - -

"structures and functions" is also a subchapter in good old Kernighan & Ritchie, The C Programming Language. Suggest to review it.
 

I don't recognize valid C syntax in line 8.

- - - Updated - - -

This would be e.g. legal C code (although not useful)


Code C - [expand]
1
2
3
4
struct student * newStudent (int mark, struct student *nextstudent)
{
    return nextstudent;
}



The example clarifies that it is a function declaration, also showing usage of the function return value.

exactly I have seen four type of function's

1. void function name (void)
2. voide function name (pass parameters )
3. return result function name (void)
4. return result function name (pass parameters)

I think 4 but I do not understand what type of function is this ? What the result it return ?
 
Last edited:

You should really learn some C basics. The function returns a pointer to structure. Detailed examples in the said text book - or many others.
 

You should really learn some C basics. The function returns a pointer to structure. Detailed examples in the said text book - or many others.

That's what I am doing I was stuck with specific problem and I posted question here

Code:
struct student * newStudent (int mark, struct student *nextstudent)
{
    return nextstudent;
}

int main (void)
{
	// how to call that function in main
}

how to call that function in main ?
 

I don't recognize valid C syntax in line 8.

- - - Updated - - -

This would be e.g. legal C code (although not useful)


Code C - [expand]
1
2
3
4
struct student * newStudent (int mark, struct student *nextstudent)
{
    return nextstudent;
}



The example clarifies that it is a function declaration, also showing usage of the function return value.

you are right.
have intrpreted the line 8 as structure .(looks deceptive )
 

how to call that function in main ?

By writing empty example like the definition of nextstudent, important points are left open, e.g. does it allocate storage for the returned new structure. Using the structure provided by nextstudent as in my simplistic example makes no sense for real code. But that's a context problem and not directly related to the discussed syntax question.


Code C - [expand]
1
2
3
4
struct student * pns;
struct student onestudent;
 
pns = newstudent(5,&onestudent);



As a side remark, instead of repeating struct student in any variable or pointer definition, you can make a typedef and use it subsequently


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
typedef struct
{
    int mark;
} t_student; 
// optionally define a pointer type at one go 
// } t_student, *p_student;
 
t_student * newStudent (int mark, t_student *nextstudent)
 
// alternatively, referring to the pointer type
 
p_student newStudent (int mark, p_student nextstudent)

 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top