Solved

What's wrong with this?

Posted on 2004-05-02
8
291 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Where is my core dump file in Ubuntu? 12 449
Inorder binary search tree 5 166
Console based application in Linux 1 90
Why is compiler in oracle server ? 9 66
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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 recursion in the C programming language.

809 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