Menu

Character Pointer Arrays (C++)

2009-01-15
2012-09-26
  • anonymous nobody

    How can I make an array of character array pointers? I'm trying to achieve an array that holds 5 pointers to other arrays, with both arrays in structures. Here's what I have so far:

    Piece1.LineArray[0]=&Piece1.Line1;
    Piece1.LineArray[1]=&Piece1.Line2;
    Piece1.LineArray[2]=&Piece1.Line3;
    Piece1.LineArray[3]=&Piece1.Line4;
    Piece1.LineArray[4]=&Piece1.Line5;
    

    Piece1 is a member of the structure

    struct piece
    {
    char Line1[10],Line2[10],Line3[10],Line4[10],Line5[10],*LineArray[5];
    int pos;
    };

    And this is the error I get:
    cannot convert char (*)[10]' tochar*' in assignment
    I'm completely lost as to how I could achieve this.

    -J.M

     
    • anonymous nobody

      I'm learning off the Dev-Cpp help file... and pretty much making it up as I go along.

      Thanks for the info on multi-dimensional arrays, I was wondering if you could do more than 2 dimensions.

      -J.M

       
    • anonymous nobody

      Oh dear, I forgot the compile log :)
      The last error is on this line:
      printf("%s%s%s",PieceArray[i]->LineArray[ii],PieceArray[i+1]->LineArray[ii],PieceArray[i+2]->LineArray[ii]);

      Compiler: Default compiler
      Building Makefile: "C:\mycstuff\console\FirstAttempt\Makefile.win"
      Executing make...
      make.exe -f "C:\mycstuff\console\FirstAttempt\Makefile.win" all
      g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"

      main.cpp: In function `int main(int, char**)':

      main.cpp:94: error: cannot convert char (*)[10]' tochar' in assignment
      main.cpp:95: error: cannot convert char (*)[10]' tochar
      ' in assignment
      main.cpp:96: error: cannot convert char (*)[10]' tochar' in assignment
      main.cpp:97: error: cannot convert char (*)[10]' tochar
      ' in assignment

      main.cpp:98: error: cannot convert char (*)[10]' tochar' in assignment
      main.cpp:100: error: cannot convert char (*)[10]' tochar
      ' in assignment
      main.cpp:101: error: cannot convert char (*)[10]' tochar' in assignment
      main.cpp:102: error: cannot convert char (*)[10]' tochar
      ' in assignment
      main.cpp:103: error: cannot convert char (*)[10]' tochar*' in assignment

      main.cpp:104: error: cannot convert char (*)[10]' tochar' in assignment
      main.cpp:106: error: cannot convert char (*)[10]' tochar
      ' in assignment
      main.cpp:107: error: cannot convert char (*)[10]' tochar' in assignment
      main.cpp:108: error: cannot convert char (*)[10]' tochar
      ' in assignment
      main.cpp:109: error: cannot convert char (*)[10]' tochar' in assignment
      main.cpp:110: error: cannot convert char (*)[10]' tochar
      ' in assignment
      main.cpp:112: error: cannot convert char (*)[10]' tochar' in assignment
      main.cpp:113: error: cannot convert char (*)[10]' tochar
      ' in assignment
      main.cpp:114: error: cannot convert char (*)[10]' tochar' in assignment
      main.cpp:115: error: cannot convert char (*)[10]' tochar
      ' in assignment
      main.cpp:116: error: cannot convert char (*)[10]' tochar' in assignment
      main.cpp:118: error: cannot convert char (*)[10]' tochar
      ' in assignment
      main.cpp:119: error: cannot convert char (*)[10]' tochar' in assignment
      main.cpp:120: error: cannot convert char (*)[10]' tochar
      ' in assignment
      main.cpp:121: error: cannot convert char (*)[10]' tochar' in assignment
      main.cpp:122: error: cannot convert char (*)[10]' tochar
      ' in assignment

      main.cpp:124: error: cannot convert char (*)[10]' tochar' in assignment
      main.cpp:125: error: cannot convert char (*)[10]' tochar
      ' in assignment
      main.cpp:126: error: cannot convert char (*)[10]' tochar' in assignment
      main.cpp:127: error: cannot convert char (*)[10]' tochar
      ' in assignment
      main.cpp:128: error: cannot convert char (*)[10]' tochar' in assignment
      main.cpp:130: error: cannot convert char (*)[10]' tochar
      ' in assignment
      main.cpp:131: error: cannot convert char (*)[10]' tochar' in assignment
      main.cpp:132: error: cannot convert char (*)[10]' tochar
      ' in assignment
      main.cpp:133: error: cannot convert char (*)[10]' tochar' in assignment
      main.cpp:134: error: cannot convert char (*)[10]' tochar
      ' in assignment
      main.cpp:136: error: cannot convert char (*)[10]' tochar' in assignment
      main.cpp:137: error: cannot convert char (*)[10]' tochar
      ' in assignment
      main.cpp:138: error: cannot convert char (*)[10]' tochar' in assignment
      main.cpp:139: error: cannot convert char (*)[10]' tochar
      ' in assignment
      main.cpp:140: error: cannot convert char (*)[10]' tochar' in assignment
      main.cpp:142: error: cannot convert char (*)[10]' tochar
      ' in assignment

      main.cpp:143: error: cannot convert char (*)[10]' tochar' in assignment
      main.cpp:144: error: cannot convert char (*)[10]' tochar
      ' in assignment
      main.cpp:145: error: cannot convert char (*)[10]' tochar' in assignment
      main.cpp:146: error: cannot convert char (*)[10]' tochar
      ' in assignment
      main.cpp:267: error: `LineArray' undeclared (first use this function)
      main.cpp:267: error: (Each undeclared identifier is reported only once for each function it appears in.)

      make.exe: *** [main.o] Error 1

      Execution terminated

       
    • Richard Kopcke

      Richard Kopcke - 2009-01-15

      char ** LineArray = new char * [5];

      for (int i=0; i<5; i++) LineArray[i] = new char [10];

      this also allows you to simplify
      piece1.LineArray [i] [j]

       
    • cpns

      cpns - 2009-01-15

      Omit the &, the address of a char array hast type char**

      Just:

      Piece1.LineArray[0] = Piece1.Line1;

      Although I cannot think why you would not simply have:

      struct piece
      {
      char LineArray[5][10] ;
      int pos ;
      } ;

      Clifford

       
    • anonymous nobody

      I'm sorry, I think I missed something; what exactly does char LineArray[5][10] ; declare? I've never seen an array declared that way.

      -J.M

       
    • anonymous nobody

      Did a bit of reading - LineArray[5][10] declares a 5x10 "grid" so to speak. I now have a new problem:
      Compiler: Default compiler
      Building Makefile: "C:\mycstuff\console\FirstAttempt\Makefile.win"
      Executing make...
      make.exe -f "C:\mycstuff\console\FirstAttempt\Makefile.win" all
      g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include" -Wall -Werror -Wformat

      main.cpp: In function `int main(int, char**)':

      main.cpp:212: error: LineArray' has not been declared main.cpp:212: error: request for member of non-aggregate type before '[' token main.cpp:212: error:LineArray' has not been declared

      main.cpp:212: error: request for member of non-aggregate type before '[' token

      main.cpp:212: error: `LineArray' has not been declared
      main.cpp:212: error: request for member of non-aggregate type before '[' token

      make.exe: *** [main.o] Error 1

      Execution terminated

      new structure:
      struct piece
      {
      / draw array /
      char LineArray[5][10];
      / pos is the position of the piece /
      int pos;
      };
      line 212:
      printf("%s%s%s",PieceArray[i].LineArray[ii],PieceArray[i+1].LineArray[ii],PieceArray[i+2].LineArray[ii]);
      PieceArray declaration:
      struct piece Piece1,Piece2,Piece3,Piece4,Piece5,Piece6,Piece7,Piece8,Piece9,
      PieceArray[9];
      PieceArray[0]=&Piece1;
      PieceArray[1]=&Piece2;
      PieceArray[2]=&Piece3;
      PieceArray[3]=&Piece4;
      PieceArray[4]=&Piece5;
      PieceArray[5]=&Piece6;
      PieceArray[6]=&Piece7;
      PieceArray[7]=&Piece8;
      PieceArray[8]=&Piece9;
      I'm thoroughly confused.

       
    • anonymous nobody

      Ah.. I have fixed the problem.
      I changed
      printf("%s%s%s",PieceArray[i].LineArray[ii],PieceArray[i+1].LineArray[ii],*PieceArray[i+2].LineArray[ii]);
      to
      printf("%s%s%s",PieceArray[i]->LineArray[ii],PieceArray[i+1]->LineArray[ii],PieceArray[i+2]->LineArray[ii]);

       
    • cpns

      cpns - 2009-01-16

      > I'm sorry, I think I missed something; what exactly does char LineArray[5][10] ; declare?
      I've never seen an array declared that way.

      It is no magic and in fact quite common. It is a two dimensional array which it seems is what you were attempting to synthesize. You can have arrays with any number of dimensions (although more than three requires some spacial kind of brain to follow!)

      An array of arrays if you like, LineArray is an array of five char[10] arrays for example.

      http://en.wikipedia.org/wiki/Array#Multi-dimensional_arrays
      http://www.exforsys.com/tutorials/c-language/c-arrays.html

      > PieceArray[0]=&Piece1;

      Looks like you are doing the same odd thing there! Why is PieceArray an array of piece pointers and not simply an array of piece objects?

      If it does for some reason need ot be an array of pointers, then:

      *PieceArray[i].LineArray[ii]

      is more usually written:

      PieceArray[i]->LineArray[ii]

      It is unusual to dereference a struct pointer to access members rather than using the -> operator. Where are you learning this stuff!?

      Clifford

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.