Declaration of structure

Thread Starter

anukalp

Joined Jul 28, 2018
158
I have some of doubts on declaration of structure with pointer

We declare a structure
Code:
struct member
{
            int id;
            float hight;
};
Following is the syntax of a function declaration that will return result.

Code:
returnType functionName(dataType parametersName, ...);
For example : void function ( int age , char grade, int*pointer, struct member )

I think this function passing parameters such as integer variable, character variable, pointer value and structure

We say function is passing structure What does it mean How do we define it ?
 

bogosort

Joined Sep 24, 2011
696
We say function is passing structure What does it mean How do we define it ?
What does it mean to pass a structure to a function? It means that the data associated with the structure object gets copied to the stack, where the function will have access to copies of the member values. Since this is a copy, the function cannot change the values of the actual structure object. To do so, we'd need to pass the function a pointer to the structure object.

Even if we don't plan on changing the structure object within the function, it's generally a good idea to pass a pointer to the object regardless, as it is typically much more efficient than copying all the member values.

For example,
C:
#include <stdio.h>
struct Person { int id; float height; } tom = { 1, 2.0 };

// show_person() gets a copy of the structure object
void show_person(const struct Person p) {
  printf( "person %d is %f meters tall.\n", p.id, p.height );
}

// change_height() gets a pointer to the structure object
void change_height(struct Person *p, float new_height) {
  p->height = new_height;
}

int main(void) {
  show_person(tom);
  change_height(&tom, 1.9)  ;
  show_person(tom);

  return 0;
}
This results in:
Code:
person 1 is 2.000000 meters tall.
person 1 is 1.900000 meters tall.
 

Thread Starter

anukalp

Joined Jul 28, 2018
158
Even if we don't plan on changing the structure object within the function, it's generally a good idea to pass a pointer to the object regardless, as it is typically much more efficient than copying all the member values.
For example,
C:
void change_height(struct Person *p, float new_height) {
  p->height = new_height;
}
Code:
returnType functionName(dataType parametersName, ...);
If I compare below code with above syntax I don't understand return type , function name and data parameters
C:
struct person* newPerson(struct person *p, float new_height)
{
    p->height = new_height;
    return p;
}
Which one is function name ? It may be newPerson
What type of data does it return ? ( int, char, flot, double )
What type of data does it pass ? ( int, char, float, double, array)
 
Last edited:

WBahn

Joined Mar 31, 2012
29,979
One thing that may help is that it is often more meaningful to a human to read data type declarations in reverse order.

So

struct Person *p

would be read, "the variable 'p', which is a pointer to a Person structure."

Use the delimiters (things like parentheses and commas) to help identify the various pieces of the declaration.

returnType functionName(dataType parametersName, ...);

