C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-02-2008, 08:02 AM   #1
Registered User
 
Join Date: Jan 2008
Posts: 124
size of array

i am trying to have the user chose the size of the array
what am i doing wrong?
Code:
#include <stdio.h>
#include <stdlib.h>

main (){
int size;
int array [size];

printf("Please enter the size of the array", size);
scanf("%i", size);
system("pause");
}
goran00 is offline   Reply With Quote
Old 04-02-2008, 08:04 AM   #2
Wheres the lesbians?
 
mike_g's Avatar
 
Join Date: Oct 2006
Location: UK
Posts: 1,219
Well for a start 'size' is undefined when you declare the array.
mike_g is offline   Reply With Quote
Old 04-02-2008, 08:05 AM   #3
Jack of many languages
 
Join Date: Nov 2007
Location: Katy, Texas
Posts: 1,929
Two things are wrong.

First, it's against the "law". When you declare "array", size is uninitialized. If you want a random sized array, you should malloc a chunk of storage from the heap.

Second, your scanf should pass the address of size, not size itself.

Todd
__________________
Mac and Windows cross platform programmer. Ruby lover.

Memorable Quotes From Recent Posts:

I can't remember.
Dino is offline   Reply With Quote
Old 04-02-2008, 08:07 AM   #4
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Main returns int.
Size contains garbage at the point you try to allocate the array, so theoretically it could get an undefined size. Secondly, this is only allowed in C99 and not C89.
Thirdly, C is a linear programming language. One statement is executed after the other.
Do the array won't be created after you've asked the size, it will be created before because the line is before the input.
Fourthly, I think you need to take another look at printf.
And avoid system("pause"). Better to use getchar().
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 04-02-2008, 08:07 AM   #5
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
1. It's not part of C89 standard to use variable size arrays. To be portable to C89 compilers, you should use dynamic memory allocation.

2. You can not declare a variable size array before the size component has been assigned. Right now, when you are saying "int size;" , size contains some "random" value, and then try to create an array of that size. [The "random" value is usually the same for the same piece of code each time, but it's random in the sense that it's not defined by any standard and it may well change if you introduce another variable before it, change your code in some other way, change the compile options, etc, etc]

So, if you want to use the C99 feature of variable sized arrays, you should at least set the size before creating the array.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 04-02-2008, 08:09 AM   #6
Registered User
 
Join Date: Jan 2008
Posts: 124
Code:
#include <stdio.h>
#include <stdlib.h>

main (){
int size;
int array [1];

printf("Please enter the size of the array", size);
scanf("%i", size);
array[size];
system("pause");
}
would this work?
would array size be what the user enters now?
goran00 is offline   Reply With Quote
Old 04-02-2008, 08:11 AM   #7
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
It would be 1.
As I mentioned, it's linear. It won't just jump back and set a new size.
And array[size] just access the element size in the array.

Fix main and system("pause"), then go learn dynamic memory, malloc & free.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 04-02-2008, 08:12 AM   #8
Registered User
 
Join Date: Jan 2008
Posts: 124
i have read my book but its too complicated so i am not understanding much of it
goran00 is offline   Reply With Quote
Old 04-02-2008, 08:13 AM   #9
Registered User
 
Join Date: Jan 2008
Posts: 124
thats y i am trying to get some help here
goran00 is offline   Reply With Quote
Old 04-02-2008, 08:13 AM   #10
Jack of many languages
 
Join Date: Nov 2007
Location: Katy, Texas
Posts: 1,929
Go learn this too: http://www.cppreference.com/stdio/scanf.html
__________________
Mac and Windows cross platform programmer. Ruby lover.

Memorable Quotes From Recent Posts:

I can't remember.
Dino is offline   Reply With Quote
Old 04-02-2008, 08:15 AM   #11
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Well, you need to understand pointers first.
Then you simply call malloc to allocate requested amount and when you don't need it, you call free.

Code:
int main()
{
    int* pArray;
    int size;

    printf("Enter size: ");
    scanf("%i", &size);
    pArray = malloc(size * sizeof(*pArray));
    free(pArray);
    pArray = NULL;

    getchar();
    return 0;
}
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 04-02-2008, 08:18 AM   #12
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Can I also point out that allocating or sizing arrays dynamicly is OFTEN not necessary - unless your application is large and/or uses A LOT of memory, just using "sufficiently large" arrays [e.g. an int array[10000]; will use 40KB of memory, which is not very much when your system has 10000x as much memory].

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 04-02-2008, 08:26 AM   #13
Registered User
 
Join Date: Jan 2008
Posts: 124
am i on the right path?
Code:
#include <stdio.h>
#include <stdlib.h>

main (){
int size;

printf("Please enter the size of the array", size);
scanf("%i", size);

makeAnArray(size);

system("pause");
}
int* makeAnArray(size){
	int* array;
array=calloc(size,sizeof(int));
return array;
}
what does error C2040: 'makeAnArray' : 'int *()' differs in levels of indirection from 'int ()' mean?
it point to the line: int* makeAnArray(size){
how do i fix that?
goran00 is offline   Reply With Quote
Old 04-02-2008, 08:29 AM   #14
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Sheesh. Start listening.
Code:
#include <stdio.h>
#include <stdlib.h>

// MAIN RETURNS INT
int main (){
    int size;
    // YOU HAVEN'T SPECIFIED ANYTHING FOR PRINTF TO PRINT, SO REMOVE THE "size" PART
    printf("Please enter the size of the array");
    // SCANF TAKES A POINTER
    scanf("%i", &size);

    makeAnArray(size);
    // DON'T USE SYSTEM("PAUSE")
    getchar();
}

// ALL ARGUMENTS MUST HAVE A TYPE
int* makeAnArray(int size){
    int* array;
    array=calloc(size,sizeof(int));
    return array;
}
You are also missing a prototype for makeAnArray.
And you are not freeing the return from calloc.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.

Last edited by Elysia; 04-02-2008 at 08:39 AM.
Elysia is offline   Reply With Quote
Old 04-02-2008, 08:34 AM   #15
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Quote:
Originally Posted by Elysia View Post
Sheesh. Start listening.
Code:
#include <stdio.h>
#include <stdlib.h>

// MAIN RETURNS INT
int main (){
    int size;
    printf("Please enter the size of the array", size);
    // SCANF TAKES A POINTER
    scanf("%i", &size);

    makeAnArray(size);
    // DON'T USE SYSTEM("PAUSE")
    getchar();
}

// ALL ARGUMENTS MUST HAVE A TYPE
int* makeAnArray(int size){
    int* array;
    array=calloc(size,sizeof(int));
    return array;
}
You are also missing a prototype for makeAnArray.
And you are not freeing the return from calloc.
ANd missing storing the return from makeAnArray().

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Invalid conversion from 'void*' to 'BYTE' help bikr692002 C++ Programming 9 02-22-2006 11:27 AM
any way to determine size of array thats a function parameter? fishjie C Programming 42 12-19-2004 08:59 AM
Unknown Memory Leak in Init() Function CodeHacker Windows Programming 3 07-09-2004 09:54 AM
Type and nontype parameters w/overloading Mr_LJ C++ Programming 3 01-02-2004 01:01 AM


All times are GMT -6. The time now is 05:51 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22