Solved

What's wrong with this?

Posted on 2004-05-02
8
295 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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:
Kent Olsen 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

717 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