# Stuck on passing an array to a struct (Solved)

This topic is 4441 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi guys i can not seem to pass this g_maze1 array to my array in the struct, any sugestions? Maze.cpp
//=========== Includes

#include "maze.h"

char g_maze1[]={3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
3,2,1,1,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,2,3,
3,1,3,3,1,3,3,3,3,1,3,1,3,3,3,3,1,3,3,1,3,
3,1,1,1,1,1,3,3,3,1,3,1,3,3,3,1,1,1,1,1,3,
3,1,3,3,3,1,1,1,1,1,1,1,1,1,1,1,3,3,3,1,3,
3,2,1,1,1,1,3,1,3,3,3,3,3,1,3,1,1,1,1,2,3,
3,3,3,3,3,1,3,1,1,1,3,1,1,1,3,1,3,3,3,3,3,
0,0,0,0,3,1,3,1,3,1,3,1,3,1,3,1,3,0,0,0,0,
0,3,3,3,3,1,3,1,1,1,1,1,1,1,3,1,3,3,3,3,0,
0,0,0,0,0,1,3,1,3,3,0,3,3,1,3,1,0,0,0,0,0,
0,3,3,3,3,1,1,1,3,0,0,0,3,1,1,1,3,3,3,3,0,
0,0,0,0,3,1,3,1,3,3,3,3,3,1,3,1,3,0,0,0,0,
3,3,3,3,3,1,3,1,1,1,1,1,1,1,3,1,3,3,3,3,3,
3,2,1,1,1,1,3,1,3,3,3,3,3,1,3,1,1,1,1,2,3,
3,1,3,3,3,1,1,1,1,1,3,1,1,1,1,1,3,3,3,1,3,
3,1,1,1,1,1,3,3,3,1,3,1,3,3,3,1,1,1,1,1,3,
3,3,1,3,3,1,3,1,1,1,1,1,1,1,3,1,3,3,1,3,3,
3,1,1,1,3,1,1,1,3,3,3,3,3,1,1,1,3,1,1,1,3,
3,1,3,1,3,1,3,1,1,1,3,1,1,1,3,1,3,1,3,1,3,
3,2,1,1,1,1,1,1,3,1,0,1,3,1,1,1,1,1,1,2,3,
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//******************************************* Function Declarations *****************************************************
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//******************************************* Globals *******************************************************************
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sMaze maze;

void maze_Create()
{
maze.g_sectors = g_maze1;
}


Maze.h
#ifndef MAZE_H
#define MAZE_H

struct sMaze
{
char g_sectors[];
};

void maze_Create();

#endif


The error i get is: - error C2440: '=' : cannot convert from 'char [441]' to 'char []' There is no context in which this conversion is possible [Edited by - Prog101 on November 10, 2006 4:01:57 AM]

##### Share on other sites
#ifndef MAZE_H#define MAZE_Hstruct sMaze{        char* g_sectors;};void maze_Create();#endif

##### Share on other sites
First you have to give g_sectors a size when you dclare it in your structure. Second, you cannot copy an array to an array via simple assignment. If you're using C, you would use strcpy and freinds. But, you must make sure g_sectors has enough space allocated for it to be able to hold what g_mazel is trying to copy over. If this is C++, there are better alternatives that others can tell you. I have to leave to work so that's all I can say for now...

- xeddiex

##### Share on other sites
If you were to use a std::vector<char> instead of a raw array, you could assign one to the other with = like that.

You wouldn't be able to statically assign the values like you have, but then it would be better to load these from a file than hardcode into your game anyway.

#include <vector>#include <fstream>bool LoadMap(const std::string &Path,std::vector<char> &v){    std::ifstream is(Path); if(!is.is_open()) return false;    int W,H; is >> W >> H; v.resize(W*H);    int temp; char c;    for(int i=0;i<W*H;++i){ is >> temp; v=char(temp); }    return true;}class maze{private:    std::vector<char> v;public:    maze() { }    bool Load(const std::string &Path){ return LoadMap(Path,v); }};maze m;int main(){    if(!m.Load("map.txt")) return -1;    return 0;}

map.txt
10 103 3 3 3 3 3 3 3 3 33 0 0 0 0 0 0 0 0 33 0 0 0 0 0 0 0 0 33 0 0 0 0 0 2 2 2 33 0 0 0 0 0 0 0 0 33 0 0 0 0 0 0 0 0 33 0 0 0 1 1 1 0 0 33 0 2 0 0 0 0 0 0 33 0 2 0 0 0 0 0 0 33 3 3 3 3 3 3 3 3 3

This would have the advantage that you could edit the map (with Notepad or whatever) without having to recompile your program, which is even more handy when you have different levels and so on.

##### Share on other sites
I have it running now but i have one problem, when i want to reset the maze again it will not reset to its original g_maze1[]?

Maze.h
#ifndef MAZE_H#define MAZE_Hstruct sMaze{	char g_tempSectors[441];	char *g_Sectors;};void maze_Create();void maze_ResetMap();#endif

[Edited by - Prog101 on September 18, 2006 6:28:41 AM]

##### Share on other sites
I'm going to go out on a limb here and guess that you are storing the original maze in char g_tempSectors[441], the assigning the pointer char *g_Sectors to point to char g_tempSectors[441], then modifying the maze via g_Sectors during the game. Is this correct?

If so, this will modify the data in g_tempSectors as well, since g_Sectors is only pointing to this data. If you want to store the original maze so you can reset it you will need to replace g_Sectors with g_Sectors[441] and when you create or reset the maze, copy the info from g_tempSectors into g_Sectors.

If you were using std::vectors for this it would be trivial. With raw arrays, your copy could look a bit like:

std::memcpy(g_Sectors,g_TempSectors,sizeof(char)*441);

I suppose although it is more complexity for no gain over a vector.

HTH Paul

##### Share on other sites
Oh my god.

1) You're using C++. Use a constructor, not a free "Create" function.
2) Use the standard library.
3) Why "g_" on a struct member variable? Such a prefix normally indicates globals. Personally I prefer not to use any such tags, but the normal one for struct data members is m_.
4) Why tag your Maze struct with "s"? What else can it be, in context? There's no reason you'd ever have to *know, in calling code*, whether you wrote it as a "struct" or a "class", BTW.

