2

in a larger code i have this code:

#define N 10
..
..
..
char Map [N][N] =              {"##########",
                                "#@       #",
                                "#        #",
                                "#        #",
                                "#        #",
                                "#        #",
                                "#        #",
                                "#        #",
                                "#        #",
                                "##########"};

when i try to compile and run it form Code bloke i gives me the error

F:\C++\Maze\main.cpp|25|warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]|

any idea what i did wrong in this simple code?

1
  • a warning is not an error (yet). Commented Aug 15, 2014 at 7:46

4 Answers 4

6

The length of each row in your example is 10+1=11 (remember the null terminator for string needs space to allocate). So:

#define N 10
#define M 11
char Map [N][M] =              {"##########",
                                "#@       #",
                                "#        #",
                                "#        #",
                                "#        #",
                                "#        #",
                                "#        #",
                                "#        #",
                                "#        #",
                                "##########"};
Sign up to request clarification or add additional context in comments.

Comments

4

You're forgetting that a string literal has an implicit null-terminator so requires an extra byte of storage.

If you had written char Map [N][N + 1] then it will work fine.

Even better, let the compiler count the rows: write char Map [][N + 1]. You might want to change the definition of N. N + 1 will be evaluated at compile time which is why you can use it as an array dimension.

Comments

0

Why not using a vector of strings? You can still reference the elements inside by using operator[]:

#include <string>
#include <iostream>
#include <vector>

using std::cout;
using std::endl;

int main(){

std::vector<std::string > s =  {{"##########"},
  {"#@       #"},
  {"#        #"},
  {"#        #"},
  {"#        #"},
  {"#        #"},
  {"#        #"},
  {"#        #"},
  {"#        #"},
  {"##########"}};

  for (auto i : s)
      cout<<i<<endl;

  cout<<"\nGet @ symbol:"<<s[1][1]<<end
  return 0;
}

Outputs:

##########
#@       #
#        #
#        #
#        #
#        #
#        #
#        #
#        #
##########

Get @ symbol:@

You can find the code here

Comments

-1

You need to create Map as follows:

char *Map [N] =            {"##########",
                            "#@       #",
                            "#        #",
                            "#        #",
                            "#        #",
                            "#        #",
                            "#        #",
                            "#        #",
                            "#        #",
                            "##########"};

For example:

#include <stdio.h>

#define N 10

int
main (void) {

    int i;
    char *Map [N] =            {"##########",
                                "#@       #",
                                "#        #",
                                "#        #",
                                "#        #",
                                "#        #",
                                "#        #",
                                "#        #",
                                "#        #",
                                "##########"};

    for (i = 0; i < 10; i++)
        printf (" Map[%d]  %s\n", i, Map[i]);

    return 0;
}

output:

$ ./bin/tcs
 Map[0]  ##########
 Map[1]  #@       #
 Map[2]  #        #
 Map[3]  #        #
 Map[4]  #        #
 Map[5]  #        #
 Map[6]  #        #
 Map[7]  #        #
 Map[8]  #        #
 Map[9]  ##########

5 Comments

That's different: in this case the data block will be read-only memory and attempts to modify it is undefined behaviour. The way the OP has done it is fine.
Hmm, is there any reason you can't just make it constant with const char *Map [N] = ...?
Well I apologize, the subtleties of the question were missed, the first simple solution was to declare an array of pointers and fill them with the literals supplied allowing null terminator to be added without restriction. Why is that different from explicitly declaring the column size one bigger? (I"m curious as to how that would change the use/implementation?)
OK - I understand. *(Map []) + N + 1 does not equal *Map[] Sorry for the misread of the question.
"is there any reason you can't just make it constant with const char *Map [N] = ...? " -- The reason is that the OP presumably wants to alter Map, which is just being initialized with its starting value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.