Uživatelské nástroje

Nástroje pro tento web


prog_c

Rozdíly

Zde můžete vidět rozdíly mezi vybranou verzí a aktuální verzí dané stránky.

Odkaz na výstup diff

Obě strany předchozí revizePředchozí verze
Následující verze
Předchozí verze
prog_c [2013/04/30 11:34] – [sizeOf] djbuldogprog_c [2025/08/28 10:09] (aktuální) – upraveno mimo DokuWiki 127.0.0.1
Řádek 34: Řádek 34:
 </code> </code>
  
 +==== Ukazatele ====
  
 +Read it backwards (as driven by Clockwise/Spiral Rule)...
 +
 +<code c>
 +int* // pointer to int
 +int const * // pointer to const int
 +int * const // const pointer to int
 +int const * const // const pointer to const int
 +</code>
 +
 +Now the first const can be on either side of the type so:
 +
 +<code c>
 +const int * // == int const *
 +const int * const // == int const * const
 +</code>
 +
 +If you want to go really crazy you can do things like this:
 +
 +<code c>
 +int ** // pointer to pointer to int
 +int ** const // a const pointer to a pointer to an int
 +int * const * // a pointer to a const pointer to an int
 +int const ** // a pointer to a pointer to a const int
 +int * const * const // a const pointer to a const pointer to an int
 +</code>
 +
 +And to make sure we are clear on the meaning of const
 +
 +<code c>
 +const int* foo;
 +int *const bar; //note, you actually need to set the pointer 
 +                //here because you can't change it later ;)
 +</code>
 +
 +foo is a variable pointer to a constant int. This lets you change what you point to but not the value that you point to. Most often this is seen with cstrings where you have a pointer to a const char. You may change which string you point to but you can't change the content of these strings. This is important when the string itself is in the data segment of a program and shouldn't be changed.
 +
 +bar is a const or fixed pointer to a value that can be changed. This is like a reference without the extra syntactic sugar. Because of this fact, usually you would use a reference where you would use a T* const pointer unless you need to allow null pointers.
 +
 +=== Operace ===
 +
 +  * jsou závislé na typu
 +  * přičítání
 +
 +<code c>
 +void *p=0;
 +uint32_t *u=0;
 +
 +printf("%d", p);   // 0
 +printf("%d", ++p); // 1 !!
 +
 +printf("%d", u);   // 0
 +printf("%d", ++u); // 4 !!
 +</code>
 +
 +  * rozdíl
 +
 +<code c>
 +void *p=0, *p2=4;
 +uint32_t *u=0, *u2=4;
 +
 +printf("%d", p2-p);  // 4
 +printf("%d", u2-u);  // 1 !!
 +</code>
 +
 +==== Pole znaků ====
 +
 +  * definice 2d pole znaků
 +
 +<code c>
 +  char tmp[3][3] = {{ 'a', 'b', '\0' }, { 'A', 'B', '\0' }};
 +</code>
 +
 +  * definice s využitím string literálu (null term se přidává automaticky)
 +
 +<code c>
 +  char tmp[3][3] = { "ab", "AB" };
 +</code>
 +
 +  * počet prvků v poli lze vynechat
 +
 +<code c>
 +  char tmp[][3] = { "ab", "AB" };
 +  tmp[0][0] = '\0'
 +</code>
 +
 +  * ale... tohle už není platný zápis
 +
 +<code c>
 +  char tmp[][] = { "ab", "AB" };
 +</code>
 +
 +  * pole musí byt dostatečně velké (pro nejdelší prvek)
 +  
 +<code c>
 +  char tmp[][5] = { "ab", "ABCD" };
 +</code>
 +
 +  * inicializace 1d pole ukazatelů na pole znaků, které má ukazatele inicializováno na konstantní string literály
 +  * pokud o zápis do pole znaků způsobí sagmentation fault, jelikož paměť je readonly
 +
 +<code c>
 +  char *tmp[2];
 +  tmp[0] = "219.bari2.eu:8080";
 +  tmp[1] = "ffff:123:ff:111:";
 +</code>
 +
 +<code c>
 +  char *tmp[]={
 +    "219.bari2.eu:8080",
 +    "ffff:123:ff:111:"
 +  };
 +</code>
 +
 +  * definice pole znaků - hodnoty lze měnit
 +
 +<code c>
 +  char tmp[] = "abc";;
 +  tmp[0] = 'a';
 +</code>
 +
 +  * definice ukazatele na literál - hodnoty nelze měnit (způsobí segmentation fault), protože jsou uložený v .txt části programu
 +
 +<code c>
 +  char *tmp2 = "abc";
 +  tmp2[1] = 'a';
 +</code>
 +
 +  * více info [[http://www.cplusplus.com/doc/tutorial/ntcs|zde]]
prog_c.1367314463.txt.gz · Poslední úprava: (upraveno mimo DokuWiki)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki