• Programming a roguelike C++ & PDCurses. Function wont modify map array.
    6 replies, posted
The function addRoom() in [url]http://dl.dropbox.com/u/5342745/helpc%2B%2B/main.cpp[/url] Does not modify the map1 array declared in int main(). The obj.h file: [url]http://dl.dropbox.com/u/5342745/helpc%2B%2B/obj.h[/url] [editline]15th October 2010[/editline] Sorry that should be carveRoom()
[code]void carveRoom(char a[320][640], int ys, int xs, int height, int width) { for(int y = ys; y < height; y++) { for (int x = xs; x < width; x++) { a[y][x] = '.'; } } return; }[/code] Looking at this function I don't see how it shouldn't work. Just debug it and go through it step-by-step and see what it actually does. My guess is that you filled in the wrong parameters [editline]15th October 2010[/editline] Also, it's not a good habit to hardcode numbers like that. When you want to change something you'll have to change a lot, and this will create errors and segfaults very easily. [editline]15th October 2010[/editline] And you don't need to return; when the function is of the type void. [editline]15th October 2010[/editline] Also we don't need the entire source, just create a minimalistic testcase next time.
I think maybe, meant to use a pointer, this passes a copy of the array, alters the copy,then throws it away
[QUOTE=nekosune;25418175]I think maybe, meant to use a pointer, this passes a copy of the array, alters the copy,then throws it away[/QUOTE] No, arrays are different. When passing an array as parameter you're actually passing a pointer to the first index. See [url]http://www.fredosaurus.com/notes-cpp/arrayptr/26arraysaspointers.html[/url] and "Pointers and Arrays" at [url]http://www.cplusplus.com/doc/tutorial/pointers/[/url] An example of what OP is trying to do can be found here: [url]http://www.daniweb.com/forums/thread80339.html[/url]
Oh yeah I forgot that, arrays are like char* = char[] great now I feel foolish :-S
[QUOTE=nekosune;25418175]I think maybe, meant to use a pointer, this passes a copy of the array, alters the copy,then throws it away[/QUOTE] Not only is this wrong in the sense of forgetting how arrays work, you're also forgetting how pointers work; You're not passing a copy of the object, you're passing a copy of the pointer to the same object. Therefore, the function would have to create the copy based on the object pointed to by the pointer, and if it's thrown away, you might just as well pass a copy to the function. [editline]15th October 2010[/editline] But this isn't relevant because you probably didn't mean it.
[QUOTE=esalaka;25419922]Not only is this wrong in the sense of forgetting how arrays work, you're also forgetting how pointers work; You're not passing a copy of the object, you're passing a copy of the pointer to the same object. Therefore, the function would have to create the copy based on the object pointed to by the pointer, and if it's thrown away, you might just as well pass a copy to the function. [editline]15th October 2010[/editline] But this isn't relevant because you probably didn't mean it.[/QUOTE] I know, I was saying without a pointer, it would be passing a copy, I was forgetting arrays are just pointers.
Sorry, you need to Log In to post a reply to this thread.