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

array initlisation

I fisrt define an array and then give values to its elements.

float MATRIX2D_1[3][3]

/*other stuff*/

float MATRIX2D_1[3][3] = {1/9,-1/9,1/9}, {1/9,-1/9,1/9}, {1/9,-1/9,1/9}};

I get a "parse error before float" for the second line. Does antone now why ?

Thanks,

Daniel
Oct 11 '06 #1
8 1825
D_C
293 100+
Each array can only be identified once. Get rid of the first declaration, since the second one has the data. Also you need an extra curly brace at the beginning, so the declaration is { {...} {...} {...} }.
Oct 11 '06 #2
You can use as below:
Expand|Select|Wrap|Line Numbers
  1. float MATRIX2D_1[3][3] = { {1/9,-1/9,1/9}, {1/9,-1/9,1/9}, {1/9,-1/9,1/9} };
  2.  
  3. or
  4.  
  5. float MATRIX2D_1[3][3] = { 1/9, -1/9, 1/9, 1/9, -1/9, 1/9, 1/9, -1/9, 1/9 };
  6.  
Oct 12 '06 #3
Each array can only be identified once. Get rid of the first declaration, since the second one has the data. Also you need an extra curly brace at the beginning, so the declaration is { {...} {...} {...} }.

Thanks a lot (!)

And if I want the matrix to be available throught the whole code, can I declare it as a macro (#define MATRIX2D_1 float arrayMRT[9][9]) or shall I define a globale varibale before the main() funtion (float MATRIX2D_1 arrayMRT[9][9];) ?
Oct 12 '06 #4
Thanks a lot (!)

And if I want the matrix to be available throught the whole code, can I declare it as a macro (#define MATRIX2D_1 float arrayMRT[9][9]) or shall I define a globale varibale before the main() funtion (float MATRIX2D_1 arrayMRT[9][9];) ?
If you're writing in C++, don't use #define for a global variable. As a macro, all #define does is replace MATRIX2D_1 with float. It takes the characters of the 1st argument, and replaces them with the characters of the 2nd argument.

Better to define a global variable in C++ as:
Expand|Select|Wrap|Line Numbers
  1. //declare arrayMRT[9][9]
  2. public float arrayMRT[9][9];
  3.  
  4. public int main()
  5. {
  6. //Initialize arrayMRT[9][9]
  7. arrayMRT[9][9] = { {....}{....}{....}{....}{....}{....}{....}{....}{....} };
  8.  
  9. return 1;
  10. }
Of course, you might want to avoid global variables alltogether but passing 2D arrays can be tricky.

- Miles
Oct 12 '06 #5
OK, thanks. I am writting in C, how would you then do it ?


If you're writing in C++, don't use #define for a global variable. As a macro, all #define does is replace MATRIX2D_1 with float. It takes the characters of the 1st argument, and replaces them with the characters of the 2nd argument.

Better to define a global variable in C++ as:
Expand|Select|Wrap|Line Numbers
  1. //declare arrayMRT[9][9]
  2. public float arrayMRT[9][9];
  3.  
  4. public int main()
  5. {
  6. //Initialize arrayMRT[9][9]
  7. arrayMRT[9][9] = { {....}{....}{....}{....}{....}{....}{....}{....}{....} };
  8.  
  9. return 1;
  10. }
Of course, you might want to avoid global variables alltogether but passing 2D arrays can be tricky.

- Miles
Oct 12 '06 #6
Ok, the way I wrote it before should work for C. A friend of mine reminded me that "const" was not available in C which doesn't matter anyway if you want to change the values at some point.

Try it out and see, I don't have a C compiler on me.

- Miles
Oct 12 '06 #7
Banfa
9,065 Expert Mod 8TB
A friend of mine reminded me that "const" was not available in C
const has been available in C since 1989 but is not as useful as it is in C++ because in some places in C it is still treated like a modifiable variable even though it is const (switch case statements for instance).
Oct 12 '06 #8
Thanks for the info Banfa.

Never programmed in C if it isn't already obvious...

- Miles
Oct 12 '06 #9

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

Similar topics

2
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
2
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
15
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
8
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
8
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
35
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
11
by: deko | last post by:
I need to create a basic one-dimensional array of strings, but I don't know how many strings I'm going to have until the code is finished looping. pseudo code: Dim astrMyArray() Do While Not...
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...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.