Click here to Skip to main content
15,032,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to iterate over a product array.The pointer that I want to use is Order, yet I receive an error stating;
error: called object is not a function or function pointer
   47 |       if(orders->product_items(j).sku == products[t].sku)
      |          ^~~~~~
hw4.c:49:22: error: called object is not a function or function pointer
   49 |        TotalCost += (orders->product_items(j).quantity * products[t].price);
      |                      ^~~~~~

This is an updated question.
These are the two files, I've downloaded.
struct product {
	short sku;
	char *name;
	float price;

struct item {
	short sku;
	short quantity;

struct order 
	short order_id;
	char *customer_name;
	short num_products;
	struct item *product_items;

struct order * read_orders(const char *filename, short *num_orders);

#include <inttypes.h>

#include "orders.h"

struct order * read_orders(const char *filename, short *num_orders)
    int fd;
    fd = open(filename, O_RDONLY);
    int err = fd;
    if (err == -1) {
        return NULL;

    read(fd, num_orders, sizeof(*num_orders));
    struct order *orders = (struct order *)
        calloc(*num_orders, sizeof(struct order));

    int i;
    for (i=0; i<*num_orders; i++) {
        struct order *op = &orders[i];
        read(fd, &op->order_id, sizeof(op->order_id));

        uint32_t sz;
        read(fd, &sz, sizeof(sz));
        op->customer_name = (char *)malloc(sz+1);
        read(fd, op->customer_name, sz);
        op->customer_name[sz] = 0;

        read(fd, &op->num_products, sizeof(op->num_products));
        op->product_items = (struct item *)
            calloc(op->num_products, sizeof(struct item));

        int j;
        for (j=0; j<op->num_products; j++) {
            struct item *ip = &(op->product_items[j]);
            read(fd, &ip->sku, sizeof(ip->sku));
            read(fd, &ip->quantity, sizeof(ip->quantity));

    return orders;

It's only this slight part of the code mentioned above, that's been troubling me. Other than that, the remainder of the code I believe should be fine. I would want assistance but not the answer. I also cannot make any modifications to this code before my first loop that iterates over the orders array.
to run the code; gcc -o hw4 hw4.c orders.c

What I have tried:

<pre>#include <stdio.h>
#include <stdlib.h>
#include "orders.h"

struct product products[] = {
		1234, "Bread", 2.99
		5678, "Milk", 4.19
		9012, "Eggs", 3.49
		3456, "Butter", 3.99
		7890, "Juice", 2.49
		2345, "Muffin", 1.49

int main()
		short num_orders;
		struct order *orders = read_orders("orders.db", &num_orders);
		if (orders == NULL)
			printf("Order database does not exist\n");

		//This for loop will loop all over order structures.
		for (int i = 0; i < num_orders; i++)
			//This will represent the total cost 
			float TotalCost = 0;
			//Print name and order ID
			printf("Order ID: %hd", orders[i].order_id);
			printf("Name: %s\n", orders[i].customer_name);
			for (int j = 0; j < orders[i].num_products; j++) //This is for the loop that will iterate over the product array
				for (int t = 0; t< 6; t++)
						if(orders->product_items(j).sku == products[t].sku)
							TotalCost += (orders->product_items(j).quantity * products[t].price);

							printf("%s %hd %f/n", products[t].name, orders->product_items[j].quantity, products[t].price);
			printf("Total: %f\n",TotalCost);
Updated 16-Apr-21 16:25pm
CPallini 16-Apr-21 2:04am
"I just have an issue with iterating over the orders array that over the orders array"
What is the issue?

1 solution

You forgot to put the index when accessing orders. Try putting [i] and replace (j) with [j].
if(orders[i].product_items[j].sku == products[t].sku)
TotalCost += (orders[i].product_items[j].quantity * products[t].price);
Member 15084336 16-Apr-21 23:12pm
Yes, I've done that in the mean time. Only Issue I have now is upon running, it states my order database doesn't exist. Currently going over.
Shao Voon Wong 16-Apr-21 23:30pm
You can try an absolute path to the file, instead of a relative path.
Member 15084336 17-Apr-21 1:45am
Would I do this in the command line? It's a db file that I just transferred into my C drive on my windows desktop but to no avail.
Shao Voon Wong 17-Apr-21 2:51am
Assuming you put orders.db in the temp folder in the C drive. This is how you would specify it in read_orders. As for the command line, you can google it.

struct order *orders = read_orders("c:\\temp\\orders.db", &num_orders);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900