ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
So in the 'while' loop I am guessing the order of operation is
first 'array[0] is 'evaluated' and then if it checks out TRUE it is 'augmented'
by one and THEN the 'printf' is executed. Am I right?
Which would also be the case with the 'i'.
So I came to conclusion or 'remembered' that the augmentation should take place
in the printf statement or somewhere within the 'while' loop braces. Right?
And bringing me to my second question ..
What does an int array terminate with? It obviously wasn't '\0'. Or did I do something wrong?
thanks alot
Last edited by purpleburple; 09-04-2002 at 10:45 AM.
1... no, that's total gibberish. array's start at 0 and not 1, everything else should fall into place then.
2... it doesn't terminate. once you get into pointers and all that junk, you'll see that technically an array is open ended, and you just get given the contents of the next memory space if the array has not been defined there. you should use a sizeof() operation really to ensure you know how long the array really is instead. see it's manpage for details
thanks acid I know arrays start at array[0] but looking at my output (totally skipping the printing of array[0]'s
contents to standard out) I would assume that while loop order is
It checks to see if array[0] does not equal '\0' (which it would never in this example) and THEN it augmets the 'i' in the array[i++] and after all that it prints array[1] = instead of array[0] = beacuse it would have augmented the i++ in the check part before moving on to the printf statement.
for integer arrays, you'll need to manually terminate them.
The first time through the loop array[0] is evaluated but
after that evaluation i is incremented to 1. So in order to
get the correct array values with the printf stmt, use i-1.
Code:
#include <stdio.h>
int main(void)
{
int array[] = { 1, 2, 3, 4, 5, -1 };
int i = 0;
while(array[i++] != -1)
printf("array[%d] contains %d\n", i-1, array[i-1]);
return 0;
}
Side note (using the example purple posted):
Be very careful using sizeof(array) in this situation. sizeof(array) will return
20 because there are 5 integers that are 4 bytes each. 5 (ints) * 4 (bytes) = 20. So (sizeof(array) / sizeof(int)) would work.
thanks alot guys .... but I think the simpler way to work thru this loop would have been to do this >
int array[] = { 1, 2, 3, 4, 5, -1 };
int i = 0;
while( array[i] != -1)
printf(array[%d] contains %d\n", i, array[i++]);
return 0;
}
or just put the augment of 'i' somewhere inside the 'while' loop instead of in the
printf statement.
So I guess another question is ..... if I have an array that holds say 100 'ints' and I ask
the user to input as many 'int's' as he or she wants and then I want to print 'only' those
array contents to the screen and NOT the entire 100 array elements I use sizeof() or something
similiar?
100 'ints' question:
using sizeof will return the length of the whole array. it will not
give you the length of the values the user inputted. You'll
want to terminate your array with a special value in order to
only print out the ints the user inputted.
side note:
once again :-) be careful about putting using the pre/post
increment operators or nested assignment stmts inside functions.
Code:
printf("%d %d\n", ++n, power(2, n));
produces different results on different machines.
Best to do
Code:
++n;
printf("%d %d\n", n, power(2, n));
Moral of the story: don't write code which depends on the order of evaluation.
well at that point you should get into the concepts of arrays and pointers. as arrays are of a fixed length (goign by the book at least) you should always really require them to be filled or whatever. if in your example the user can giev as many or as little values as they want then an array is a poor choice of data structure to use. You can resize array's pretty easily, but it's done by treating them as pointers, which is all they really are. BUT if that's what you're doing then you should just use it as a pointer from step 1...
oh and your idea of using a -1 at the end of an array is generally pretty poor, there are proper ways to do these things, cludgey solutions aren't needed
thanks alot guys .... you've been a tremendous help
As far as augmenting 'i' and WHEN it's done I did the following to find out >
#include <stdio.h>
int main(void)
{
int array[3] = { 5, 2, 3 };
int i = 0;
printf("array[%d] contains %d and i eqauls %d\n", i++, array[i], i);
printf("After leaving printf i eqauls %d\n", i);
return 0;
}
and the ouput was >
array[0] contains 5 and i eqauls 0
After leaving printf i eqauls 1
Now .... the ouput was not what I expected. I expected
array[0] contains 5 and i equals 1
After leaving printf i eqauls 1
The reason I expected the above was I thought that 'i' would be augmented and THEN the last 'i'
in the printf statement would equal 1 but it looks as though 'i' is not 'augmented' until 'after' it leaves
printf. Am I correct?
no because i++ is POST increment not pre,so one is added to it AFTER it's value has been given to the code. to get what you expected you would use ++i instead
As far as using -1 as a terminator in an array of ints I don't see how that
is cludgey at all (unless -1 must also be used).
Look at "strings" in C. It's an array of characters terminated by '\0' and
there exist standard functions for dealing with "strings".
If I haven't completely lost it, if you use
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.