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..
:-D
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.
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:
That is an array of 5 elements each of which is an int.
won't compile. You need to declare the number of elements.
Second, this array:
is still an array of 5 elements. Each element is an array of 10 int.
is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.
won't compile. You need to declare the number of elements.
Third, the name of an array is the address of element 0
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*.
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: -
int array[5][10];
-
-
int (*ptr)[10] = array;
-
Fourth, when the number of elements is not known at compile time, you create the array dynamically: -
int* array = new int[value][5];
-
int (*ptr)[10] = new int[value][10];
-
int (*ptr)[10][15] = new int[value][10][15];
-
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: -
int** ptr = new int[value][10]; //ERROR
-
new returns the address of an array of 10 int and that isn't the same as an int**.
Likewise: -
int*** ptr = new int[value][10][15]; //ERROR
-
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: -
int tictactoe[9] = {1,2,3,4,5,6,7,8,9};
-
has a memory layout of:
1 2 3 4 5 6 7 8 9
Whereas this array: -
int tictactoe[3][3] = {1,2,3,4,5,6,7,8,9};
-
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. -
fread( tictactoe, sizeof( int), 9, fp );
-
//or, in your case:
-
fread(&matrixPointer[0][0], sizeof(int), rows*columns, fp);
-
You can still use matrixPointer as the address of and array of row elements where each element is a columns.
arent all multidimensional arrays just arrays of arrays? thats how it was explained when i did java..
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.
'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.
never mind....bad post, it just looks way too similiar to my uni assignment, and yeah its hard to not think java, damn lecturer
int* array = new int[value][5];
gives an error, cannot convert int(*)[5] to int * in initialization in GCC using Dev-C++
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
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: -
int* array = new int[value];
-
and I have fixed it.
Thank you very much!
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]
Sign in to post your reply or Sign up for a free account.
Similar topics
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
|
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
|
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
|
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...
|
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,...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
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: 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...
|
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...
|
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,...
|
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...
| |