Link to home
Start Free TrialLog in
Avatar of tmp09
tmp09

asked on

What's wrong with this?

Here is what I have so far. I am beginning to write a program.

#include<stdio.h>
#include<conio.h>
#include<iomanip>
#include<time.h>
#include<stdlib.h>

int randarray[];
int full;

int main(){

printf("Enter the number of integers you wish to sort.\n");
scanf("%d",&full);


for(int i = 0; i < full; i++)
{
      randarray[i] = rand();
      printf("%d\n",randarray[i]);
}

return 0;


         
     }

Why is it giving me this external error??



lab1.obj : error LNK2001: unresolved external symbol "int * randarray" (?randarray@@3PAHA)
Debug/letsgo.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

letsgo.exe - 2 error(s), 0 warning(s)

Avatar of Sjef Bosman
Sjef Bosman
Flag of France image

Your randarray has no length, you defined it more or less as a pointer. C requires you to reserve space for the arrays you have (unlike JavaScript). Write
    int randarray[100];
or, even better
    #define MAXRANDS    100
    int randarray[MAXRANDS];

If you want, you can also allocate space dynamically, see your manual on malloc() or calloc();
Avatar of tmp09
tmp09

ASKER

I don't want to give the array a specific size.  The user needs to determine the size of the array.  So how do I define it without giving it a size?
Avatar of tmp09

ASKER

Does it even matter?? Will the computer just allocate more memory if it needs it?
Or will it lock up if it runs out of array space?
>>Will the computer just allocate more memory if it needs it?

Not automatically, you have to tell the computer to do it .. through malloc() etc .

/abhijit/
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You'll also have to seed the random number generator using a call to randomize() or srand().Otherwise,you'll get the same values everytime.

you can use int a[] only when you initialize the values in the declaration itself.
for e.g.

int a[]={1,2,3,4};
it will automatically allocate an array of size 4.

but when u dont initialize the values,you have to provide the array size,or use a pointer and allocate mem. dynamically using malloc.
Hi,

you cannot define an array without giving it a size. so following are the way by which you can declare an array.

1) directly give some size.
int randarray[256];

2) thorugh #define
#define SIZE 256
int randarray[SIZE];

if you want to do run time allocation
3) through malloc
printf("Enter the number of integers you wish to sort.\n");
scanf("%d",&full);
int *ranarray = (int *)malloc(sizeof(int) * full) ;

4)If you are working in linux or compiler which support C99 standard.
printf("Enter the number of integers you wish to sort.\n");
scanf("%d",&full);
int array[full] ;


But I suggest you tio make use of method 3.

or if you are working in C++ you can use vectors its a dynamic array

Dennis


What was wrong with my answer?