The thing immediately to the left of the '(' is the function name. Everything before the function name is the return type. Between the parens is a list of paramater declarations separated by commas (if there's more than one). The last thing in each declaration is the parameter name and everything before it is the type of that parameter.
 

Thread Starter

anukalp

Joined Jul 28, 2018
158
The thing immediately to the left of the '(' is the function name. Everything before the function name is the return type. Between the parens is a list of paramater declarations separated by commas (if there's more than one). The last thing in each declaration is the parameter name and everything before it is the type of that parameter.
Code:
struct person* newPerson(struct person *p, float new_height)
{
    p->height = new_height;
    return p;
}
Does it define like this ?

(struct person is return type) * (newPerson is function name) (struct person *p pass the pointer, pass the float value )

I have never used (*) between return type and function name
 

bogosort

Joined Sep 24, 2011
696
I have never used (*) between return type and function name
The '*' belongs to the return type. In other words, your function newPerson() returns a pointer to a 'struct person'. The function call would look something like this:
C:
#include <stdio.h>

struct person { int id; float height; };

struct person * newPerson(struct person *p, float new_height) {
    p->height = new_height;
    return p;
}

int main(void) {
    struct person tom = { 1, 2.0 }, *ptr;

    ptr = newPerson( &tom, 1.9 );

    printf( "tom.height: %.02f\nptr->height: %.02f\n", tom.height, ptr->height );

    return 0;
}
Running this will show that both tom.height and ptr->height are 1.9 after the function call, which makes sense because they both reference the same thing. In the call to the newPerson() function, we pass the address of 'tom', which is stored in the pointer 'p' inside of the function. This means that 'p' points to precisely the same memory address as the variable 'tom', i.e., both 'p' and 'tom' reference the same data. By changing the value of 'p->height', we are changing the value of 'tom.height'. The function then returns the pointer 'p', which we assign to the pointer 'ptr' in the main() code. Now in main() we have two variables that reference the same data: 'tom' and 'ptr'.

Notice that because we sent a pointer to newPerson() -- i.e., we sent the actual address of the 'tom' data -- we were able to change the 'tom' data from within the function (had we instead sent a copy of the structure, instead of a pointer to it, this would not be the case). Thus, returning the pointer 'p' from newPerson() is redundant. If the calling program really needed a pointer to 'tom', it could have made one itself: "struct person *ptr = &tom;".

Here's a more idiomatic way of achieving the same result:
C:
#include <stdio.h>

struct Person { int id; float height; };

void newHeight(struct Person *p, float new_height) {
    p->height = new_height;
}

int main(void) {
    struct Person tom = { 1, 2.0 }, *ptr = &tom;

    newHeight( ptr, 1.9 );

    printf( "tom.height: %.02f\nptr->height: %.02f\n", tom.height, ptr->height );

    return 0;
}
Notice that I've capitalized the structure tag 'Person', which is a common convention, and renamed newPerson() to newHeight(), as that's a more accurate name for what the function does.
 

Thread Starter

anukalp

Joined Jul 28, 2018
158
The '*' belongs to the return type. In other words, your function newPerson() returns a pointer to a 'struct person'. The function call would look something like this:
Thanks for clearing a doubt's. You have explained very well

I am taking part of your code

C:
#include <stdio.h>

struct person { int id; float height; };

struct person * newPerson(struct person *p, float new_height) {
    p->height = new_height;
    return p;
}
}
For more complex declaration
I seen this type of declaration in linked list
C:
struct person { int id; float height; struct person *nextPerson;};

/* pass next person and new height */
struct person *newPerson(struct person *nextPerson, float new_height) {
    struct person *p = malloc(sizeof(*p));
    p->height = new_height;
    p->nextPerson = nextPerson;
    return p;
}
I understand that can pass each person and height of person. before calling to function we have to assign new height and next person in the main function How to assign new height and next person in the main .
 
Last edited:

bogosort

Joined Sep 24, 2011
696
I understand that can pass each person and height of person. before calling to function we have to assign new height and next person in the main function How to assign new height and next person in the main .
Typically, we don't want the main function dealing with the internals of our data structures, including linked lists. What we do is create helper functions that handle list management, e.g., creating the list, adding and deleting nodes, printing values, etc. Here's a very simple implementation of your example:
C:
#include <stdio.h>
#include <stdlib.h>


struct Person { int id; float height; struct Person *nextPerson;};

// add person to head of the list
void addPerson(struct Person **head, int id, float height) {
    struct Person *p = malloc(sizeof(*p));
    p->id = id;
    p->height = height;
    p->nextPerson = *head; // assign the old head node to the nextPerson node
    *head = p; // the node we just created becomes the new head node
}

// create the list with an initial head node
struct Person *create_list(int id, float height) {
    struct Person *head;

    head = malloc( sizeof(*head) );

    head->id = id;
    head->height = height;
    head->nextPerson = NULL; // no other nodes in the list yet

    return head;
}

void show_people(struct Person *head) {
    struct Person *current = head;

    while ( current != NULL ) {
        printf( "Person %d -> %.02f\n", current->id, current->height );
        current = current->nextPerson;
    }
}

int main(void) {
    int i;
    struct Person *list = create_list( 1, 2.0 );

    // demonstrate adding nodes to the list
    for ( i = 2; i < 5; ++i ) {
        addPerson( &list, i, (float)2 + i );
    }

    show_people(list);

    return 0;
}
 

Thread Starter

anukalp

Joined Jul 28, 2018
158
Typically, we don't want the main function dealing with the internals of our data structures, including linked lists. What we do is create helper functions that handle list management, e.g., creating the list, adding and deleting nodes, printing values, etc. Here's a very simple implementation of your example:
very nice explanation, you are creating node node, adding a node and printing the node. There is one thing left deleting node in code
 

bogosort

Joined Sep 24, 2011
696
very nice explanation, you are creating node node, adding a node and printing the node. There is one thing left deleting node in code
See if you can implement a delete node function that accepts an id and deletes the corresponding node. Also, try implementing an insert node function that adds the new node to the end of the list.
 

ApacheKid

Joined Jan 12, 2015
1,533
I have some of doubts on declaration of structure with pointer

We declare a structure
Code:
struct member
{
            int id;
            float hight;
};
Following is the syntax of a function declaration that will return result.

Code:
returnType functionName(dataType parametersName, ...);
For example : void function ( int age , char grade, int*pointer, struct member )

I think this function passing parameters such as integer variable, character variable, pointer value and structure

We say function is passing structure What does it mean How do we define it ?
Take a look at this post, it may be helpful to you.
 
Top