Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
Using ternary and summing array
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="James Kuyper, post: 4393321"] On 10/31/2011 07:11 PM, flebber wrote: .... No, that declaration REQUIRES that 'a' be a global variable. You can define it as a global variable in some other module, but you can never assign it; C doesn't allow assignment of arrays. Once you've defined the array, you can then assign values to each element of the array, separately. No, only the definition of 'a' can give it a size, and the size that it is given by the definition is the one and only correct size for that array. You can declare 'a' in multiple different modules, none of which know the size, but you need to make sure that none of those modules try to use a part of the array that doesn't exist. You need to either define or dynamically allocate an array of at least n ints, somewhere, but that array doesn't need to be defined with a name of 'a'. 'a' is simply the name of a local pointer variable that is initialized with a copy of the pointer value passed to three_five_sum() as it's first argument. Example: int main(void) { int bunny[256]; // Fill in values for bunny // Calculate the sum for the middle 128 elements of bunny: int sum = three_five_sum(bunny+64, 128); // rest of program } [i][i] This declares a function named two_pointer_args which takes two arguments, both of which are integers, not pointers. Contrary to what I think you were trying to do, the lines after this are NOT interpreted as a definition of that function. First of all, the ';' terminates the declaration, so everything after this point is part of the main() function body, and has nothing to do with two_pointer_args(). Secondly, even if you had left out the ';', it wouldn't have worked. You can declare the function within main() (though I think it's better to declare it outside of main()), and you can call it from main(), but you cannot define it inside main(); you have to define it somewhere else. That line does not do what you think it does. It retrieves the value of 'i', and then discards it. Then it sets j to 0. Read up on the comma operator. You're calculating two different totals. I was talking about calculating a single total. The whole point of my suggestion was to create an example where it would be natural to use the ?: ternary operator. If you had only one variable, named 'total', and if you had declared the arguments as pointers named a and b, then you could have written: if( i%3 == 0 || i%5 == 0) total += a[i]; else total += b[i]; Now, that code can be re-written to use the ?: operator, and it's a very natural thing to do. Figure out how, and you'll have a much better understanding of what that operator is all about.[/i][/i][/i][/i] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
Using ternary and summing array
Top