473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

how to declare an array that takes any dimension specified by the user

HI,
i want to declare an array that should accept any size given by the user.how to declare such array?please explain me with the help of a code..it should display on the screen "enter the no of rows" and" enter the no of columns" and it should accept any size specified by the user.
Apr 4 '07 #1
6 2131
gpraghuram
1,275 Expert 1GB
Hi,
You cant declare dynamic array,but u can achieve it using pointers.
Expand|Select|Wrap|Line Numbers
  1. char **myArray;
  2. myArray=(char**)malloc(sizeof(char*)*(ROWSIZE));
  3. for(i=0;i<ROWSIZE;i++)
  4. {
  5.      myArray[i]=(char*)malloc(COLSIZE);
  6. }
  7.  
Here ROWSIZE and COLSIZE are the inputs given by the user dynamically.

Thanks
Raghuram
Apr 4 '07 #2
Hi,
You cant declare dynamic array,but u can achieve it using pointers.
Expand|Select|Wrap|Line Numbers
  1. char **myArray;
  2. myArray=(char**)malloc(sizeof(char*)*(ROWSIZE));
  3. for(i=0;i<ROWSIZE;i++)
  4. {
  5.      myArray[i]=(char*)malloc(COLSIZE);
  6. }
  7.  
Here ROWSIZE and COLSIZE are the inputs given by the user dynamically.

Thanks
Raghuram
thankyou sir, but is it not at all possible to simple arrays to achieve that.i dont want to use pointers for this,isn't there any other way of doing it?
Apr 4 '07 #3
gpraghuram
1,275 Expert 1GB
HI,
Array size are needed during compile time, so u cant specify the size during run time.
U can achieve this only using pointers in C.

Thanks
Raghuram
Apr 4 '07 #4
HI,
Array size are needed during compile time, so u cant specify the size during run time.
U can achieve this only using pointers in C.

Thanks
Raghuram
the array that i have taken is an image.the image i have saved using .TXT format and actually i have to read the image values and classify that and finally i have to store the result in another array.the image i have is 400 by 800.but my program to read the array should be generic so that if i have a bigger image to read, it shouldn't create any problem and accept the user desired value.one more thing that is i declare an array of size[120][120],it is taking,but not more than that.and if i declare size[10][2000],then it is accepting.why is it that if i decrease the value of row and increase the value of column it is taking?
Apr 4 '07 #5
HI,
Array size are needed during compile time, so u cant specify the size during run time.
U can achieve this only using pointers in C.

Thanks
Raghuram
the array that i have taken is an image.the image i have saved using .TXT format and actually i have to read the image values and classify that and finally i have to store the result in another array.the image i have is 400 by 800.but my program to read the array should be generic so that if i have a bigger image to read, it shouldn't create any problem and accept the user desired value.one more thing that is i declare an array of size[120][120],it is taking,but not more than that.and if i declare size[10][2000],then it is accepting.why is it that if i decrease the value of row and increase the value of column it is taking? please help me with this confusion.thankyou.
Apr 4 '07 #6
the array that i have taken is an image.the image i have saved using .TXT format and actually i have to read the image values and classify that and finally i have to store the result in another array.the image i have is 400 by 800.but my program to read the array should be generic so that if i have a bigger image to read, it shouldn't create any problem and accept the user desired value.one more thing that is i declare an array of size[120][120],it is taking,but not more than that.and if i declare size[10][2000],then it is accepting.why is it that if i decrease the value of row and increase the value of column it is taking? please help me with this confusion.thankyou.
actually system doesnt knw abt ur size.... it only knw aboutsizes in bytes only. if u declare as like size[9][9] then we can store 9*9 values.... lik wis if u declare as lik [120][120] then may be it wil take much more spaces... but reason is this only......
Apr 4 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Michael Drumheller | last post by:
(If you're not interested in NumArray, please skip this message.) I am new to NumArray and I wonder if someone can help me with array-indexing. Here's the basic situation: Given a rank-2 array...
4
by: Linda | last post by:
How can I declare a two dimention array of character with dynamical first dimention and static second dimention using mix of pointer* and . I think "char *A" means first dimention is static and...
2
by: raxitsheth | last post by:
I am using array in my progrm... is there any tool /tips so that i can convert my program to Dynamic Array...so that I can Add more Element to Array.... Code is around 4000 line 'C' (not C++)...
1
by: Krish | last post by:
How do you find out the number of rows in the array {Array} I have ..... thanks
10
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to...
5
by: Martin Pöpping | last post by:
Hello, I want to iterate the second dimension of a 2-dim-array. Let´s say I have an array: double myArray and a given index: int i. Assume my index is given in want to iterate my array...
4
by: ottawajn | last post by:
Hi, There, I want to declare a two-dimension array by "float coeftemp1 ;" in Dev c++. It doesn't work. But, if I change it to "float coeftemp1 ;". It works. Could any one know the reason? ...
9
by: =?Utf-8?B?RGFya21hbg==?= | last post by:
Hi, I am wondering how you multi-dimension an array function? My declared function looks like this: Public Function GetCustomerList(ByVal Val1 As String, ByVal Val2 As Long, ByVal Val3 As...
2
by: yeshello54 | last post by:
so here is my problem...in a contact manager i am trying to complete i have ran into an error..we have lots of code because we have some from class which we can use...anyways i keep getting an error...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.