Greetings, I am writing a naval battle for the academy, here’s a working piece of my draft project, the problem is that only the first line of the two-dimensional deck5 array is copied to the map.

#include <stdlib.h> #include <time.h> /* #include <conio.h> */ #define s 13 #define c 28 char deck5[3][7] = { { '0', '0', '0', '0', '0', '0', '0', '0' }, { '0', '*', '*', '*', '*', '*', '*', '0' }, { '0', '0', '0', '0', '0', '0', '0', '0' } }; char map[s][c]= { " 0123456789 0123456789 ", " ---------- ---------- ", "0| | 0| |", "1| | 1| |", "2| | 2| |", "3| | 3| |", "4| | 4| |", "5| | 5| |", "6| | 6| |", "7| | 7| |", "8| | 8| |", "9| | 9| |", " ---------- ---------- ", }; void show(char (*pmap)[c]) { int i,j; for(i=0; i<s; ++i) { for(j=0; j<c; ++j) { printf("%c",pmap[i][j]); } printf("\n"); } } h_ship5(char (*pmap)[c]) { int i=0,j=0,x=0,y=0,ii=0,jj=0,yes=0; do//зацикливает проверку правильности расположения корабля { x=rand()%7+2; y=rand()%7+2; yes=0; for(; j<(y+5); j++) { if(pmap[x][j]==' ') { yes++; } } } while(yes<5); x--,y--; i=x,j=y; for(;i<(x+3);i++){ if(jj==7){ ii++; jj=0;} for(;j<(y+7);j++){ jj++; if(pmap[i][j]==' ') pmap[i][j]=deck5[ii][jj]; } } return pmap; } int main() { srand( (unsigned)time( NULL ) ); char*pmap=map; pmap=h_ship5(pmap); show(pmap); return 0; } 

Here is a simple copy of the array into the array, but for some reason only the first line is copied.

 #define s 13 #define c 28 #define a 3 #define b 8 char deck5[a][b] = { "0000000", "0*****0", "0000000",}; char map[a][b] = { "1111111", "1*****1", "1111111",}; int main(int argc, char** argv) { int i = 0, j = 0; for (; i < 3; i++) { for (; j < 7; j++) { map[i][j] = deck5[i][j]; //if(j==7)deck5[i][j]='\0'; или map[i][j]='\0'; или оба варианта вместе не помогают } } for (i = 0; i < a; ++i) { for (j = 0; j < b; ++j) { printf("%c", map[i][j]); } printf("\n"); } return (EXIT_SUCCESS); } 

Compiler Warnings:

  \Desktop\test\test_fight\main.c||In function 'show':| \Desktop\test\test_fight\main.c|41|warning: implicit declaration of function 'printf'| \Desktop\test\test_fight\main.c|41|warning: incompatible implicit declaration of built-in function 'printf'| \Desktop\test\test_fight\main.c|43|warning: incompatible implicit declaration of built-in function 'printf'| \Desktop\test\test_fight\main.c|49|warning: return type defaults to 'int'| \Desktop\test\test_fight\main.c||In function 'h_ship5':| \Desktop\test\test_fight\main.c|82|warning: return makes integer from pointer without a cast| \Desktop\test\test_fight\main.c||In function 'main':| \Desktop\test\test_fight\main.c|91|warning: initialization from incompatible pointer type| \Desktop\test\test_fight\main.c|93|warning: passing argument 1 of 'h_ship5' from incompatible pointer type| \Desktop\test\test_fight\main.c|48|note: expected 'char (*)[28]' but argument is of type 'char *'| \Desktop\test\test_fight\main.c|93|warning: assignment makes pointer from integer without a cast| \Desktop\test\test_fight\main.c|94|warning: passing argument 1 of 'show' from incompatible pointer type| \Desktop\test\test_fight\main.c|33|note: expected 'char (*)[28]' but argument is of type 'char *'| ||=== Build finished: 0 errors, 9 warnings ===| 
  • And what is this incorrectness? - VorobyevEvgeniy
  • I got the impression that going beyond the deck array is happening. Try to check the indexes, make a test output, for example. - insolor
  • @abrakadabra Did you try to translate your code? My compiler issued 7 warnings and 2 comments. - alexlz
  • VorobyevEvgeniy- only the first line is printed, or numbers from 1 to 7. insolor - everything is fine. alexlz - thanks, need to look. - Mr Fish-cat
  • implicit declaration of function 'printf' - no printf prototype (it is in stdio.h) warning: return type defaults to 'int' In function 'h_ship5' - no description of type h_ship5. Better add. Well, etc. It is better to fix, there will be less problems. - alexlz pm

1 answer 1

 char deck5[3][7] = { "0000000", "0*****0", "0000000", }; 

We look at this code. we have an array of 3 by 7, but you fill it not with characters, but with a string that has 7 characters in it, and like everything from a line, the '\ 0' null character ends. That is, you have a '\ 0' character outside the array, and since the arrays are arranged one behind the other, then the null character goes to the beginning of the new array and it turns out not what you want. Try this.

 char deck5[3][7] = { { '0', '0', '0', '0', '0', '0', '0' }, { '0', '*', '*', '*', '*', '*', '0' }, { '0', '0', '0', '0', '0', '0', '0' } }; 

This is the first thing I saw.

  • Thanks, almost works, displays only the first row of characters, can you know how to solve it? I tried to add a newline in the loop, but this does not work. - Mr Fish-cat
  • I added compiler's varning to the question - mr fish-cat
  • I had a mistake, I myself filled in an array of 8 characters, so correct as now. And I also did not understand how your h_ship5 (char (* pmap) [c]) function returns a value, but the definition does not indicate what it should return and I just do not compile your code. Maybe it will be easier for you to make this array globally and you don’t need to pass a pointer, but don’t need to return anything to it, but work directly with it. - Roman Goriachevskiy
  • it doesn’t work anyway, for the experiment I created two two-dimensional arrays of the same dimension and, by the same principle, tried to copy one into another, but only the first line is copied. I can not understand what's the matter ... I added the code to the question - copying an array. - mr fish-cat
  • @Roman Goriachevskiy I found the answer. In the cycle condition of copying an array, you need to assign i and j values. In my non-working code, i and j are assigned x and y, BEFORE entering the loop, it turns out that this should be done exactly in the loop condition. Hmm, I lost a week because of this, I could have already written a game. for (i = x, ii = 0; i <(x + 3); i ++, ii ++) {for (j = y, jj = 0; j <(y + 7); j ++, jj ++) {if (pmap [ i] [j] == '') pmap [i] [j] = deck5 [ii] [jj]; }} - mr fish-cat