1

I am trying to write a program that passes information from the kernel space to the user space on ububtu 14.04. I declared a struct as the follows:

typedef struct
{
   long pid;
   char TTY[64];
   unsigned long long time; 
   char COMM[64]
  } myTasks; 

In main, I then create an array of myTasks structs like:

struct myTasks taskInfo[2500]; 
//do stuff 
syscall(__NR_my_syscall2,numTasks,sizeof(taskInfo),taskInfo); // use it here

However, when I do that, I get a error on this line saying:

struct myTasks taskInfo[2500] Error: array type has incomplete element

What am I doing wrong? I wanted to create an array of myTasks structs that I could pass as a buffer to a syscall... but I can't figure out what I'm doing wrong. I'm new to C so any help would be greatly appreciated.

ocean800
  • 3,489
  • 13
  • 41
  • 73

2 Answers2

4

You typedefed the anonymous struct to myTasks. To declare your array, use myTasks taskInfo[2500].

C thinks you are declaring a new named struct struct myTasks, and it's incomplete because you didn't define the struct. However, myTasks is an alias for the struct you already defined.

Purag
  • 16,941
  • 4
  • 54
  • 75
  • 1
    Oh.. so the it's a problem in my understanding of structs? So I should look into something like [this](http://stackoverflow.com/questions/1675351/typedef-struct-vs-struct-definitions)? – ocean800 Feb 18 '16 at 09:39
  • 1
    Yeah, that's a great resource! It looks like you just have some confusion on naming of the structs. In the code you provided, the struct you defined doesn't have a name; it's *anonymous*. You declared `myTasks` as an alias for that struct type. So it's like assigning a nickname to the struct. Later, you tried to declare an array containing `struct myTask`s, and `struct myTask` doesn't actually exist. C thought you were going to declare a new struct there, and since you didn't, it gave the error. – Purag Feb 18 '16 at 09:41
  • Thanks!! That makes a lot more sense, and I'll definitely read more into that link. – ocean800 Feb 18 '16 at 09:44
2

myTasks is a type-alias and not a structure name. The correct use of it is e.g.

myTasks taskInfo[2500];
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621