LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 07-24-2023, 08:44 PM   #16
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,267
Blog Entries: 24

Rep: Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195

Quote:
Originally Posted by ajiten View Post
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 View Post
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:

Code:
 for (int i=0; i<total_elements; ++i, printf(" %d ", *(int *) vals[i]))
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.
 
1 members found this post helpful.
Old 07-24-2023, 11:19 PM   #17
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Blog Entries: 1

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

Last edited by NevemTeve; 07-24-2023 at 11:20 PM.
 
Old 07-25-2023, 07:51 AM   #18
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,782

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by pan64 View Post
that printf is not invoked at all, because the comma operator cannot invoke functions, just evaluate expressions.
A function call is an expression.
 
1 members found this post helpful.
Old 07-28-2023, 08:52 AM   #19
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by ntubski View Post
A function call is an expression.
Very sorry, but you seem to contradict what is quoted in your answer.
 
Old 07-28-2023, 08:56 AM   #20
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
That's what one call 'correction'.
Anyways, could you solve the digit-counting problem?
 
  


Reply



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
LXer: 'This was bigger than GNOME and bigger than just this case.' GNOME Foundation exec director talks patent trolls and much, much more LXer Syndicated Linux News 0 10-24-2020 04:21 AM
[SOLVED] Read digit by digit in variable pedropt Programming 6 12-25-2017 04:24 AM
In Javascript How to replace elements in one object with elements from another object pizzipie Linux - Software 1 05-08-2014 02:28 AM
[SOLVED] infinite loop that isnt infinite? frieza Programming 2 10-27-2010 02:16 PM
Substitue single-digit, two-digit, and 3-digit numbers with text using sed dmason165 Programming 13 08-07-2009 10:38 AM

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

All times are GMT -5. The time now is 09:17 PM.

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