Passing string along function. Pointer.

This is a discussion on Passing string along function. Pointer. within the C++ Programming forums, part of the General Programming Boards category; I'm making a 13h, DJGPP-compiled game. I just wrote a function that writes letters to the screen (and it took ...

  1. #1
    Ecologist
    Join Date
    Aug 2001
    Location
    Utah.
    Posts
    1,291

    Passing string along function. Pointer.

    I'm making a 13h, DJGPP-compiled game. I just
    wrote a function that writes letters to the
    screen (and it took me a long time. Not a good
    programmer )The function accepts two parameters;
    a position-variable, and a text-string...

    Code:
    #define A 0
    #define B 1
    #define C 2
    
    class cXYCOORD{
      public:
        int x;
        int y;
    };
    
    void WriteText(cXYCOORD Position, char *string){
      int StringLength = strlen(string);
      int Pixels = Position.x;
      
      for(int i=0;i<StringLength;i++){
        int choice = string[i];
        switch(choice){
          case 65: Alphabet[A]->image->DrawImage(Position); break;
          case 66: Alphabet[B]->image->DrawImage(Position); break;
         ...
         /*More Lines of Code. Moves to another line,
           etc. They aren't important, though*/
        }
      }
    }
    Here's my question. Back at the old, blue cprog
    board, I asked how to pass a text-string along
    a function. Someone said I should declare a
    char pointer as a parameter. I did. char *string.

    What exactly is this variable holding when I
    pass a string through it, though? Pointers hold
    variable-addresses, right? What am I assigning to
    this pointer? It isn't an address... Is this a
    bad way of passing a string? Will my program/game
    screw up later?

    WriteText(RightHere, "Write This");

    Thanks a lot,
    ethic.

  2. #2
    Davros
    Guest
    > Pointers hold
    variable-addresses, right? What am I assigning to
    this pointer? It isn't an address...

    That's exactly what you are doing. You are passing an address to the first character in the string. The compiler knows where the string ends because, in C++, strings are always terminated with a NULL (zero) character.

    Usually, pointers are the best way to pass data around, because you are only passing an (32 bit) address to some data. If you were to pass the data itself, this could be mean large amounts of memory are copied on each call (which is slower).

    > Will my program/game
    screw up later?

    Not if you take care of deleting any heap memory you use. The code you have given seems OK.

    Let me give some example:


    [\code]

    char s[] = "Hello World";
    cXYCOORD p;
    p.x = 5;
    p.y = 5;
    WriteText(p, s);

    [code]

    This is fine because s is declare on the stack; the compiler knows how big it is and will ensure its deleted when it goes out of scope.

    However, the following (on its own) is not fine:

    [\code]

    char* s = new char[20];
    strcpy(s, "Hello World");
    cXYCOORD p;
    p.x = 5;
    p.y = 5;
    WriteText(p, s);

    [code]


    Because you have declare s as a pointer and created the memory for it on the heap, which is not deleted.

    In this case, you need to call:

    delete[] s;

    after the call to WriteText.

    Pointer in C/C++ may seem complicated, but it's really worth learning if you want write reliable & fast code. It's a bit to much to describe in detail here, however. I suggest you find a good book, it does really all make sense.

  3. #3
    Davros
    Guest
    Damm! I still can't remember my code tags.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    edit your post and fix them!
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  5. #5
    Davros
    Guest
    > edit your post and fix them!

    Can't. Not registered. Don't wanna a be.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  2. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21