Skip to content

Instantly share code, notes, and snippets.

@HappyCerberus
Created March 28, 2012 12:02
Show Gist options
  • Save HappyCerberus/2225673 to your computer and use it in GitHub Desktop.
Save HappyCerberus/2225673 to your computer and use it in GitHub Desktop.
Prevod jednorozmerne - dvourozmerne pole
/* Copyright (c) 2012 Mgr. Simon Toth (kontakt@simontoth.cz)
* Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
*/
#include <stdio.h>
#include <string.h>
union multipole
{
int pole1[35];
int pole2[7][5];
};
int main()
{
union multipole x;
memset(&x,0,sizeof(x)); // vynulovani
x.pole1[10] = 10;
// nasledujici pristup je podle C99 nedefinovane chovani
// novy standard C1X pravidlo upresnuje tak, ze nasledujici
// radek je validni. Vetsina kompilatoru by mela projit bez problemu.
printf("%d\n",x.pole2[2][0]);
/* Pro upresneni v C99 je pristup k polozce unionu, jine nez je ta
* ktera byla naposled zapsana definovan jako UB (undefined behaviour).
* C1X to upresnuje tak, ze takovy pristup je UB pouze tehdy, pokud se
* pristupuje k bytum, ktere nebyly pri posledni zapisu pouzity.
*
* Jinak receno, pokud sizeof(zapsana_polozka) >= sizeof(ctena_polozka) neni to UB.
*/
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment