• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 05:52
CEST 11:52
KST 18:52
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
Team TLMC #5: Winners Announced!3[ASL20] Ro8 Preview Pt2: Holding On9Maestros of the Game: Live Finals Preview (RO4)5TL.net Map Contest #21 - Finalists5Team TLMC #5: Vote to Decide Ladder Maps!0
Community News
5.0.15 Patch Balance Hotfix (2025-10-8)60Weekly Cups (Sept 29-Oct 5): MaxPax triples up3PartinG joins SteamerZone, returns to SC2 competition285.0.15 Balance Patch Notes (Live version)119$2,500 WardiTV TL Map Contest Tournament 154
StarCraft 2
General
TL.net Map Contest #21 - Finalists PartinG joins SteamerZone, returns to SC2 competition 5.0.15 Patch Balance Hotfix (2025-10-8) Geoff 'iNcontroL' Robinson has passed away Classic Games #3: Rogue vs Serral at BlizzCon
Tourneys
Sparkling Tuna Cup - Weekly Open Tournament SC2's Safe House 2 - October 18 & 19 RSL Offline Finals Dates + Ticket Sales! SC4ALL $6,000 Open LAN in Philadelphia $2,500 WardiTV TL Map Contest Tournament 15
Strategy
Custom Maps
External Content
Mutation # 494 Unstable Environment Mutation # 493 Quick Killers Mutation # 492 Get Out More Mutation # 491 Night Drive
Brood War
General
Any rep analyzer that shows resources situation? Whose hotkey signature is this? BW General Discussion BGH Auto Balance -> http://bghmmr.eu/ I'm making videos again
Tourneys
[Megathread] Daily Proleagues [ASL20] Ro8 Day 4 Small VOD Thread 2.0 [ASL20] Ro8 Day 3
Strategy
BW - ajfirecracker Strategy & Training Siegecraft - a new perspective TvZ Theorycraft - Improving on State of the Art Current Meta
Other Games
General Games
Stormgate/Frost Giant Megathread Nintendo Switch Thread ZeroSpace Megathread Dawn of War IV Path of Exile
Dota 2
Official 'what is Dota anymore' discussion LiquidDota to reintegrate into TL.net
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
SPIRED by.ASL Mafia {211640} TL Mafia Community Thread
Community
General
US Politics Mega-thread The Games Industry And ATVI Stop the Construction YouTube Thread Things Aren’t Peaceful in Palestine
Fan Clubs
The herO Fan Club! The Happy Fan Club!
Media & Entertainment
Anime Discussion Thread [Manga] One Piece Movie Discussion!
Sports
2024 - 2026 Football Thread Formula 1 Discussion MLB/Baseball 2023 NBA General Discussion TeamLiquid Health and Fitness Initiative For 2023
World Cup 2022
Tech Support
SC2 Client Relocalization [Change SC2 Language] Linksys AE2500 USB WIFI keeps disconnecting Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List Recent Gifted Posts
Blogs
Inbreeding: Why Do We Do It…
Peanutsc
From Tilt to Ragequit:The Ps…
TrAiDoS
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1215 users

C Programming Practice - Page 4

Blogs > CecilSunkure
Post a Reply
Prev 1 2 3 4 All
obesechicken13
Profile Blog Joined July 2008
United States10467 Posts
November 05 2012 04:35 GMT
#61
On November 05 2012 13:31 frogmelter wrote:
Show nested quote +
On November 05 2012 13:25 obesechicken13 wrote:
1) Code a case to check if it is a prime number to begin with. If it isn't, then enter the loop

If I could do this I'd be a millionaire.


It can find the prime factors of 9,223,372,036,854,775,808 pretty fast [sub 5 seconds].

Lol. I don't know if you're trolling or if you misunderstand the problem. But That number above has only one prime factor: 2.


I'm not trolling.

Since the algorithm divides, it will come up with the answer pretty fast. The factors are 2^63, and every pass will divide it by 2. Meaning the loop only needs to run 63 times.

The problem occurs when you have a huge jump, like the first prime factor is like 6,000,000,000+ or so.

Ok. well you seem to know more than I gave you credit for then.

