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

Need help with arrays

1
I'm trying to make an array to hold the item name of 12 items:

Here's what I did

char itemname[12][32]= ("Milk", "Cereals", "Shampoo", "Facial Tissue", "Miller Lite", "Laundry Detergent", "Navel Oranges", "Heineken", "Seedless Grapes", "Dry Dog Food", "Diapers", "Chardonnay Wine");

When it compiles it says "Invalid Initializer".


What am I doing wrong?

And how would I call these separate arrays when its correct?
Oct 4 '08 #1
6 1304
You're using paranthesis instead of brackets.
Oct 4 '08 #2
Tassos Souris
152 100+
The correct way to do this is:

Expand|Select|Wrap|Line Numbers
  1. char *itemname[] = {  "Milk", "Cereals", "Shampoo", "Facial Tissue", "Miller Lite", "Laundry Detergent", "Navel Oranges", "Heineken", "Seedless Grapes", "Dry Dog Food", "Diapers", "Chardonnay Wine"
  2. };
  3.  
I used an array of pointers to strings to save space.
I also need not state the number of elements of the array. This gives me more flexibility to add more strings in the array.
Oct 5 '08 #3
donbock
2,426 Expert 2GB
I suggest a couple of changes to post #3 by Tassos Souris:

1. Declare itemname as "const char * const". That prevents you from ever trying to write into the literal strings. Doing so is nonportable because whether literal strings are writeable is implementation dependent. It also prevents you from changing any itemname entries to point at different strings.

2. It is helpful to have some variable that holds the number of entries in the itemname array.

..... 2a. If your compiler warns you when you have too few initializers for an array then the following snippet works for you. Such a warning is not mandated by the Standard.
Expand|Select|Wrap|Line Numbers
  1. #define NUMITEMS 12
  2. const char * const itemname[NUMITEMS] = {  "Milk", "Cereals", "Shampoo", ..., "Chardonnay Wine" };
..... 2b. The following technique is portable, but you have to be careful to only use the ARRAYSIZE macro with actual arrays, never with pointers.
Expand|Select|Wrap|Line Numbers
  1. #define ARRAYSIZE(a) ( (int)(sizeof(a) / sizeof(a[0])) )
  2. const char * const itemname[NUMITEMS] = {  "Milk", "Cereals", "Shampoo", ..., "Chardonnay Wine" };
  3. const int numitems = ARRAYSIZE(itemname);
Oct 6 '08 #4
Tassos Souris
152 100+
I suggest a couple of changes to post #3 by Tassos Souris:

1. Declare itemname as "const char * const". That prevents you from ever trying to write into the literal strings. Doing so is nonportable because whether literal strings are writeable is implementation dependent. It also prevents you from changing any itemname entries to point at different strings.

2. It is helpful to have some variable that holds the number of entries in the itemname array.

..... 2a. If your compiler warns you when you have too few initializers for an array then the following snippet works for you. Such a warning is not mandated by the Standard.
Expand|Select|Wrap|Line Numbers
  1. #define NUMITEMS 12
  2. const char * const itemname[NUMITEMS] = {  "Milk", "Cereals", "Shampoo", ..., "Chardonnay Wine" };
..... 2b. The following technique is portable, but you have to be careful to only use the ARRAYSIZE macro with actual arrays, never with pointers.
Expand|Select|Wrap|Line Numbers
  1. #define ARRAYSIZE(a) ( (int)(sizeof(a) / sizeof(a[0])) )
  2. const char * const itemname[NUMITEMS] = {  "Milk", "Cereals", "Shampoo", ..., "Chardonnay Wine" };
  3. const int numitems = ARRAYSIZE(itemname);
I totally agree with the second but i thought that if someone were to access an array at least he/she knows the problems about indexing and getting off the bounds of arrays and the rest of the stuff, so he/she would put a guard in the form of #define :-P

As far as the first is concerned, it really does not matter, because as you said you don't know what the hell the compiler will do. Some implementations, treat multiple occurrences of the same literal strings as one in memory not allowing you to change it, but some as distinct which are the most likely to let you change it... :-P
Personally, i put the first const.. i miss the second even though now it seems like a good idea (not that it will make a difference cause const does nothing...)

