Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

What's wrong with this?

Posted on 2004-05-02
8
Medium Priority
?
303 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 46

Accepted Solution

by:
Kent Olsen earned 500 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Suggested Courses

572 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