Maker Pro
Maker Pro

structure and function to pass the parameter

anukalp

Jul 28, 2018
35
Joined
Jul 28, 2018
Messages
35
I wrote code to store the Id and price of one product.

Code:
#include <stdio.h>

struct Product{
    int id;
    int price;
}product1;

int main()
{
    /*Assigning id and pric of each product here*/
     product1.id = 1;
     product1.price = 30;

     /* Displaying product details*/
     printf("\n Product1 Id : %d", product1.id);
     printf("\n Product1 Price : %d", product1.price);
    
     return 0;
}

I want to extend the code to store so many id's and prices of products

I have following idea in mind for multiple products to their id's and prices
Code:
 void ProductsDetails( Products, ID, Price)
 
 void main()
 {
     ProductsDetails( 1, 0001, 30)  //Call function from here 
    
     ProductsDetails( 2, 0002, 45)
    
     ProductsDetails( 3, 0003, 60)
    
     .......
    
     ProductsDetails( Products, ID, Price)
    
 }

Hope somebody would help me
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,725
Joined
Nov 17, 2011
Messages
13,725
For a static implementation define teh products as an array:
Code:
struct Product{
    int id;
    int price;
}products[1000];
Or better yet (in my opinion:
Code:
typedef struct MyProduct{
    int id;
    int price;
}

MyProduct products[1000];

You can then access each product by its index
Code:
int main()
{
   /*Assigning id and pric of each product here - of course you'd have to find a way to assign different ids and prices here, e.g. read from a file*/
for (i=0;i<1000;i++) {
    products[i].id = 1;
    products[i].price = 30;
}

    /* Displaying product details*/
/* put another for loop here, see below */
 
    return 0;
}

You can then also use a function to access the product parameters:
Code:
    /* Displaying product details*/
for (i=0;i<1000;i++) {
    Display_Product(products[i])
}
With Display_Product() defined as e.g.
Code:
    /* Displaying product details*/
void Display_Product(MyProduct product) {
    printf("\n Product Id : %d", MyProduct.id);
    printf("\n Product Price : %d", MyProduct.price);
}

Of course, using an array with a ststic size limits the scalability of the program. If you need more than 1000 components (in this example), yo'll have to change the size of the array and recompile. A much more elegant approach would use dynamic memory allocation.

Update: corrected indexing of product.
 
Last edited:

anukalp

Jul 28, 2018
35
Joined
Jul 28, 2018
Messages
35
For a static implementation define teh products as an array:
You can then access each product by its index
How can we access each product by array index

Code:
#include <stdio.h>

struct Product{
    int id;
    int price;
}products[1000];

int main()
{
  /*How to access each product by its index */
 
  /* Displaying product details*/
   for (i=0;i<1000;i++) {
       Display_Product(product)
}
     return 0;
}
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,725
Joined
Nov 17, 2011
Messages
13,725
How can we access each product by array index
Sorry, a typo of mine in my post #2 (now corrected). You access the array elements in this way:
Code:
for (i=0;i<1000;i++) {
    product.id[i] = 1; /*
    product.price[i] = 30;
}
You do not need this within the subroutine as the indexing is done in the main calling loop:
Code:
#include <stdio.h>

struct Product{
   int id;
   int price;
}products[1000];

int main()
{
  /*How to access each product by its index */

  /* Displaying product details*/
   for (i=0;i<1000;i++) {
      Display_Product(product[i])
}
    return 0;
}
 

anukalp

Jul 28, 2018
35
Joined
Jul 28, 2018
Messages
35
Sorry, a typo of mine in my post #2 (now corrected). You access the array elements in this way:

You do not need this within the subroutine as the indexing is done in the main calling loop:
I can see You haven't assign id and prices to member products
so What should be under the Display_Product routine
Code:
#include <stdio.h>

struct Product{
   int id;
   int price;
}products[1000];

/*what should be in the following routine  ?
Display_Product(product[i])
{

}
int main()
{
      Display_Product(product[i])
}
    return 0;
}
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,725
Joined
Nov 17, 2011
Messages
13,725
Look up my post #2. I had to struggle a bit as the editor kept removing my indexing ([i]) - this requires special coding in the editor.
 

anukalp

Jul 28, 2018
35
Joined
Jul 28, 2018
Messages
35
Look up my post #2. I had to struggle a bit as the editor kept removing my indexing ([i]) - this requires special coding in the editor.
I am taking step back because I don't think I have clear understand on some topics

I hope you will keep to guide me. I want to pass the array to function.

I have this basic idea but it's not complete code
Code:
#include <stdio.h>
void display( int array_product[10])
{
}
int main()
{
    return 0;
}
How to make complete code to pass array[10] to function ?
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,725
Joined
Nov 17, 2011
Messages
13,725
Passing a complete arry to a subroutine is usually not done. It would require to copy the complete array into the subroutine's memory area. Typically only a reference to the array is given as parameter to the subroutine. Read more here.
 

anukalp

Jul 28, 2018
35
Joined
Jul 28, 2018
Messages
35
Passing a complete arry to a subroutine is usually not done. It would require to copy the complete array into the subroutine's memory area. Typically only a reference to the array is given as parameter to the subroutine. Read more here.
I am struggling to pass array
when I call to display function, then the value of entire array should be double
Code:
#include <stdio.h>
void display( int product[5])
{
    int i;
    for( i = 0; i < 5; i++)
        product[i] = product[i] * 2;
}

int main()
{
    int array[5] = {1, 2,3, 4,5};
    int result;
   
    result =  display(product);
   
    printf ("result %d", result );
   
    return 0;
}
error: 'product' undeclared (first use in this function)
result = display(product);
I know that a local variable exists only inside the function
 
Last edited:

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,725
Joined
Nov 17, 2011
Messages
13,725
1) You don't pass an array as parameter to a function; you pass a pointer to the beginning of the array to avoid copying of the entirre array when you call the subroutine, see post #8 and the link therein.
2) Why do you think the compiler throws an error here?
Code:
result =  display(product);
The scope in question is this piece of your code:
Code:
int main()
{
    int array[5] = {1, 2,3, 4,5};
    int result;
  
    result =  display(product);
  
    printf ("result %d", result );
  
    return 0;
}
Where is the definition of the variable product?
 

anukalp

Jul 28, 2018
35
Joined
Jul 28, 2018
Messages
35
Where is the definition of the variable

I need some help to understand following code

Code:
struct structureName {
 
  struct structureName *nextPointer;    // "nextPointer" is member of structure "structureName"
};

//function return pointer "newPointer" to structure "struct structureName"
//Function pass the pointer "nextPointer" to the structure "struct structureName"
struct structureName *function ( struct structureName *nextPointer)
{
    struct structureName *newPointer = malloc(sizeof(*newPointer))
    
      newPointer->nextPointer = nextPointer
      
      return newPointer;
    
}
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,725
Joined
Nov 17, 2011
Messages
13,725
1) Have you understood the answer in post #10 to the quetsion in post #9?

2)
I need some help to understand following code
Start by explaining in your own words what this dfinition menas:
Code:
struct structureName {
 
  struct structureName *nextPointer;    // "nextPointer" is member of structure "structureName"
};
 

anukalp

Jul 28, 2018
35
Joined
Jul 28, 2018
Messages
35
Start by explaining in your own words what this dfinition menas:

Code:
struct structureName {
  struct structureName *nextPointer;    // "nextPointer" is member of structure "structureName"
};

This define structure whose name is structureName. structure has only one member. we can include a structure within another structure

Code:
struct structureName *nextPointer;    // "nextPointer" is member of structure "structureName"

Pointer is variable which can hold the address of another variable
here *nextPointer; is a pointer variable that can point to structure struct structureName
We can have pointer of structure that can point the address of structure variable
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,725
Joined
Nov 17, 2011
Messages
13,725
Both parts are correct. But what does that mean in the context of the defintion of the structure. In other words, what is the content of the structure?
 

anukalp

Jul 28, 2018
35
Joined
Jul 28, 2018
Messages
35
Both parts are correct. But what does that mean in the context of the defintion of the structure. In other words, what is the content of the structure?
we can access the member of structure by declaring structure variable
Look at this example : I want to make function that can pass the list element and next node and it can be return the current node

Code:
#include<stdio.h>

struct student
{
   int age;
   float hight ;
   struct student *nextstudent;
 
}student1;
 
int main()
{
    student1.age = 29;
    student1.hight = 6.3;
    
    printf(" age %d \n",student1.age);
    printf(" hight %f \n",student1.hight);
 
   return 0;
}

I think this line point to next student struct student *nextstudent;
It's pointer of structure, I know pointer store the address of another variable but I don't understand which is another whose address stored pointer
 
Last edited:

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,725
Joined
Nov 17, 2011
Messages
13,725
You are evading me by posting new code.
Code:
struct structureName {
 
  struct structureName *nextPointer;    // "nextPointer" is member of structure "structureName"
};
is a structure with only one member. This member is a pointer to another structure of the same type. No other content. Therefore not really useful.

The code in your post #15 contains a much more meaningful structure definition because the structure contains infomation (age, hight - you probbaly mean height?) and a pointer to another structure of the same type. This is useful for creating linked lists of dynamic length (instead of using an array). Linked lists typicallly use dynamic memory allocation. Read the linked info for more detail.
 

anukalp

Jul 28, 2018
35
Joined
Jul 28, 2018
Messages
35
You are evading me by posting new code.
The code in your post #15 contains a much more meaningful structure definition because the structure contains infomation (age, hight - you probbaly mean height?) and a pointer to another structure of the same type. This is useful for creating linked lists of dynamic length (instead of using an array). Linked lists typicallly use dynamic memory allocation. Read the linked info for more detail.
Take a look at this
Code:
#include <stdio.h>
#include <stdlib.h>
 
struct student{
       int age;
       struct node *next;
       };

struct student *addStudent(struct student *head,int data){
    struct student *newStudent;
    newStudent = malloc(sizeof(struct student));
    newStudent->age = data;
    newStudent->next = NULL;
 
    if (head!=NULL){
        head->next = newstudent; 
    }
    return newstudent;
}
 
void showdetaills( ){
    
}
    
int main(void){    
  struct node *head = NULL;
  showdetaills(head);
 return 0; 
}

I do not have complete idea I am trying to complete showdetaills. What should be inside the routine to display details
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,725
Joined
Nov 17, 2011
Messages
13,725
am trying to complete showdetaills.
Showdetails() would display the data from the srtucture as in your previous examples.
But since head is a Null Pointer, there is nothing to display at all.
Code:
int main(void){   
  struct node *head = NULL;
  showdetaills(head);
 return 0;
}

You should really understand the use of pointers in C and of linked lists (see link in post #16). I cannot give you a complete tutorial on these topics. There are lots of tutorials on the internet. I gave you two suitable links.
Split your learning experience:
Try to understand pointers first (without diving into linked lists). That is challenging enough.
Once you have a firm concept of pointers,go on to linked lists, that is then almost easy.
 

anukalp

Jul 28, 2018
35
Joined
Jul 28, 2018
Messages
35
You should really understand the use of pointers in C and of linked lists (see link in post #16). I cannot give you a complete tutorial on these topics. There are lots of tutorials on the internet. I gave you two suitable links.
Split your learning experience:
Try to understand pointers first (without diving into linked lists). That is challenging enough.
Once you have a firm concept of pointers,go on to linked lists, that is then almost easy.
I have been searching a lot I am struggling to make linked list. I understand the use of pointers, structure .I do not understand how to use them into linked list
Code:
void showdetaills( ){
    
}
    
int main(void){   

 return 0;
}
I have shown you What I am missing so how to achieve it
 
Last edited:
Top