Thanks
Oct 7 '08 #5
donbock
2,426 Expert 2GB
Personally, i put the first const.. i miss the second even though now it seems like a good idea (not that it will make a difference cause const does nothing...)
If by 'const does nothing' you mean 'const can be trivially defeated by casting' then I have to agree. If you mean 'const is worthless' then I disagree.

Advantages of using const:

1. It acts as a comment to the maintenance programmer indicating my intentions regarding whether a particular variable should be changed.

2. Liberal use of const reduces the number of nuisance const/non-const warnings from using functions in the standard library.

3. The compiler will warn me if I accidentally write into a const variable.

4. Static analysis tools (like lint) may warn me if I deliberately write into a const variable by casting away const-ness or omitting 'const' from the extern in one source file.

5. On some compilers, if the moon is full and my heart is pure, declaring a variable as 'static const' with an initializer might maybe cause that variable to be defined in the code segment. This would speed up my program by the tiniest bit and activate a run-time error if my program writes into the variable (whether accidentally or deliberately). I can't rely on this behavior, but no harm in taking advantage of it when it is available.
Oct 7 '08 #6
Tassos Souris
152 100+
If by 'const does nothing' you mean 'const can be trivially defeated by casting' then I have to agree. If you mean 'const is worthless' then I disagree.

Advantages of using const:

1. It acts as a comment to the maintenance programmer indicating my intentions regarding whether a particular variable should be changed.

2. Liberal use of const reduces the number of nuisance const/non-const warnings from using functions in the standard library.

3. The compiler will warn me if I accidentally write into a const variable.

4. Static analysis tools (like lint) may warn me if I deliberately write into a const variable by casting away const-ness or omitting 'const' from the extern in one source file.

5. On some compilers, if the moon is full and my heart is pure, declaring a variable as 'static const' with an initializer might maybe cause that variable to be defined in the code segment. This would speed up my program by the tiniest bit and activate a run-time error if my program writes into the variable (whether accidentally or deliberately). I can't rely on this behavior, but no harm in taking advantage of it when it is available.
you can see the related topic about const ( something called "how this is possible in c(const variable)" ) to see my view on const...
Oct 7 '08 #7

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

Similar topics

5
by: Golf Nut | last post by:
I am finding that altering and affecting values in elements in multidimensional arrays is a huge pain in the ass. I cannot seem to find a consistent way to assign values to arrays. Foreach would...
41
by: Psykarrd | last post by:
I am trying to declare a string variable as an array of char's. the code looks like this. char name; then when i try to use the variable it dosn't work, however i am not sure you can use it...
2
by: Pasacco | last post by:
dear I want to ask help on this problem. Array a is partitioned into a0 and a1 in main(). Then a1 is partitioned into a2 and a3 in th_partition() function. And I think this problem is something...
1
by: John Smith | last post by:
I have a two dimentional char array. Before filling it using strtok(), I reset its elements to '\0' using two nested for loops. The code works as I hope it would but I wonder whether I really need...
2
by: Thomas Connolly | last post by:
Anyone know if there is a C# equivallent to: enum { LIFFE_SIZE_AUTOMARKETREF = 15 }; typedef char LiffeAutoMarketReference ; Thanks,
8
by: hothead098 | last post by:
ASSIGNMENT (4) USING AND MANIPUPATING ARRAYS (Chapter 10 material) For this assignment you are to: 1) Create and manage arrays a) One of type integers (containing 10 elements). b) One of...
23
by: vinod.bhavnani | last post by:
Hello all, I need desperate help Here is the problem: My problem today is with multidimensional arrays. Lets say i have an array A this is a 4 dimensional static array.
1
by: rllioacvuher | last post by:
I need help with a program. I have implemented that following header file with an unordered list using one array, but i need to be able to use an ordered list and 2 arrays (one for the links and one...
6
by: junk | last post by:
Senerio: I have a custom user control which contains two control arrays. The user control has a group box in which the two control arrays are dynamically built. The two control arrays are...
0
by: sumalats | last post by:
Hello, I need to use Visual basic to acquire some data through the serial port, store it etc. As I am just getting acquainted with VB6 I need some help. My Controller board (8 bit micro) sends...
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: 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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.