I'm not sure if it is possible to prove if a(any) number that large is prime in 5 seconds. I just heard from wikipedia that:
There is no known useful formula that yields all of the prime numbers and no composites.


http://en.wikipedia.org/wiki/Prime_number
I think in our modern age technology has evolved to become more addictive. The things that don't give us pleasure aren't used as much. Work was never meant to be fun, but doing it makes us happier in the long run.
frogmelter
Profile Blog Joined April 2009
United States971 Posts
Last Edited: 2012-11-05 04:38:21
November 05 2012 04:36 GMT
#62
On November 05 2012 13:35 obesechicken13 wrote:
Show nested quote +
On November 05 2012 13:31 frogmelter wrote:
On November 05 2012 13:25 obesechicken13 wrote:
1) Code a case to check if it is a prime number to begin with. If it isn't, then enter the loop

If I could do this I'd be a millionaire.


It can find the prime factors of 9,223,372,036,854,775,808 pretty fast [sub 5 seconds].

Lol. I don't know if you're trolling or if you misunderstand the problem. But That number above has only one prime factor: 2.


I'm not trolling.

Since the algorithm divides, it will come up with the answer pretty fast. The factors are 2^63, and every pass will divide it by 2. Meaning the loop only needs to run 63 times.

The problem occurs when you have a huge jump, like the first prime factor is like 6,000,000,000+ or so.

Ok. well you seem to know more than I gave you credit for then.

I'm not sure if it is possible to prove if a(any) number that large is prime in 5 seconds. I just heard from wikipedia that:
Show nested quote +
There is no known useful formula that yields all of the prime numbers and no composites.


http://en.wikipedia.org/wiki/Prime_number


http://en.wikipedia.org/wiki/Miller–Rabin_primality_test

Try that test

And yes, wikipedia is correct. What we are doing is a smarter brute force approach. At best we can try to mitigate the worst case run times, but there are some cases where the run time for the particular algorithm I posted will be O(sqrt(n)/2), which in some cases is still too high
TL+ Member
obesechicken13
Profile Blog Joined July 2008
United States10467 Posts
November 05 2012 04:37 GMT
#63
On November 05 2012 13:36 frogmelter wrote:
Show nested quote +
On November 05 2012 13:35 obesechicken13 wrote:
On November 05 2012 13:31 frogmelter wrote:
On November 05 2012 13:25 obesechicken13 wrote:
1) Code a case to check if it is a prime number to begin with. If it isn't, then enter the loop

If I could do this I'd be a millionaire.


It can find the prime factors of 9,223,372,036,854,775,808 pretty fast [sub 5 seconds].

Lol. I don't know if you're trolling or if you misunderstand the problem. But That number above has only one prime factor: 2.


I'm not trolling.

Since the algorithm divides, it will come up with the answer pretty fast. The factors are 2^63, and every pass will divide it by 2. Meaning the loop only needs to run 63 times.

The problem occurs when you have a huge jump, like the first prime factor is like 6,000,000,000+ or so.

Ok. well you seem to know more than I gave you credit for then.

I'm not sure if it is possible to prove if a(any) number that large is prime in 5 seconds. I just heard from wikipedia that:
There is no known useful formula that yields all of the prime numbers and no composites.


http://en.wikipedia.org/wiki/Prime_number


http://en.wikipedia.org/wiki/Miller–Rabin_primality_test

Try that test

No. Good night. I'll do sieve when I feel like it in the future.
I think in our modern age technology has evolved to become more addictive. The things that don't give us pleasure aren't used as much. Work was never meant to be fun, but doing it makes us happier in the long run.
frogmelter
Profile Blog Joined April 2009
United States971 Posts
November 05 2012 04:55 GMT
#64
On November 05 2012 13:37 obesechicken13 wrote:
Show nested quote +
On November 05 2012 13:36 frogmelter wrote:
On November 05 2012 13:35 obesechicken13 wrote:
On November 05 2012 13:31 frogmelter wrote:
On November 05 2012 13:25 obesechicken13 wrote:
1) Code a case to check if it is a prime number to begin with. If it isn't, then enter the loop

