![]() |
| | #1 |
| Registered User Join Date: Jan 2008
Posts: 124
| size of 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 | |
| | #2 |
| Wheres the lesbians? 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 | |
| | #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 | |
| | #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:
| |
| Elysia is offline | |
| | #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 | |
| | #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 array size be what the user enters now? |
| goran00 is offline | |
| | #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:
| |
| Elysia is offline | |
| | #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 | |
| | #9 |
| Registered User Join Date: Jan 2008
Posts: 124
| thats y i am trying to get some help here |
| goran00 is offline | |
| | #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 | |
| | #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:
| |
| Elysia is offline | |
| | #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 | |
| | #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;
}
it point to the line: int* makeAnArray(size){ how do i fix that? |
| goran00 is offline | |
| | #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;
}
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:
Last edited by Elysia; 04-02-2008 at 08:39 AM. | |
| Elysia is offline | |
| | #15 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
-- 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |