473,385 Members | 1,780 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.

I'd like more information on using arrays in C

3
Hi,
could you please give me some more info on how to use arrays in C??

and also, could you tell me whether this statement is correct or not:

void do_child(int data_pipe[][],int i)
close(data_pipe[i][1]);
Dec 1 '07 #1
13 1518
int a[10];

it's a kind of array from int type..
it has 10 index & it's name is a
Dec 1 '07 #2
Hi,
could you please give me some more info on how to use arrays in C??

and also, could you tell me whether this statement is correct or not:

void do_child(int data_pipe[][],int i)
close(data_pipe[i][1]);
why in the world do you approach advanced topics like InterProcess Communication if you don't even know what an array is???
An array variable stores the first addres of a set of contiguous memory locations where a discrete number of bytes(or words,or double words) is stored.the type of an array variable is therefore a pointer.Please read Ritchie and Kernighan ANSI C book
Dec 1 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
Read this and then post again if you still have questions:
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***.

With the above in mind this array:
Expand|Select|Wrap|Line Numbers
  1. int array[10] = {0,1,2,3,4,5,6,7,8,9};
  2.  
has a memory layout of

0 1 2 3 4 5 6 7 8 9

Wheras this array:
Expand|Select|Wrap|Line Numbers
  1. int array[5][2] = {0,1,2,3,4,5,6,7,8,9};
  2.  
has a memory layout of

0 1 2 3 4 5 6 7 8 9

Kinda the same, right?

So if your disc file contains

0 1 2 3 4 5 6 7 8 9

Does it make a difference wheher you read into a one-dimensional array or a two-dimensional array? No.

Therefore, when you do your read use the address of array[0][0] and read as though you have a
one-dimensional array and the values will be in the correct locations.
Dec 1 '07 #4
Does it make a difference wheher you read into a one-dimensional array or a two-dimensional array? No.
that's because the C language uses a row major order matrix representation.
Dec 1 '07 #5
zubbaz
3
why in the world do you approach advanced topics like InterProcess Communication if you don't even know what an array is???
An array variable stores the first addres of a set of contiguous memory locations where a discrete number of bytes(or words,or double words) is stored.the type of an array variable is therefore a pointer.Please read Ritchie and Kernighan ANSI C book

i know HOW TO DEAL with ARRAYS AND I KNOOOW what arrays are. all this stuff ur giving me.. well i already know it, ive studies C++ and i know all the basics. the problem is that i keep on getting some kind of error on the arrays in my code and i just want to know if theres any difference AT ALL between arrays in C and arrays in C++. u guys keep on giving me very simple info on arrays. as if i dont know what they are!! and besides, u didnt even tell me whether those two code lines were consider correct or not, which was mainly my question :)
Dec 2 '07 #6
oler1s
671 Expert 512MB
Apparently, you don't know how to do any of the following: write acceptable English, Google, read properly, ask proper detailed questions, show respect to those who actually try to help you, and finally, consider the fact that it's your sloppy coding that might be causing errors.

If you had googled, you would have discovered that C and C++ treat arrays the same.

If you are having trouble dealing with an error, you could post relevant code and the error here.

u didnt even tell me whether those two code lines were consider correct or not, which was mainly my question :)
How are we supposed to know? Outwardly, the syntax looks fine. You might have bungled it up, passing the wrong type of parameter to your defined functions.

We're here to help, but you have no excuse to act offended if you get the wrong answers because you can't write proper English and you can't ask the right question.
Dec 2 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
that's because the C language uses a row major order matrix representation.
That has nothing to do with it. row-major is a database table concept.

In C and C++ the array elements are required to be contiguous in memory and the size of the element must be known at compile time. Otherwise, the compiler cannot perform the necessary pointer aritmetic needed to navigate the array.

