Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with error message

Status
Not open for further replies.

anatazi

Programmer
Jun 24, 2002
33
US
Hi all
Whenever I try to compile this code, I get
cc: Error: readtopps.c, line 3: In this declaration, "data" has no linkage and is of an incomplete type. (incompnolink)
int data[];

What does the error message means?
Thanks,

#include <stdio.h>
int* readtopps (char fn[]){
int data[];
int n,i;
FILE* fp;

fp=fopen(fn,&quot;r&quot;);
fscanf(fp,&quot;%d&quot;,&n);
data=(int*)malloc(sizeof(int)*n);
for (i=0;i<n;i++)
fscanf(fp,&quot;%f&quot;,&data);
fclose(fp);
return(data);
}
 
Probably that the compiler can't reserve memory for the &quot;data&quot; array because it doesn't know how large it is. Even though arrays and pointers are basically the same, the way the compiler handles them is fundamentally different. The dynamic memory for a pointer comes from the heap, while an array is stored on the stack. When you declare a pointer, the compiler knows how much stack space to reserve (usually 4 bytes) to hold the address of where the pointer's going to point. However, an array is stored entirely on the stack. The compiler must know at compile time how large the array is so that it can reserve that much memory on the stack.

This is also the reason why the following code is illegal:

Code:
int x=3;
int a[x]; //This line gives a compiler error


As an aside, that's only illegal because the value of x can change. However, change x's declaration to read

const int x=3;

and the code becomes legal.
 
Hello !

You must always specify the size of the array as a constant value when you define it. You do not need to specify its size when you declare it.
Code:
extern int a[];
/* Declaration ok */

int a[20];
/* Definition of the previously
   declared variable: must give size */

int b[];
/* Definition without size -> ERROR */

int myfunction(
        int a[],
        /* Declaration of argument ok */
        int b[20]
        /* also ok */
        )
{
    int c[10];
    /* Definition of local array needs size */

    a[0] = b[0];     /* ok */

    *(a++) = 1; /* ok ??? */
    /* Yes the previous line is OK.
       In fact an array declared in the argument list
       of a function is actually a pointer to the first
       element of the array */

    *(c++) = 1; /* ERROR */
    /* This is not true for arrays defined outside
       argument list*/
}

To dds82:
sorry the
Code:
const int x = 3;
int a[x]
does not work with all C compilers. The GNU gcc 3.0.3, for example, does not allow it. I am not sure this is part of the C ansi specification.
I regret it: it is a cleaner way to define arrays size than the use of
Code:
#define
(not to say litteral constants).
This is a valid construct in C++, though.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top