• Advertisement
Sign in to follow this  

Stuck on passing an array to a struct (Solved)

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 10
3 3 3 3 3 3 3 3 3 3
3 0 0 0 0 0 0 0 0 3
3 0 0 0 0 0 0 0 0 3
3 0 0 0 0 0 2 2 2 3
3 0 0 0 0 0 0 0 0 3
3 0 0 0 0 0 0 0 0 3
3 0 0 0 1 1 1 0 0 3
3 0 2 0 0 0 0 0 0 3
3 0 2 0 0 0 0 0 0 3
3 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 this post


Link to post
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_H

struct 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 this post


Link to post
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.

If I've misunderstood your last post, please give a bit more information about the nature of the problem.

HTH Paul

Share this post


Link to post
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_H

struct 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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by xeddiex
If you're using C, you would use strcpy and freinds.


You would never copy an array of this type with strcpy cause you would end up copying part of the array - up to the 0, the correct way to copy an char array would be to use memcpy. Friends are a C++ keyword.

Share this post


Link to post
Share on other sites
Uh, I think he meant "and friends" in the colloquial sense of "and the other functions with similar names that do similar things." :P

Anyway, in C++, even when you think you need memcpy() or memmove(), you can still do much better: use std::copy() from <algorithm>.

Share this post


Link to post
Share on other sites
I apologise... I typed that work so I guess the friend keyword was going through my mind.

There was nothing to indicate that the OP was using C++ and I was responding to the C strcpy comment. I do agree that if coding in C++ you would use std::copy

Edit: Oops... :( He would have been using C++ (the // gives it away) It's the little things you miss :P

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement