Why infinite loop for input elements, bigger than single digit.
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.
Thanks, but had no idea that only the first element of the array (i.e., spacing) would be picked up; if not specified the dimension of the array. Seems due to one pass compilation, C-language has this constraint. But, wouldn't there be garbage value for the variable: total_elements, rather than just containing the value = 1, each time the code is compiled & run (or might be it is specific to the www.onlinegdb.com compiler only); or is it that if the static array's dimension is un-specified, then only the first element is taken? Confused about that part.
What do you mean by static array?
It is all about the runtime value of total_elements, and the fact that it is indeterminate when first used. So you cannot make any assumptions about the runtime state of the spacing array either. If you think you saw some "default" C language behavior compensating for your error, you are likely mistaken.
Quote:
Originally Posted by ajiten
Also, if the last line of the code below is changed to the one shown below, then the array contents are not displayed. The ordering of the two statements (i.e., '++i', & 'printf(" %d ", *(int *) vals[i])') in the 3rd part of the for-loop is important, but unclear as why that matters?:
EDIT: I believe NevemTeve may have part of the actual answer to this part of your question in the next post.
Consider also that putting the printf(...) after increment of the loop control variable in the update expression may have unintended consequences. Using your example:
The complete update expression, including the printf(...) will be executed before the incremented loop control is tested and will likely result in a missed first array member and at least one printf(...) execution (and use of illegal array index) beyond the bounds of the loop. It is confusing at best.
I think you have more than one problem with that loop construct and should probably rethink it. Again, I would suggest working that out in isolation from the rest of your code.
Last edited by astrogeek; 07-25-2023 at 09:55 PM.
Reason: Corrected second part answer ref.
> Also, if the last line of the code below is changed to the one shown below, then the array contents are not displayed. The ordering of the two statements (i.e., '++i', & 'printf(" %d ", *(int *) vals[i])') in the 3rd part of the for-loop is important, but unclear as why that matters?
There is a difference between printing vals[0]..vals[num-1] and printing elements vals[1]..vals[num], especially that there is no vals[num], that is an invalid index.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.