-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

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). Commented Oct 3, 2021 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. Commented Oct 3, 2021 at 21:25
  • Does this answer your question? How to initialize a pointer to a struct in C? Commented Oct 3, 2021 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 #included in the main file is irrelevant. Commented Dec 29, 2021 at 7:30

3 Answers 3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

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.
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.