and i just want to know if theres any difference AT ALL between arrays in C and arrays in C++.
There is absolutely no difference between arrays in C and arrays in C++ just like oler1s said.
Dec 2 '07 #8
That has nothing to do with it. row-major is a database table concept.
Please,open Patterson Hennessy,Computer Architecture:A Quantitative Approach
Chapter 5:"Compiler Optimizations to ReduceMiss Rate" and read it carefully,very carefully........
Dec 2 '07 #9
bonzo
2
That has nothing to do with it. row-major is a database table concept.
Row major order is a methods for storing multidimensional arrays. Rows in a matrix are stored consecutively (a[0][0], a[0][1], a[0][2],...a[0][n], a[1][0], a[1][1],...a[m][n]).

So, just you have already said, you can access elements increasing a pointer.

Row Major Order <=> Column Major Order

Swap the concepts... :)
Dec 2 '07 #10
weaknessforcats
9,208 Expert Mod 8TB
There is a disconnect here since C and C++ only have one-dimensional arrays.

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***.

With the above in mind this array:
Expand|Select|Wrap|Line Numbers
  1. int array[10] = {0,1,2,3,4,5,6,7,8,9};
  2.  
has a memory layout of

0 1 2 3 4 5 6 7 8 9

Wheras this array:
Expand|Select|Wrap|Line Numbers
  1. int array[5][2] = {0,1,2,3,4,5,6,7,8,9};
  2.  
has a memory layout of

0 1 2 3 4 5 6 7 8 9

Kinda the same, right?

So if your disc file contains

0 1 2 3 4 5 6 7 8 9

Does it make a difference wheher you read into a one-dimensional array or a two-dimensional array? No.

Therefore, when you do your read use the address of array[0][0] and read as though you have a
one-dimensional array and the values will be in the correct locations.
Dec 3 '07 #11
There is a disconnect here since C and C++ only have one-dimensional arrays.

Read this:
'Father, forgive them, for they do not know what they are doing' " (23:33-34)
Dec 3 '07 #12
bonzo
2
There is a disconnect here since C and C++ only have one-dimensional arrays.
Read this:
this is blasphemy this is madness...
So array of integer doesn't exist because they are four-dimensional array of byte, isn't it?

This is not a C/C++ approach: this is assembly.

Yes, all memory is a huge one-dimensional array of byte, but in a high level language we talk about n-dimensional arrays. In C they are not the same. For compilers it makes difference (type checking).

Only brutal cast can break the rule, but this is discouraged.

P.S: Sorry for my English.
Dec 3 '07 #13
the truth must be visible to everyone in this forum,my dear weakness,do not try again to hide the truth;you can make it once,maybe twice...but how long are you supposed to do it?Accept the fact that you are wrong,and PLEASE do not try again to make a thread float down just to make it not visible to the others.
Say hi to phanepip and begin to study,seriously.
Dec 4 '07 #14

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

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
42
by: Dan | last post by:
Hello, I have trouble with class calling. I am calling getvolume() with succes in the function CreateCircle but it do not want to call it in ShowCircle() function. I am staying in the same...
0
by: adavidso | last post by:
I am a bit of a newbie to python, and would like a little advice on how to more efficiently process arrays. I work a lot with satellite data, and have opened and read two binary images into...
2
by: ssubbarayan | last post by:
Dear all, I would like to learn regarding Bit arrays from basics up to implementation level.Typically I am looking for understanding their utility,their implications and how they are different...
5
by: Arjen | last post by:
Hello, I have an array "aPeople" with people objects which I serialize like this: XmlSerializer x = new XmlSerializer( typeof(People) ); TextWriter writer = new StreamWriter( "data.xml" );...
0
by: Markus Bertheau | last post by:
В Пнд, 02.08.2004, в 14:49, Federico Di Gregorio пишет: > On lun, 2004-08-02 at 14:30 +0200, Markus Bertheau wrote: > > Hi, > > > > I wonder if there is some kind of support for...
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
2
by: kimi | last post by:
Hi ppl, I am new to PHP. I would need some information on the following: 1. a) I wanted to know from where the data is extracted and stroed in the global assocoative arrays ( specifically...
24
by: =?Utf-8?B?U3dhcHB5?= | last post by:
Can anyone suggest me to pass more parameters other than two parameter for events like the following? Event: Onbutton_click(object sender, EventArgs e)" Event handler: button.Click += new...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.