473,385 Members | 1,343 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,385 software developers and data experts.

doubt in array

Good morning friends...

is it possible to store one array to another array if it is like this


s[][]=s1[];
s[1][1]=s1[];

what will happen for this code.
Oct 26 '07 #1
5 1433
Ganon11
3,652 Expert 2GB
As I'm sure weaknessforcats will explain in fuller detail, there are actually no two-dimensional arrays, or any other dimension arrays in C++. All these things are is single dimension arrays of arrays. For instance,

Expand|Select|Wrap|Line Numbers
  1. int matrix[5][5];
Here you may recognize matrix as a 2-dimensional array. It is actually a 1 dimensional array, each element of which is a 1 dimensional array, each element of which is an int. So when you write

Expand|Select|Wrap|Line Numbers
  1. matrix[4][2] = 2;
what are are saying is, "Access the 5th element of matrix (which is an array). Access the 3rd element of the resultant array (which is an int). Set this int to 2."

With that in mind, I'm fairly certain you can set an array equal to another array. So, if you had matrix as above, and you had an array myArray:

Expand|Select|Wrap|Line Numbers
  1. int myArray[5] = {1, 2, 3, 4, 5};
I think you can legally say:

Expand|Select|Wrap|Line Numbers
  1. matrix[1] = myArray;
I'm not exactly sure, but why not go to a C++ compiler and try a simple program manipulating these things, and see what you get?
Oct 26 '07 #2
RRick
463 Expert 256MB
Be careful when working with multi-dimensional arrays and arrays of pointers. They may look the same, but they don't work the same.

In the above example, a 2D matrix was defined and then a row of its values were changed by another array. This isn't going to work. It would work if you were dealing with an array of pointers.
Expand|Select|Wrap|Line Numbers
  1. double arr[5] = { 1, 2, 3, 4, 5};
  2. double mat[5][5];
  3. double *  matp[5];
  4.  
  5. mat[1] = arr;      // Won't work
  6. matp[1] = arr;   // Works
  7.  
  8. //  Note how the syntax is the same
  9. mat[1][2] = 77;
  10. matp[1][2] = 77;
  11.  
With multi-dimensional arrays, the compiler does a lot of work that the user doesn't see. When accessing mat[1], the compiler will generate a temporary pointer to that location. With matp[1], it just returns the pointer. You can use those two pointers in the same way, but since the mat pointer is a temporary value, you can't assign it a value.
Oct 26 '07 #3
With that in mind, I'm fairly certain you can set an array equal to another array. So, if you had matrix as above, and you had an array myArray:

Expand|Select|Wrap|Line Numbers
  1. int myArray[5] = {1, 2, 3, 4, 5};
I think you can legally say:

Expand|Select|Wrap|Line Numbers
  1. matrix[1] = myArray;
I'm not exactly sure, but why not go to a C++ compiler and try a simple program manipulating these things, and see what you get?
sir i need to do in Java.... searching file concept i want to use the array and it complex less.

if s is an array...
s[5]=s is it possible ?
Oct 27 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
s[][]=s1[];
s[1][1]=s1[];
To start with, this won't compile. s[][] is invalid syntax in ANSI/ISO 1998 C++.

f s is an array...
s[5]=s is it possible ?
Probably not. An array is not a type. s[5] is a type. You cannot assign a non-type to a type.

Read this:
First, there are only one-dimensional arrays in C or C++. The number of elements in put between brackets:
Expand|Select|Wrap|Line Numbers
  1. int array[5];
  2.  
That is an array of 5 elements each of which is an int.

Expand|Select|Wrap|Line Numbers
  1. int array[];
  2.  
won't compile. You need to declare the number of elements.

Second, this array:
Expand|Select|Wrap|Line Numbers
  1. int array[5][10];
  2.  
is still an array of 5 elements. Each element is an array of 10 int.

Expand|Select|Wrap|Line Numbers
  1. int array[5][10][15];
  2.  
is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.


Expand|Select|Wrap|Line Numbers
  1. int array[][10];
  2.  
won't compile. You need to declare the number of elements.

Third, the name of an array is the address of element 0
Expand|Select|Wrap|Line Numbers
  1. int array[5];
  2.  
Here array is the address of array[0]. Since array[0] is an int, array is the address of an int. You can assign the name array to an int*.

Expand|Select|Wrap|Line Numbers
  1. int array[5][10];
  2.  
Here array is the address of array[0]. Since array[0] is an array of 10 int, array is the address of an array of 10 int. You can assign the name array to a pointer to an array of 10 int:
Expand|Select|Wrap|Line Numbers
  1. int array[5][10];
  2.  
  3. int (*ptr)[10] = array;
  4.  
Fourth, when the number of elements is not known at compile time, you create the array dynamically:

Expand|Select|Wrap|Line Numbers
  1. int* array = new int[value];
  2. int (*ptr)[10] = new int[value][10];
  3. int (*ptr)[10][15] = new int[value][10][15];
  4.  
In each case value is the number of elements. Any other brackets only describe the elements.

Using an int** for an array of arrays is incorrect and produces wrong answers using pointer arithmetic. The compiler knows this so it won't compile this code:

Expand|Select|Wrap|Line Numbers
  1. int** ptr = new int[value][10];    //ERROR
  2.  
new returns the address of an array of 10 int and that isn't the same as an int**.

Likewise:
Expand|Select|Wrap|Line Numbers
  1. int*** ptr = new int[value][10][15];    //ERROR
  2.  
new returns the address of an array of 10 elements where each element is an array of 15 int and that isn't the same as an int***.
Oct 27 '07 #5
hi

mr.weaknessforcats i am very thank full to you.....great explanation...



thank you very much
Oct 28 '07 #6

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

Similar topics

1
by: Guilherme Pinto | last post by:
Hello. I am reading the book written by Bjarne Stroustrup called " The C++ Programming Language - Special Edition" and had a doubt which a think is really important to distinguish between the...
138
by: ambika | last post by:
Hello, Am not very good with pointers in C,but I have a small doubt about the way these pointers work.. We all know that in an array say x,x is gonna point to the first element in that...
20
by: K.M. Jr. | last post by:
Hi all, Consider this line - char s = "\0"; Does this initialize all array elements to zero ? Thanks.
20
by: maadhuu | last post by:
firstly, i am thankful to all those who answered the 1st set of doubts. And i am not yet enlightened to that extent , coz ' i keep getting doubts. is the following defined in the language ?? int...
5
by: Baskar RajaSekharan | last post by:
Hi, I have one doubt in c_sharp. Please let me know is there any Collection or Dictonery having the Sort facility. I want to sort a list of Numbers. how to do that? For Example i am going...
1
by: Solli Moreira Honorio | last post by:
Hi, I've a method that return a array, and my doubt is how i'll create a array variable that will receive the return if I don't know the size of return. There are another way to do this...
13
by: deepak | last post by:
Hi In the following function how the memory 'll be allocated. 1) Will it allocate memory for all the char's together or allocate for first char. then for int then for float and after this only...
5
by: subramanian | last post by:
Consdier the following program: #include <stdio.h> struct typeRecord { int id; char str; } records = { {0, "zero"}, {1, "one"},
29
by: swapna.annamaneni | last post by:
hi, i tried with this simple code int array,i; for(i=-1;i<=sizeof(array);i++) but the condition is failing always even array size is greater than zero. can u help me.............
5
by: nembo kid | last post by:
In the following function, s shouldn't be a pointer costant (array's name)? So why it is legal its increment? Thanks in advance. /* Code starts here */ void chartobyte (char *s) { while...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.