2

say I have declare a struct

struct mystruct {
  char a[10];
  double b;
} 

struct mystruct array[20] = { 
   {'test1',1.0},
   {'test2',2.0}  <---- I just want to declare 2 items first because I am going to add new ones later.
};
int i;
for( i=0; array[i].a != NULL ;i++){ 
    ....  <--- so here I just want to display what is initialized first
} 

However, the for loop display past the 2 items ( ie to 20 items but all the rest are garbage ). I just want to display currently only what is initialized, even though i declared to store 20 of them. How to do it? thanks.

I am using C90 standard. Also, let's say I added more items in future , but still lesser than 20 items, i would just want to display until the "last item that is valid" .

10
  • 1
    That you're comparing array[i].a against NULL worries me significantly as to the code your not showing us in: "...initialize ok here with just 3 items". Commented Dec 2, 2013 at 2:58
  • 3
    You mean { "test1", 1.0 }, etc in the initializers; you're using a multi-character character literal, which is non-portable. Commented Dec 2, 2013 at 3:03
  • 2
    What a difference an array declaration makes. now your code is wrong. array[i].a will never be null. You want to check array[i].a[0] != 0, if I understand your intent. Oh, and try "test1" etc. I've a feeling you'll be more pleased with that. Commented Dec 2, 2013 at 3:03
  • 2
    @self.: the standard allows you to write a character literal containing multiple characters, but the result is implementation defined. ISO/IEC 9899:2011 §6.4.4.4 Character constants: ¶10 …The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined.… Commented Dec 2, 2013 at 3:16
  • 2
    Just use double quotes instead of single quotes. "test1" instead of 'test1'. Commented Dec 2, 2013 at 3:23

2 Answers 2

6

For compilers that will accept the initializer syntax (that should be any standard C compiler), you should be able to write:

struct mystruct
{
  char a[10];
  double b;
};  // semi-colon added!

struct mystruct array[20] =
{ 
   { "test1", 1.0 },  // character strings!
   { "test2", 2.0 },
};
enum { ARRAY_SIZE = sizeof(array) / sizeof(array[0]) };

int i;
for (i = 0; i < ARRAY_SIZE && array[i].a[0] != '\0'; i++)
{ 
    printf("[%s] => %f\n", array[i].a, array[i].b);
}
Sign up to request clarification or add additional context in comments.

2 Comments

+1 and a note to the OP. don't make a habit of this. It makes the code harder to read, and in particular your code becomes "update-brittle". Suppose instead of 20 elements you only had 3. now suppose someone follows you, sees the code and wants to add a third element. Well, look at that, they think... theres already space for it, so we'll just add it on the end. Suddenly you're walking into undefined behavior. If you're going to do this, make sure it is well-documented.
... and then Jonathan puts in the hard limit and makes my prior comment moot. =P heh. There you go again, writing solid-code ;-)
1

If you have only initialized three items, you usually have to remember that piece of information and do

for(i=0; i<3; i++) { ... }

In case you actively zeroed the rest of the array (in a for loop or by memset for example) or if you declared it as global variable (which are guaranteed to be zeroed by the compiler), you can do something similar to what you were trying to do:

for( i=0; array[i].a != '\0' ;i++){ ... }

Since array[i].a is a char, you should compare it to chars. Coincidentally, thanks to some implicit conversions (char to int), you should also be able to compare it to ints so

 for( i=0; array[i].a != 0 ;i++){ ... } 

should be OK as well. Transitively, you're NULL version should work too, since NULL is just a macro for 0 (on most computer architectures anyway), but you shouldn't be using that because the (human) convention is that it should only be used for empty pointers.

1 Comment

You may want to check the posted code again. While you're typing this things.... changed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.