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

C code proof-read needed.. unsure about passing data

Hi, Im doing a unit in C and have written a program for my class as best as I can.. just I am having trouble undestanding all the different ways to pass data between modules and some of the syntax. could someone please look at my code below and tell me what I need to do to fix it or whether Ive got it right?
Its not hugely long or complex..

Expand|Select|Wrap|Line Numbers
  1. <code removed by admin>
  2.  
:-D
Oct 4 '07 #1
10 1434
Ive never used pointers before and this is my second C program overall.. so please.. be nice. Im used to java where you dont have to worry about alot of this.
Oct 4 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
You are aware that C has no multi-dimensional arrays. Yes?

What it has is 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][5];
  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 tictactoe[9] = {1,2,3,4,5,6,7,8,9};
  2.  
has a memory layout of:

1 2 3 4 5 6 7 8 9

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

1 2 3 4 5 6 7 8 9

Kinda the same, right?

So, when you read your array from the file, just read the entire array in as one block.
Expand|Select|Wrap|Line Numbers
  1. fread( tictactoe, sizeof( int), 9, fp );
  2. //or, in your case:
  3. fread(&matrixPointer[0][0], sizeof(int), rows*columns, fp);
  4.  
You can still use matrixPointer as the address of and array of row elements where each element is a columns.
Oct 4 '07 #3
arent all multidimensional arrays just arrays of arrays? thats how it was explained when i did java..
Oct 5 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
arent all multidimensional arrays just arrays of arrays? thats how it was explained when i did java..
That's the way I was explained to me in C and C++. It took several years for ti to dawn on me that it was only the first index that you could use to dynamically allocate an array. The other indexes had to be hard-coded. I finally understood that the other indexes just descibed the element. The first index always was the number of elements.

In memory, the array is just a sequential string of values. So

1 2 3 4 5 6 7 8 9 10

could be an array of [10] or [5][2] or [2][5] depending on how you wanted to use the values. For reading the array from disc, you just read 10 sequential values and you are done.
Oct 5 '07 #5
Studlyami
464 Expert 256MB
'Im not real familiar with C and I don't know if arrays are handled exactly the same or not, but in C++ the link below was a big help in understanding multidimensional arrays.
here . Look down about half way through the page where it states "pointers and arrays". Look at the examples given and hopefully it will make more sense. Also look up some pointer arithmetic for multidimensional arrays.
Oct 5 '07 #6
never mind....bad post, it just looks way too similiar to my uni assignment, and yeah its hard to not think java, damn lecturer
Oct 6 '07 #7
int* array = new int[value][5];

gives an error, cannot convert int(*)[5] to int * in initialization in GCC using Dev-C++
Oct 6 '07 #8
Banfa
9,065 Expert Mod 8TB
Dameon99, please read the Posting Guidelines and particularly the Coursework Posting Guidelines.

This is clearly a class assignment (you say so), we do not allow posting of the full code to a class assignment. This is to prevent it being copied and to prevent you being accused of copying by your professors.

Please take more care not to do this in future.

Banfa
Administrator
Oct 6 '07 #9
weaknessforcats
9,208 Expert Mod 8TB
int* array = new int[value][5];

gives an error, cannot convert int(*)[5] to int * in initialization in GCC using Dev-C++
You found a bug in my explanation. I keep this thing in a file so I can paste it as necessary. The code should have been:
Expand|Select|Wrap|Line Numbers
  1. int* array = new int[value];
  2.  
and I have fixed it.

Thank you very much!
Oct 6 '07 #10
not a problem at all, unfortunately although I do not work in C++ on a daily basis, I realized of late that I lost my understanding of fundamentals,

may be you could help me with the following:
[HTML]http://www.thescripts.com/forum/thread719257.html
[/HTML]

[HTML]http://www.thescripts.com/forum/thread717947.html[/HTML]
Oct 7 '07 #11

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

Similar topics

192
by: Vortex Soft | last post by:
http://www.junglecreatures.com/ Try it and tell me what's happenning in the Microsoft Corporation. Notes: VB, C# are CLS compliant
193
by: Vortex Soft | last post by:
http://www.junglecreatures.com/ Try it and tell me what's happenning in the Microsoft Corporation. Notes: VB, C# are CLS compliant
245
by: Vortex Soft | last post by:
http://www.junglecreatures.com/ Try it and tell me what's happenning in the Microsoft Corporation. Notes: VB, C# are CLS compliant
82
by: Edward Elliott | last post by:
This is just anecdotal, but I still find it interesting. Take it for what it's worth. I'm interested in hearing others' perspectives, just please don't turn this into a pissing contest. I'm in...
66
by: Jon Skeet [C# MVP] | last post by:
I'm sure the net will be buzzing with this news fairly soon, but just in case anyone hasn't seen it yet: Microsoft are going to make the source code for the .NET framework (parts of it,...
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: 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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.