Thread: Function that takes in array of characters as parameter

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    11

    Function that takes in array of characters as parameter

    I'm writing a function that is supposed to add a Product object to an array of Products and also initialize the data members for each Product object. I'm running into an issue where I can't figure out how to initialize my name and category data members, both of which are an array of characters and would appreciate some help. I tried changing the parameters to things like 'char[] name' or 'char name[]' but it's not working out.


    I'm trying to compile my program below but I keep getting the errors:
    Code:
        store.c: In function ‘addProduct’:
        store.c:24:40: error: assignment to expression with array type
          productArr->products[productArr->numProducts]->name = name;
                                                ^
        store.c:25:33: error: ‘ProductType {aka struct <anonymous>}’ has no member named ‘category’
          productArr->products[productArr->numProducts]->category = category;

    store.c

    Code:
        int addProduct(ProductArrType* productArr, char name, char category){    
            productArr->products[productArr->numProducts] = malloc(sizeof(ProductType));
            productArr->products[productArr->numProducts]->name = name;
            productArr->products[productArr->numProducts]->category = category;
            return 1;
        }

    defs.h

    Code:
        #define MAX_RUNNERS  10
        #define MAX_STR  30
    
    
        typedef struct {
          char category[MAX_STR];
        } EntityType;
    
    
        typedef struct {
          char name[MAX_STR];
        } ProductType;
    
    
        typedef struct {
          int numProducts;
          ProductType *products[MAX_PRODUCTS]; 
        } ProductArrType;

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    I'm guessing it is because "category" is in 'EntityType', and not in 'ProductType'.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    505
    Code:
        int addProduct(ProductArrType* productArr, char name, char category){    
            productArr->products[productArr->numProducts] = malloc(sizeof(ProductType));
            productArr->products[productArr->numProducts]->name = name;
            productArr->products[productArr->numProducts]->category = category;
            return 1;
        }

    defs.h

    Code:
        #define MAX_RUNNERS  10
        #define MAX_STR  30
    
    
        typedef struct {
          char category[MAX_STR];
        } EntityType;
    
    
        typedef struct {
          char name[MAX_STR];
        } ProductType;
    
    
        typedef struct {
          int numProducts;
          ProductType *products[MAX_PRODUCTS]; 
        } ProductArrType;
    In C, string are passed around as char *'s. They are arrays of char
    terminated with a NUL. Since you have a buffer, you need to
    call the function strcpy() to copy your input strings to this buffer.

    So the prototype for the fucntion should be

    Code:
    int addProduct(ProductArrType *productArr, const char *name, const char *category):
    The const isn't strictly necessary but makes it clear that you are not
    modifying the input string, you are taking a copy of them.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to call a function that takes a STRUCT parameter
    By fouzimedfouni in forum C Programming
    Replies: 2
    Last Post: 02-24-2015, 10:24 AM
  2. a function which takes a character array and reverses it
    By Kevin Nguyen in forum C Programming
    Replies: 2
    Last Post: 12-07-2013, 03:46 PM
  3. C++ Function takes File Object as Reference Parameter
    By noob_coder in forum C++ Programming
    Replies: 4
    Last Post: 02-22-2013, 01:19 AM
  4. Replies: 4
    Last Post: 11-28-2011, 03:39 PM
  5. Replies: 1
    Last Post: 10-21-2007, 07:44 AM

Tags for this Thread