Kód: Vybrat vše
$ cc -Wall -Wextra test.c -o test
test.c: In function ‘main’:
test.c:3:5: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits]Moderátor: Moderátoři
Kód: Vybrat vše
$ cc -Wall -Wextra test.c -o test
test.c: In function ‘main’:
test.c:3:5: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits]K tomu bych se vyjádřil. Mluvíme o céčku, předpokládám.Panda38 píše:S ukazatelem do dvourozměrného pole je potíž, že není jednoznačně dané, s jakou prioritou jsou indexy v paměti uložené (zda se mění nejdříve první nebo druhý index) - některé překladače to ukládají opačně (myslím že častější případ je měnit přednostně druhý index, ale nejsem si tím teď jistý).
Kód: Vybrat vše
int main(int argc, char** argv)
{
char p1[5] = {"ahoj"};
char p2[5] = {" "};
char* ptr1 = p1;
char* ptr2 = p2;
int i = 0;
// --------- pointery -------
while(*ptr1)
*ptr2++ = *ptr1++;
// --------- pole ------------
while(p1[i])
p2[i] = p1[i++];
return 0;
}
Example
Consider the array object defined by the declaration
int x[3][5];
Here x is a 3x5 array of int s; more precisely, x is an array of three
member objects, each of which is an array of five int s. In the
expression x[i] , which is equivalent to (*(x+(i))) , x is first
converted to a pointer to the initial array of five int s. Then i is
adjusted according to the type of x , which conceptually entails
multiplying i by the size of the object to which the pointer points,
namely an array of five int objects. The results are added and
indirection is applied to yield an array of five int s. When used in
the expression x[i][j] , that in turn is converted to a pointer to the
first of the int s, so x[i][j] yields an int.
Tohle mi přijde trochu divné. Spíš bych to psal takhle:mtajovsky píše:char p1[5] = {"ahoj"};
Kód: Vybrat vše
char p1[5] = "ahoj";Tohle mi přijde trochu nebezpečné. U operátoru přiřazení se výslovně píše:mtajovsky píše:p2[i] = p1[i++];
The order of evaluation of the operands is unspecified.
To je složitější. Ačkoliv projde:masterboy píše:Takze co z toho plyne?Mam tedy pouzivat pointery na vicerozmerne pole?
Kód: Vybrat vše
#include <stdio.h>
int main(int argc, const char **argv)
{
int pole[2][3] = {{1, 2, 3}, {4, 5, 6}};
int i;
int j;
int (*radka)[3];
int *prvek;
for ( i = 2, radka = pole; i--; radka++ )
{
for ( j = 3, prvek = *radka; j--; prvek++ )
{
printf("%i\n", *prvek);
}
}
return 0;
}