If I could do this I'd be a millionaire.


It can find the prime factors of 9,223,372,036,854,775,808 pretty fast [sub 5 seconds].

Lol. I don't know if you're trolling or if you misunderstand the problem. But That number above has only one prime factor: 2.


I'm not trolling.

Since the algorithm divides, it will come up with the answer pretty fast. The factors are 2^63, and every pass will divide it by 2. Meaning the loop only needs to run 63 times.

The problem occurs when you have a huge jump, like the first prime factor is like 6,000,000,000+ or so.

Ok. well you seem to know more than I gave you credit for then.

I'm not sure if it is possible to prove if a(any) number that large is prime in 5 seconds. I just heard from wikipedia that:
There is no known useful formula that yields all of the prime numbers and no composites.


http://en.wikipedia.org/wiki/Prime_number


http://en.wikipedia.org/wiki/Miller–Rabin_primality_test

Try that test

No. Good night. I'll do sieve when I feel like it in the future.


Good night, and good luck
TL+ Member
teamamerica
Profile Blog Joined July 2010
United States958 Posts
November 05 2012 07:45 GMT
#65
Hey can someone tell me if my thought process is right? I'm thinking of how to find if a number is prime given fermats little thm. which states a^p is equivalent to a in mod p (I'm taking a class which covers basic public key exchange).

Knowing that to figure if some p is prime it's just a matter of figuring a^p, and for large p you can use the fast powering algorithm. That splits finding a^p as finding the product to a raised to the binary expansion of p. I.e. a^5 becomes
a^(2^2) *a(2^0). Since this entire set of operations is done mod p, we never exceed p (for overflow - this is how pythons math.pow(x,y,z) works). So we end up with log2 multiplications to perform.

But on thinking about this, why how come you can't just use a=1 and then it would seem that every p is prime o.O...Anyone tell me what obvious part I'm doing wrong/missing?
RIP GOMTV. RIP PROLEAGUE.
mmp
Profile Blog Joined April 2009
United States2130 Posts
Last Edited: 2012-11-05 08:29:41
November 05 2012 08:25 GMT
#66
On November 05 2012 16:45 teamamerica wrote:
Hey can someone tell me if my thought process is right? I'm thinking of how to find if a number is prime given fermats little thm. which states a^p is equivalent to a in mod p (I'm taking a class which covers basic public key exchange).

Knowing that to figure if some p is prime it's just a matter of figuring a^p, and for large p you can use the fast powering algorithm. That splits finding a^p as finding the product to a raised to the binary expansion of p. I.e. a^5 becomes
a^(2^2) *a(2^0). Since this entire set of operations is done mod p, we never exceed p (for overflow - this is how pythons math.pow(x,y,z) works). So we end up with log2 multiplications to perform.

But on thinking about this, why how come you can't just use a=1 and then it would seem that every p is prime o.O...Anyone tell me what obvious part I'm doing wrong/missing?

Fermat's probable prime theorem is probable. You have to repeat the test for various moduli.

Even then, it's ridiculously fast to repeat ~100 times to get good confidence.

Note that Carmichael Numbers break Fermat's Little Theorem, but I believe the density of Carmichael numbers goes down for big numbers, so this can be ignored for applications like RSA.
I (λ (foo) (and (<3 foo) ( T_T foo) (RAGE foo) )) Starcraft
teamamerica
Profile Blog Joined July 2010
United States958 Posts
November 05 2012 12:41 GMT
#67
On November 05 2012 17:25 mmp wrote:
Show nested quote +
On November 05 2012 16:45 teamamerica wrote:
Hey can someone tell me if my thought process is right? I'm thinking of how to find if a number is prime given fermats little thm. which states a^p is equivalent to a in mod p (I'm taking a class which covers basic public key exchange).

Knowing that to figure if some p is prime it's just a matter of figuring a^p, and for large p you can use the fast powering algorithm. That splits finding a^p as finding the product to a raised to the binary expansion of p. I.e. a^5 becomes
a^(2^2) *a(2^0). Since this entire set of operations is done mod p, we never exceed p (for overflow - this is how pythons math.pow(x,y,z) works). So we end up with log2 multiplications to perform.

