-1

I have a struct, located in my header file, and I want to set its members to values I have in my main() function, those being "size" and "cap". I get the following: error: expected identifier or ‘(’ before ‘->’ token struct Array->size = size; I also get the same error for the line with "cap."

I've provided my header file, where the struct is found, and my function definitions file.

Header File:

#include <stdio.h>

struct Array {
  unsigned int size;
  unsigned int cap;
  int data;
};

struct Array *newArray(unsigned int size, unsigned int cap); //Prototype

Function Definition File:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

struct Array *newArray(unsigned int size, unsigned int cap) {

  struct Array->size = size;
  struct Array->cap = cap;

}

I have intentionally not included my header file in my function definitions file because I include it in my main file. Having header.h included twice gives me more errors/warnings.

Could anyone please help? Thanks

BlueAngel45
  • 21
  • 1
  • 4
  • 2
    `struct Array` is a type. You can't store values in a type. Create an instance of that type first (malloc some space). – tkausl Oct 03 '21 at 21:25
  • `struct Array` is the name of a type, not the name of a pointer variable which you can use the `->` operator with. You need to allocate memory for a `struct Array` first and then use the pointer to that memory. – mkrieger1 Oct 03 '21 at 21:25
  • Does this answer your question? [How to initialize a pointer to a struct in C?](https://stackoverflow.com/questions/11709929/how-to-initialize-a-pointer-to-a-struct-in-c) – mkrieger1 Oct 03 '21 at 21:29
  • You are not be `#include`-ing your own header file, so the definition of the structure (or even that type `Array` exists) is unknown... that it is `#include`d in the main file is irrelevant. – Andrew Dec 29 '21 at 07:30

3 Answers3

1

You are trying assign something to a type which does work.

struct Array *newArray(unsigned int size, unsigned int cap) {

   struct Array->size = size;
   struct Array->cap = cap;

}

Here is how to fix your code:

struct Array *newArray(unsigned int size, unsigned int cap) {
   struct Array *arr = malloc(sizeof(struct Array));
   if(!arr)
       return NULL;

   arr->size = size;
   arr->cap = cap;
   
   return arr;
}
anton-tchekov
  • 1,028
  • 8
  • 20
0

You need to create a new instance of the Array struct, initialize its fields, and return pointer to it in the newArray function.

Kostya Dzyuba
  • 441
  • 6
  • 11
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 03 '21 at 21:39
0

don't include all the header files in the main file but include them in the one file in which all the functions are included then include said header file in the main file , you won't have the same error twice in both header file , takes the compiler time to debug in exchange