It is necessary to swap the first and last positive elements (pointers).
C language. No malloc () sizeof () and other functions (the course has just begun). The task needs to be implemented as much as possible using pointers. I work in Turbo C ++ 3.1, since other compilers give an error when assigning the value to 0 to pointers. Here is my code:
#include <stdio.h> #include <conio.h> /*Π Π·Π°Π΄Π°Π½Π½ΠΎΠΌ ΠΎΠ΄Π½ΠΎΠΌΠ΅ΡΠ½ΠΎΠΌ ΠΌΠ°ΡΡΠΈΠ²Π΅ ΠΏΠΎΠΌΠ΅Π½ΡΡΡ ΠΌΠ΅ΡΡΠ°ΠΌΠΈ ΠΏΠ΅ΡΠ²ΡΠΉ ΠΈ ΠΏΠΎΡΠ»Π΅Π΄Π½ΠΈΠΉ ΠΏΠΎΠ»ΠΎΠΆΠΈΡΠ΅Π»ΡΠ½ΡΠ΅ ΡΠ»Π΅ΠΌΠ΅Π½ΡΡ */ void main() { int a[3]; int *i, *perv, *posl, t; *perv = 0; *posl = 0; for (i = a; i < a + 3; i++) { scanf("%d", i); if (*i > 0) { *posl = *i; // Π½Π°Ρ
ΠΎΠ΄ΠΈΠΌ ΠΏΠΎΡΠ»Π΅Π΄Π½ΠΈΠΉ ΠΏΠΎΠ»ΠΎΠΆΠΈΡΠ΅Π»ΡΠ½ΡΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ t = *posl; } } for (i = a; i < a + 3; i++) { if (*i > 0) { *perv = *i; // Π½Π°Ρ
ΠΎΠ΄ΠΈΠΌ ΠΏΠ΅ΡΠ²ΡΠΉ ΠΏΠΎΠ»ΠΎΠΆΠΈΡΠ΅Π»ΡΠ½ΡΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ *posl = *perv; *perv = t; break; } } printf("%dn", *perv); printf("%dn", *posl); printf("n"); for (i = a; i < a + 3; i++) printf("%d", *i); getch(); }
At the input: 1 2 3 at the output will be: 3 1 and 1 2 3. That is, *perv
and *posl
places have changed, but when it comes to printing the array, nothing changes. Help me please.
*perv = *i;
- skegg