But on thinking about this, why how come you can't just use a=1 and then it would seem that every p is prime o.O...Anyone tell me what obvious part I'm doing wrong/missing?

Fermat's probable prime theorem is probable. You have to repeat the test for various moduli.

Even then, it's ridiculously fast to repeat ~100 times to get good confidence.

Note that Carmichael Numbers break Fermat's Little Theorem, but I believe the density of Carmichael numbers goes down for big numbers, so this can be ignored for applications like RSA.


Oh whoa, I never knew it was probable - also never knew about Carmicheal numbers (or that Fermats thm. failed at all TT). Well at least tomorrow when I ask my teacher some questions I'll look like I've actually been doing the reading!

Anyway, I saw some posts about learning C++ - I'm always an advocate for good books, esp. in languages where it's easy to do stuff the wrong way. So there's
http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
RIP GOMTV. RIP PROLEAGUE.
dakalro
Profile Joined September 2010
Romania525 Posts
November 05 2012 15:24 GMT
#68
On November 04 2012 17:29 spinesheath wrote:
Show nested quote +
On November 04 2012 07:25 Lysenko wrote:
On November 04 2012 04:15 CecilSunkure wrote:
On November 04 2012 02:09 Sawamura wrote:
now if could write some C++ program practice ^_^ hehehe thanks for the practice anyway 5/5

I could do that. I'm thinking of some object oriented C first though. You know, virtualizing some C++ features in C.


I was on my way to a much longer post, but I don't want to get in the way of what you're doing here with a lot of negativity, so I'll just ask. Who learns C today, absent a specific need to write embedded code, modify a UNIX or Linux kernel, or write a driver?

I'm speaking as someone who paid several years of bills as a C programmer, working on both embedded applications and desktop applications. This was, however, 17 years ago, before (as far as I have personally seen, which is why I'm asking) the entire world moved on to C++.



** I mean, I can see doing it out of nostalgia, or because I got a cheap deal on some single-chip-computer development board, but that's about it. Is this a topic of interest for typical, highly-motivated CS students these days? I'm not saying it's bad, I just don't get it.

You learn C to work with the crappy old codebase many companies still have. If you start a new project from scratch, you better have a damn good reason to use C. But if you work on something that uses old code, chances are it uses C.

That's no reason to start with C as your first language though.


I can tell you from 1st hand experience that you would miss your mobile phone functionality if C were to vanish now :D. C is here to stay and it will keep running the backend of a lot of stuff.
nath
Profile Blog Joined May 2010
United States1788 Posts
November 05 2012 16:56 GMT
#69
On November 04 2012 17:29 spinesheath wrote:
Show nested quote +
On November 04 2012 07:25 Lysenko wrote:
On November 04 2012 04:15 CecilSunkure wrote:
On November 04 2012 02:09 Sawamura wrote:
now if could write some C++ program practice ^_^ hehehe thanks for the practice anyway 5/5

I could do that. I'm thinking of some object oriented C first though. You know, virtualizing some C++ features in C.


I was on my way to a much longer post, but I don't want to get in the way of what you're doing here with a lot of negativity, so I'll just ask. Who learns C today, absent a specific need to write embedded code, modify a UNIX or Linux kernel, or write a driver?

I'm speaking as someone who paid several years of bills as a C programmer, working on both embedded applications and desktop applications. This was, however, 17 years ago, before (as far as I have personally seen, which is why I'm asking) the entire world moved on to C++.



** I mean, I can see doing it out of nostalgia, or because I got a cheap deal on some single-chip-computer development board, but that's about it. Is this a topic of interest for typical, highly-motivated CS students these days? I'm not saying it's bad, I just don't get it.

You learn C to work with the crappy old codebase many companies still have. If you start a new project from scratch, you better have a damn good reason to use C. But if you work on something that uses old code, chances are it uses C.

That's no reason to start with C as your first language though.

maybe in germany, in the US old codebases hardly ever use C
Founder of Flow Enterprises, LLC http://flow-enterprises.com/
nath
Profile Blog Joined May 2010
United States1788 Posts
November 05 2012 16:57 GMT
#70
On November 06 2012 00:24 dakalro wrote:
Show nested quote +
On November 04 2012 17:29 spinesheath wrote:
On November 04 2012 07:25 Lysenko wrote:
On November 04 2012 04:15 CecilSunkure wrote:
On November 04 2012 02:09 Sawamura wrote:
now if could write some C++ program practice ^_^ hehehe thanks for the practice anyway 5/5

I could do that. I'm thinking of some object oriented C first though. You know, virtualizing some C++ features in C.


I was on my way to a much longer post, but I don't want to get in the way of what you're doing here with a lot of negativity, so I'll just ask. Who learns C today, absent a specific need to write embedded code, modify a UNIX or Linux kernel, or write a driver?

I'm speaking as someone who paid several years of bills as a C programmer, working on both embedded applications and desktop applications. This was, however, 17 years ago, before (as far as I have personally seen, which is why I'm asking) the entire world moved on to C++.



** I mean, I can see doing it out of nostalgia, or because I got a cheap deal on some single-chip-computer development board, but that's about it. Is this a topic of interest for typical, highly-motivated CS students these days? I'm not saying it's bad, I just don't get it.

You learn C to work with the crappy old codebase many companies still have. If you start a new project from scratch, you better have a damn good reason to use C. But if you work on something that uses old code, chances are it uses C.

That's no reason to start with C as your first language though.


I can tell you from 1st hand experience that you would miss your mobile phone functionality if C were to vanish now :D. C is here to stay and it will keep running the backend of a lot of stuff.

very true
Founder of Flow Enterprises, LLC http://flow-enterprises.com/
RoyGBiv_13
Profile Blog Joined August 2010
United States1275 Posts
November 05 2012 18:31 GMT
#71
On November 06 2012 01:56 nath wrote:
Show nested quote +
On November 04 2012 17:29 spinesheath wrote:
On November 04 2012 07:25 Lysenko wrote:
On November 04 2012 04:15 CecilSunkure wrote:
On November 04 2012 02:09 Sawamura wrote:
now if could write some C++ program practice ^_^ hehehe thanks for the practice anyway 5/5

I could do that. I'm thinking of some object oriented C first though. You know, virtualizing some C++ features in C.


I was on my way to a much longer post, but I don't want to get in the way of what you're doing here with a lot of negativity, so I'll just ask. Who learns C today, absent a specific need to write embedded code, modify a UNIX or Linux kernel, or write a driver?

I'm speaking as someone who paid several years of bills as a C programmer, working on both embedded applications and desktop applications. This was, however, 17 years ago, before (as far as I have personally seen, which is why I'm asking) the entire world moved on to C++.



** I mean, I can see doing it out of nostalgia, or because I got a cheap deal on some single-chip-computer development board, but that's about it. Is this a topic of interest for typical, highly-motivated CS students these days? I'm not saying it's bad, I just don't get it.

You learn C to work with the crappy old codebase many companies still have. If you start a new project from scratch, you better have a damn good reason to use C. But if you work on something that uses old code, chances are it uses C.

That's no reason to start with C as your first language though.

maybe in germany, in the US old codebases hardly ever use C


As an american engineer who codes entirely in C, I disagree.

Also, I help build a corporate C compiler, ask me anything about those silly optimizations you want, though, under the hood its really just black magic.
Any sufficiently advanced technology is indistinguishable from magic
Lysenko
Profile Blog Joined April 2010
Iceland2128 Posts
November 05 2012 19:50 GMT
#72
On November 06 2012 00:24 dakalro wrote:
Show nested quote +
On November 04 2012 17:29 spinesheath wrote:
On November 04 2012 07:25 Lysenko wrote:
On November 04 2012 04:15 CecilSunkure wrote:
On November 04 2012 02:09 Sawamura wrote:
now if could write some C++ program practice ^_^ hehehe thanks for the practice anyway 5/5

I could do that. I'm thinking of some object oriented C first though. You know, virtualizing some C++ features in C.


I was on my way to a much longer post, but I don't want to get in the way of what you're doing here with a lot of negativity, so I'll just ask. Who learns C today, absent a specific need to write embedded code, modify a UNIX or Linux kernel, or write a driver?

I'm speaking as someone who paid several years of bills as a C programmer, working on both embedded applications and desktop applications. This was, however, 17 years ago, before (as far as I have personally seen, which is why I'm asking) the entire world moved on to C++.



** I mean, I can see doing it out of nostalgia, or because I got a cheap deal on some single-chip-computer development board, but that's about it. Is this a topic of interest for typical, highly-motivated CS students these days? I'm not saying it's bad, I just don't get it.

You learn C to work with the crappy old codebase many companies still have. If you start a new project from scratch, you better have a damn good reason to use C. But if you work on something that uses old code, chances are it uses C.

That's no reason to start with C as your first language though.


I can tell you from 1st hand experience that you would miss your mobile phone functionality if C were to vanish now :D. C is here to stay and it will keep running the backend of a lot of stuff.


To all the folks who keep bringing up embedded applications in response to my comments about C largely not being used today (and the comments on my comments) please note that embedded applications are one of the specific examples (among others) I mentioned that would benefit from learning C.
http://en.wikipedia.org/wiki/Lysenkoism
]343[
Profile Blog Joined May 2008
United States10328 Posts
Last Edited: 2012-11-05 22:04:44
November 05 2012 22:02 GMT
#73
bros bros there exist polynomial-time primality tests like http://en.wikipedia.org/wiki/AKS_primality_test

factoring, on the other hand, is not known to be in P (which is why RSA hasn't been broken, yet)

(in fact a lot of cryptography relies on stuff which is not proven, such as: "factoring is hard"; "discrete log is hard"; and "there exists a pseudorandom number generator" [which is true if "discrete log is hard"])

On November 05 2012 17:25 mmp wrote:
Fermat's probable prime theorem is probable. You have to repeat the test for various moduli.


hmm, I guess it's probabilistic if you choose random guys a and take a^n-1... but yeah the converse of Fermat's Little Theorem is not necessarily true.
Writer
obesechicken13
Profile Blog Joined July 2008
United States10467 Posts
November 06 2012 03:48 GMT
#74
On November 06 2012 07:02 ]343[ wrote:
bros bros there exist polynomial-time primality tests like http://en.wikipedia.org/wiki/AKS_primality_test

Wiki says the original test had a worst case of Õ(log^12(n))
So that's like log(log(log(log...log(n)))))...)))
12 iterated logarithms. I had to go on wolfram to brush up on iterated logarithms. In that case checking for primes is pretty damn fast.

The Euler problem in question though was considered the third easiest question on the site. I highly doubt everyone who solved the problem knew AKS primality test considering the next few problems look really easy in comparison.
I think in our modern age technology has evolved to become more addictive. The things that don't give us pleasure aren't used as much. Work was never meant to be fun, but doing it makes us happier in the long run.
icystorage
Profile Blog Joined November 2008
Jollibee19350 Posts
November 08 2012 13:13 GMT
#75
my solution to #5 used a simple bubble sort :D
+ Show Spoiler +

#include <stdio.h> /* printf */

/* Swap the contents of two pointers. Do not modify the prototype.
Use your function from PointerPractice_3 here. */
void SwapInts(int *a,int *b )
{
/* Place your code here. Modify these lines. */
int temp = *a;
*a = *b;
*b = temp;
}

/* Print contents of an array. Do not modify this function. */
void PrintArray( const int* array, const int size )
{
int i;
for(i = 0; i < size; ++i, ++array)
{
if(i != size - 1)
printf( "%d, ", *array);
else
printf( "%d\n", *array);
}
}

int main( void )
{
/* Create array on the stack. */
int stackArray[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

/* Get size of array. stackArray can be treated as pointer to first element. */
int arraySize = sizeof( stackArray ) / sizeof( *stackArray );

/* Can treat name of an array as pointer to first element. */
PrintArray( stackArray, arraySize );

/* Reverse the array here */
/**/
/**/
/**/
int i, j;
for (i = (arraySize - 1); i > 0; i--)
{
for (j = 1; j <= i; j++)
{
if (stackArray[j-1] < stackArray[j])
{
SwapInts(&stackArray[j-1], &stackArray[j]);
}
}
}

PrintArray( stackArray, arraySize );

/* Correct output:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 */

return 0;
}
LiquidDota StaffAre you ready for a Miracle-? We are! The International 2017 Champions!
SeeDLiNg
Profile Joined January 2010
United States690 Posts
December 19 2012 18:04 GMT
#76
So, I know what it's finding... But I don't really know how it's finding it...

int arraySize = sizeof( stackArray ) / sizeof( *stackArray );

Can someone break down how exactly this is finding the arraySize for me please?
CecilSunkure
Profile Blog Joined May 2010
United States2829 Posts
December 19 2012 21:05 GMT
#77
On December 20 2012 03:04 SeeDLiNg wrote:
So, I know what it's finding... But I don't really know how it's finding it...

int arraySize = sizeof( stackArray ) / sizeof( *stackArray );

Can someone break down how exactly this is finding the arraySize for me please?

Sure!

So with an array on the stack C actually knows the number of elements in the array. So when you do sizeof( stackArray ) the stackArray has not yet implicitly decayed into a pointer, and is actually being treated as a proper array. C multiplies the size of a single element in the array by the number of arrays, and this returns the number of bytes the entire array takes up within memory.

So then sizeof( *stackArray ) is implicitly decaying the stackArray identifier into a pointer and then dereferencing it. The value of *stackArray is that of the first element in the array, since an array's identifier can always be implicitly decayed into a pointer to the first element. So then the size of the first element is 4 bytes: the size of the integer first element.

Then you divide the size of bytes in total by the size in bytes of the first element to get the number of elements total.
Prev 1 2 3 4 All
Please log in or register to reply.
Live Events Refresh
Next event in 8m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
mouzHeroMarine 387
SortOf 132
StarCraft: Brood War
Larva 988
Shuttle 926
Hyuk 691
Mini 293
PianO 287
Leta 287
Stork 228
Pusan 202
sSak 88
EffOrt 85
[ Show more ]
ToSsGirL 64
Sharp 58
Sacsri 54
Backho 53
Yoon 38
Movie 35
soO 33
yabsab 26
Shine 26
NotJumperer 23
Free 17
ivOry 13
ajuk12(nOOB) 10
Dota 2
XcaliburYe954
Counter-Strike
Stewie2K1044
allub298
Super Smash Bros
Westballz22
Heroes of the Storm
Khaldor248
Other Games
singsing1590
Mew2King46
Organizations
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 15 non-featured ]
StarCraft 2
• LUISG 35
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• WagamamaTV9
League of Legends
• Jankos2093
• Stunt516
• HappyZerGling151
Upcoming Events
Sparkling Tuna Cup
8m
CranKy Ducklings3
Map Test Tournament
1h 8m
Zoun vs Spirit
Reynor vs herO
Clem vs MaxPax
OSC
2h 8m
IPSL
9h 8m
Bonyth vs Art_Of_Turtle
Razz vs rasowy
Afreeca Starleague
1d
Barracks vs Snow
Afreeca Starleague
2 days
Soma vs Bisu
OSC
2 days
OSC
2 days
The PondCast
4 days
OSC
4 days
[ Show More ]
CranKy Ducklings
6 days
Safe House 2
6 days
Liquipedia Results

Completed

Acropolis #4 - TS2
Maestros of the Game
HCC Europe

Ongoing

BSL 21 Points
ASL Season 20
CSL 2025 AUTUMN (S18)
C-Race Season 1
IPSL Winter 2025-26
WardiTV TLMC #15
EC S1
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 2025
BLAST Bounty Fall Qual
IEM Cologne 2025

Upcoming

SC4ALL: Brood War
BSL Season 21
BSL 21 Team A
RSL Offline Finals
RSL Revival: Season 3
Stellar Fest
SC4ALL: StarCraft II
eXTREMESLAND 2025
ESL Impact League Season 8
SL Budapest Major 2025
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.