LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-04-2002, 10:39 AM   #1
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Rep: Reputation: 30
Help with 'while' loop operation .....


Hi. I am wondering the steps a while loop takes. I know it basically but in the following >

#include <stdio.h>

int main(void)
{
int array[] = { 1, 2, 3, 4, 5 };
int i = 0;


while(array[i++] != '\0')
printf("array[%d] contains %d\n", i, array[i]);

return 0;

}

my output was ....

array[1] contains 2
array[2] contains 3
array[3] contains 4
array[4] contains 5
array[5] contains -1073743052
array[6] contains -1073743144
array[7] contains 134513729
array[8] contains 1073834212
array[9] contains -1073743052
array[10] contains -1073743096
array[11] contains 1074038023
array[12] contains 1
array[13] contains -1073743052
array[14] contains -1073743044
array[15] contains 134513402
array[16] contains 134513952
array[17] contains 0

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.
 
Old 09-04-2002, 10:51 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
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
 
Old 09-04-2002, 10:57 AM   #3
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Original Poster
Rep: Reputation: 30
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.

Am I correct?

thanks
 
Old 09-04-2002, 11:02 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
erm yeah i guess. quite you want to delibertaly ignore the first value is pretty much beyond me, but nevermind...
 
Old 09-04-2002, 11:02 AM   #5
no2nt
Member
 
Registered: Aug 2001
Location: South Carolina, USA
Distribution: Redhat 8.0/Custom
Posts: 96

Rep: Reputation: 16
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.

Last edited by no2nt; 09-04-2002 at 11:04 AM.
 
Old 09-04-2002, 11:04 AM   #6
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
ahh don't give the sizeof problem away...! makes it more fun to find out!
 
Old 09-04-2002, 11:07 AM   #7
no2nt
Member
 
Registered: Aug 2001
Location: South Carolina, USA
Distribution: Redhat 8.0/Custom
Posts: 96

Rep: Reputation: 16
rofl. you don't know how much time i spent in highschool programming class tryin to figgur that out!
 
Old 09-04-2002, 11:12 AM   #8
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Original Poster
Rep: Reputation: 30
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?

thanks alot guys
 
Old 09-04-2002, 11:18 AM   #9
no2nt
Member
 
Registered: Aug 2001
Location: South Carolina, USA
Distribution: Redhat 8.0/Custom
Posts: 96

Rep: Reputation: 16
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.

Last edited by no2nt; 09-04-2002 at 11:21 AM.
 
Old 09-04-2002, 11:21 AM   #10
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
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...

e.g.
Code:
array = (int *) realloc (array, array_length * sizeof (int));
 
Old 09-04-2002, 11:23 AM   #11
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
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
 
Old 09-04-2002, 11:36 AM   #12
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Original Poster
Rep: Reputation: 30
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?

thanks and Im done for today ...
 
Old 09-04-2002, 11:44 AM   #13
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
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
 
Old 09-04-2002, 12:11 PM   #14
no2nt
Member
 
Registered: Aug 2001
Location: South Carolina, USA
Distribution: Redhat 8.0/Custom
Posts: 96

Rep: Reputation: 16
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
Code:
array = (int *) realloc (array, array_length * sizeof (int));
in order to get the (alloc'ed) size of the array, you'll need to store
array_length * sizeof(int) somewhere.

Granted, having the length of an array (char or int) already calculated is
very nice and great for efficiency, C just doesn't do it that way.

One could do the following (ah, the Pascal way!):
Code:
struct
{
    int length;
    int *array;
};
This is not intended to be a flame!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
su - operation not permitted jrtayloriv Linux - Newbie 2 06-09-2005 09:27 PM
Needs help with bittorent operation... Ausar Linux - Software 3 08-28-2004 06:38 PM
DVD Operation robhargreaves Linux - Hardware 2 05-10-2004 02:19 PM
operation not permitted jamaso Slackware 2 05-08-2004 05:55 AM
Operation: Wine Shinobi Linux - Software 2 04-14-2003 03:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration