• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.

Archived

This topic is now archived and is closed to further replies.

Demonlir

Why Doesn't this Work?

8 posts in this topic

#include <iostream.h> #include <string.h> #include <stdlib.h> void strcopy(char *str1, char *str2); main() { char str[] = "Use malloc() to allocate memory."; char *ptr_str; int result; ptr_str = malloc(strlen(str) + 1); if (ptr_str != NULL) { strcopy(str, ptr_str); cout << "The String pointed to by prt_str is: "; ptr_str; cout << "\n"; result = 0; } else{ cout << "Malloc() function failed.\n"; result = 1; } return result; } void strcopy(char *str1, char *str2) { int i; for (i=0; str1; i++) str2[i] = str1[i]; str2[i] = ''\0''; }
0

Share this post


Link to post
Share on other sites
first thing that comes to mind is that you have a ; before ptr_str in your cout.
that would be the same as if you wrote:
cout << "The String pointed to by prt_str is: ";
ptr_str;

I'd say get rid of that ; after the "
second...I think you want "for (i = 0;i==strlen(str1);i++)"
or something like that...either that or i
I always get a little confused with the 0 based stuff...

-Lohrno

edit: ack I didnt need all the quote I had heh


Edited by - Lohrno on February 11, 2002 10:07:52 PM

Edited by - Lohrno on February 11, 2002 10:08:18 PM
0

Share this post


Link to post
Share on other sites
quote:
Original post by Lohrno
I''d say get rid of that ; after the "


And replace it with <<, of course:

cout << "The String pointed to by prt_str is: " << ptr_str;

And what''s wrong with the strcpy() function??


0

Share this post


Link to post
Share on other sites
quote:
Original post by Lohrno
second...I think you want "for (i = 0;i==strlen(str1);i++)"
or something like that...either that or i


I suggest you avoid using a function evaluation in a loop condition like
  i<strlen(str1)   
. Just assign the length of the string before the loop and use the value in the condition.
  
str_length = strlen(str1);
for(i=0;i<str_length;i++)
{

}

Faster code



Edited by - clabinsky on February 11, 2002 10:32:26 PM

Edited by - clabinsky on February 11, 2002 10:34:44 PM
0

Share this post


Link to post
Share on other sites
Another way:

void strcopy(char *str1, char *str2)
{
for (int i=0; str1[ i ] ; i++) str2 = str1[i];
str2[i] = '\0';
}


Edited by - stefu on February 11, 2002 12:38:47 AM
0

Share this post


Link to post
Share on other sites
or

  
void strcopy(char *str1, char *str2)
{
for (;*str1;)
*(str2++) = *(str1++);
*str2 = ''\0'';
}


or more clean
  
void strcopy(char *str1, char *str2)
{
for (;*str1; str1++, str2++)
*(str2) = *(str1);
*str2 = ''\0'';
}


warning the above code is considered advanced use of pointers and is BAD. dont use it, since it plays on tricks with postfix prefix incrments or in the second one with the for() fields. this last one is the worst. and should NEVER EVER be used no matter how advanced you are (though in fun code, its ok, not code that is suppsoed to do stuff).
btw i changed the varible names, cause the ones you used suck. use descriptive names. also dest comes first in most functions so its best to get in the habit of doing things like that.
  
void strcopy(char *dest, char *src)
{
while(*(dest++)=*(src++));
}
// or

void strcopy(char *dest, char *src)
{
for(;*(dest++)=*(src++););
}
// spiffy eh?



just goes to show the many ways to solve a problem (and this is not all the methdos that you can use).
0

Share this post


Link to post
Share on other sites
Yet another way:

void strcopy(char* str1,char* str2)
{
while (*str1++ = *str2++)
;
}

Much faster than the previous methods.
0

Share this post


Link to post
Share on other sites
  Just assign the length of the string before the loop and use the value in the condition.

str_length = strlen(str1);
for(i=0;i<str_length;i++)
{

}

Faster code


Yeah, I like your way better hehe, good to teach good
optomization techniques early.

Demonlir, if you're reading this, look at his code, it
takes up a little more memory, but instead of calling that
function every time of the loop, it checks it against the
variable, which is much faster. The computer knows that
the variable is for example 80. And instead of checking to see
how long the string is every time in the loop, it just checks it
against 80. Much more efficient.

-Lohrno

PS - heh I write that not only for him but for me! I gotta get
into those good habits too =)


Edited by - Lohrno on February 12, 2002 12:24:21 PM
0

Share this post


Link to post
Share on other sites