Maze.h:
#ifndef MAZE_H#define MAZE_Hstruct Maze{	vector<char> sectors;	// This constructor actually offers you considerably more flexibility	// than you need, but you should at least create something that doesn't	// require you to initialize from a *specific* array. Also, this version	// is surprisingly easy to write, taking advantage of the fact that the	// standard library already does the work for you.	// Of course, as it stands right now, you could just use the vector	// directly, but you *will* be adding more functionality to the Maze	// later, yes? Or maybe you could just use a typedef?	template <typename InputIterator>	Maze(InputIterator begin, InputIterator end) : sectors(begin, end) {}};#endif

Maze.cpp:
#include "maze.h"char g_maze_data[] = { 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2,1,1,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,2,3,3,1,3,3,1,3,3,3,3,1,3,1,3,3,3,3,1,3,3,1,3,3,1,1,1,1,1,3,3,3,1,3,1,3,3,3,1,1,1,1,1,3,3,1,3,3,3,1,1,1,1,1,1,1,1,1,1,1,3,3,3,1,3,3,2,1,1,1,1,3,1,3,3,3,3,3,1,3,1,1,1,1,2,3,3,3,3,3,3,1,3,1,1,1,3,1,1,1,3,1,3,3,3,3,3,0,0,0,0,3,1,3,1,3,1,3,1,3,1,3,1,3,0,0,0,0,0,3,3,3,3,1,3,1,1,1,1,1,1,1,3,1,3,3,3,3,0,0,0,0,0,0,1,3,1,3,3,0,3,3,1,3,1,0,0,0,0,0,0,3,3,3,3,1,1,1,3,0,0,0,3,1,1,1,3,3,3,3,0,0,0,0,0,3,1,3,1,3,3,3,3,3,1,3,1,3,0,0,0,0,3,3,3,3,3,1,3,1,1,1,1,1,1,1,3,1,3,3,3,3,3,3,2,1,1,1,1,3,1,3,3,3,3,3,1,3,1,1,1,1,2,3,3,1,3,3,3,1,1,1,1,1,3,1,1,1,1,1,3,3,3,1,3,3,1,1,1,1,1,3,3,3,1,3,1,3,3,3,1,1,1,1,1,3,3,3,1,3,3,1,3,1,1,1,1,1,1,1,3,1,3,3,1,3,3,3,1,1,1,3,1,1,1,3,3,3,3,3,1,1,1,3,1,1,1,3,3,1,3,1,3,1,3,1,1,1,3,1,1,1,3,1,3,1,3,1,3,3,2,1,1,1,1,1,1,3,1,0,1,3,1,1,1,1,1,1,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,};// This part *cannot* be done with an inline function, unfortunately, because// of pointer decay. See the below link for more.#define ARRAY_RANGE(arr) arr, arr + sizeof(arr) / sizeof(arr[0])Maze g_maze(ARRAY_RANGE(g_maze_data));

5) Read the first few articles.

##### Share on other sites
Listen to EasilyConfused and Zahlman.

Quote:
 Original post by Zahlman// This part *cannot* be done with an inline function, unfortunately, because// of pointer decay. See the below link for more.#define ARRAY_RANGE(arr) arr, arr + sizeof(arr) / sizeof(arr[0])Maze g_maze(ARRAY_RANGE(g_maze_data));

I'm rather fond of MaulingMonkey's* begin and end templates for this:
template < typename Type, std::size_t size >Type const * begin(Type const (&array)[size]){	return array;}template < typename Type, std::size_t size >Type const * end(Type const (&array)[size]){	return array + size;}Maze g_maze(begin(g_maze_data), end(g_maze_data));

Possibly a bit advanced for the OP, but I just thought I'd mention it.

Σnigma

*At least he was the first to bring them to my attention

##### Share on other sites
Er, that is correct; macro not necessary. ^^;;

1. 1
Rutin
34
2. 2
3. 3
4. 4
5. 5

• 12
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
633334
• Total Posts
3011410
• ### Who's Online (See full list)

There are no registered users currently online

×