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.

  • one
    Think carefully what the expression *perv = *i; - skegg
  • Value i "shove" into the index - Tkas
  • one
    And no. The value at address i is then pushed into memory at address perv, i.e. to 0, i.e. nowhere. On some OSs this should generally crash the program. You just have to perv = i; - skegg
  • one
    Once again, take a good look at the pointers, their nature, and how to work with them, especially with the disagreement of pointers. - skegg
  • one
    The expression * perv = 0 (for example) says: "Assign the value 0 to the address contained in perv". Therefore, in order to reset the index itself, we simply write perv = 0 - skegg

1 answer 1

Continued comments.

Pointer as a variable can contain anything. Another thing is how it will dereference. If the address is nonexistent or the required operations on it are prohibited, the program will crash.

0 - special value. May carry a certain information load when passed to a function. But reading / writing on it will most likely produce an execution time error.

  • When I declared the int * perv pointer, the address was not allocated in memory for it, and when I wanted to put some value there, did this value go nowhere? I understood correctly? (without * perv = 0) - Tkas
  • one
    When the pointer is declared, no memory is allocated. This requires special procedures. The pointer itself just contains some address and nothing else. - skegg 4:39 pm
  • Yes, memory is not allocated ... So the most important thing is to remember that before inserting any value into the pointer, it must first be assigned an address. So yes? - Tkas
  • one
    Well, it must be some kind of real address where you can do something - read or write, by context. For example, the address of a variable or the address of an array. You can assign any value at all. A pointer is simply an integer variable, usually of unsigned long type. Therefore, it can contain anything. Another thing is what will happen when the call is made for this value. - skegg
  • You can assign any value at all. I tried perv = 6 and everything is still good. I did not know this, thank you =) - Tkas