Solved

What's wrong with this?

Posted on 2004-05-02
8
290 Views
Last Modified: 2010-05-18
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)

0
Comment
Question by:tmp09
8 Comments
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 10973254
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();
0
 

Author Comment

by:tmp09
ID: 10973279
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?
0
 

Author Comment

by:tmp09
ID: 10973316
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?
0
 
LVL 11

Expert Comment

by:avizit
ID: 10973402
>>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/
0
Scale it in WD Gold

With up to ten times the workload capacity of desktop drives, WD Gold hard drives employ advanced technology to deliver among the best in reliability, capacity, power efficiency and performance.

 
LVL 45

Accepted Solution

by:
Kdo earned 125 total points
ID: 10973429
Hi tmp09,

With just a small change (two lines), your code can do what you want.  Define randarray[] as a pointer and reserves space when you know how big it will be.

#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);

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

  return 0;
}

Note that you should probably install "sanity checks" to make sure that "full" is a rational size.


Good Luck,
Kent

0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10974222
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.
0
 
LVL 5

Expert Comment

by:dennis_george
ID: 10975104
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


0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 10977016
What was wrong with my answer?
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
using pointers to pointers to write to a two dimensional array 16 88
Grammars for C C++ and java 1 114
Global Keyboard Hooks Blocked 4 70
Super Scope, DHCP 5 54
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now