#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define MAX_ROWS 10
#define MAX_COLS 10
void sin1(char *map); 
int main(void)
{  
   system("chcp 1253 >nul ");
   FILE *fileptr;
  
   char myColchar,apantisi,map[MAX_ROWS][MAX_COLS];
   int apotelesma,myColnum=1,myrow=1,k,a,l,c,row, col;
   char r,fileRow[MAX_COLS + 2]; // account for newline and NULL
   
   printf("dose tin diataksi me ton arithmo pou epithymeis(1/2): \n");
   scanf("%d",&k);//edo tha iparxei sinartisi pou tha pairnei to k os parametro kai tha exei void epistrofi...
   while (k!=1 && k!=2){
         printf("ekanes lathos dose ksana tin diataksi me ton arithmo pou epithymeis(1/2): \n");
         scanf("%d",&k);}
   if (k==1){
        fileptr = fopen("1.txt", "r");}
   else if (k==2){
        fileptr=fopen("2.txt","r");}     
   else {
        printf("de dosate sosto\n");}
   if (fileptr == NULL)
      {
           perror("File Open Error");
           return -1;
      }
   else if (fileptr!=NULL){
   printf(" A B C D E F G H I J\n");   
   printf("--------------------- \n");   
   for (row = 0; row < MAX_ROWS; row++)
      {  
         fgets(fileRow, sizeof(fileRow), fileptr);
         
         printf("|");
         for (col = 0; col < MAX_COLS; col++){
              
              map[row][col] = fileRow[col];
              if (map[row][col]=='.'){
                                   map[row][col]=' ';}
              printf("%c",map[row][col]);
              printf("|");}
              printf("%d",row+1);
         
              printf("\n");
              printf("--------------------- \n");     
      }}
   sin1((char *)map);   
   printf("\nse periptosi pou ekteleite voli patiste <<N>> allios <<O>>: \n");
   printf("Ektelesi Volis (N/O): \n");
   scanf("%s",&apantisi);
   if (apantisi=='N'){
                     srand ( time(NULL) ); 
                     l = rand() % 10;
                     c = rand() % 10;
                     c=65+c;
                     l=l+1;
                     printf("Katastasi: Ektelesi Volis\n");
                     printf("sintetagmenes ekserxomenis volis: %c%d\n",c,l);
                     printf("Apotelesma ektelestheisas volis(Patiste <<1>> gia eystoxi kai <<2>> gia astoxi)\n");
                     printf("Voli %c %d: \n",c,l);
                     scanf("%d",&apotelesma);
                     while (apotelesma!=1 && apotelesma!=2){
                           printf("Apotelesma ektelestheisas volis(Patiste <<1>> gia eystoxi kai <<2>> gia astoxi)\n");
                     printf("Voli %c %d: \n",c,l);
                     scanf("%d",&apotelesma);}
                     if (apotelesma==1){
                                        }
                     else if (apotelesma==2){
                          }
                                             
                           
                                                                 

                     

                    
                    

                     
                     }
   else if (apantisi=='O'){
                     printf("Katastasi: Lipsi Volis\n");
                     printf("Sintetagmenes eiserxomenis Volis: \n");
                     scanf("%s %d", &myColchar,&myrow);
                     myColnum = (int)myColchar;
                     if(myColnum >= 97){
                          myColnum = myColnum - 97;}
                     else if((myColnum >=65) && (myColnum <=90)){
                          myColnum = myColnum - 65;}
                       col = myColnum;
                       row = myrow - 1; // Need to compensate because array starts at 0.      
                     if (map[row][col]==' '){
                          map[row][col]='X';
                          printf("Apotelesma eiserxomenis volis: Anepityxis\n"); }               
                     else if (map[row][col]=='Ð'){
                          map[row][col]='ð';}                          
                     else if (map[row][col]=='Í'){ 
                          map[row][col]='í'; } 
                     else if (map[row][col]=='Ê'){     
                          map[row][col]='ê';}
                     else if (map[row][col]=='Ö'){  
                          map[row][col]='ö';}
                     else if (map[row][col]=='Õ'){     
                          map[row][col]='õ';}  
                     if (map[row][col]!='X'){
                          printf("Apotelesma eiserxomenis volis: Epityxis\n");}                     
                                                                         
                    
                          }
   
   
   fclose(fileptr);
   
   system("pause");
// now you have the map loaded,

      // so do the rest of your program
   return 0;
}

void sin1(char * map)
{    
     int row,col;
     for (row = 0; row < MAX_ROWS; row++)
      {  
          printf("|");
          for (col = 0; col < MAX_COLS; col++){
              printf("%c",*(map[row][col]));
              printf("|");}
          printf("%d",row+1);
          printf("\n");
              printf("--------------------- \n");     
      }
}

here is my code..the problem is tha i get the error' subscripted value is neither array nor pointer'....all i want to do in this game of battleship is the sin1 function to be able to return whenever it is called the map array which shows ships.....can u help me?what is wrong?or can u change my program to be able to do the same thing?pliz reply soon....Dont worry about the printf which prints other language characters it is beacuse i am greek...

Recommended Answers

All 4 Replies

To pass an array, you must specify your parameter as an array.

void aFunction(int anArray[][dim2Size]);  //prototype

void aFunction(int anArray[][dim2Size]) {  //function header
  for (int dim1 = 0; dim1 < dim1Size; ++dim1) {

    for (int dim2 = 0; dim2 < dim2Size; ++dim2) {
      printf("Value in anArray[%d][%d] = %d \n", dim1, dim2, anArray[dim1][dim2]);
    }  //end inner for

  }  //end outer for

}  //end aFunction()

are u sure?i mean what is dim2size and why isn't there dim1size??also my array includes only characters inside...can u transform my code so that the function will return the array?

are u sure?i mean what is dim2size and why isn't there dim1size??also my array includes only characters inside...can u transform my code so that the function will return the array?

well have you tried it??

look:

void sin1(char map[][MAX_COLS])

make your prototype and your definition like this. you only have to declare the size of the second index. in the main function, pass in the map directly to your function without recasting it: sin1(map); because it already is a pointer (to an array).

and no, you dont want to try and return the array. pass it by value as an argument as shown.

wow it worked it needed the sin1(map); as u said thnx a lot!!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.