Passing a char* by reference in C++

This is a discussion on Passing a char* by reference in C++ within the C++ Programming forums, part of the General Programming Boards category; Below I've presented the declaration, definition and the calling method of a function w.r.t pass by reference: Declaration Code: void ...

  1. #1
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115

    Passing a char* by reference in C++

    Below I've presented the declaration, definition and the calling method of a function w.r.t pass by reference:

    Declaration
    Code:
    void getUnpackedMsg (unsigned char &data, unsigned int numberOfBits);
    Definition
    line 278: Shown in RED
    Code:
    void packedMessage :: getUnpackedMsg (unsigned char &emptyArray, unsigned int numberOfBits)
    {
    ...
    ...
    printf ("\nAfter unpacking 0: %d ", emptyArray[0]);
    }
    Calling
    Code:
    q.getUnpackedMsg (*empty0, 8);
    Error Messages:
    Code:
    workingA.cpp:278: error: invalid types ‘unsigned char[unsigned int]’ for array subscript

    Kindly point me the error here !

    P.S.
    By Mistake I have posted this in C forum, It should go to C++ forum. How to inform the moderator now ?
    Last edited by AnishaKaul; 04-26-2010 at 05:12 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    Moved to the C++ programming forum.

    If your aim is to pass a char* by reference, then the declaration should be:
    Code:
    void getUnpackedMsg (char*& data, unsigned int numberOfBits);
    What you are doing now is passing an unsigned char by reference.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Thanks for bothering !

    Your method is working without errors now !

    Kindly tell me what is the way to inform the moderators here about mis-placed posts ?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    The way you did is just fine. Though we usually manage to figure it out without prompting
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Quote Originally Posted by Salem
    The way you did is just fine. Though we usually manage to figure it out without prompting
    Thanks for bothering !

    Actually in LinuxQuestions.org we have a Report button through which we can inform the moderators about the spams, mis-placed posts etc.

    I couldn't find anything of that sort here, that's why I got worried about informing the moderators !

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    There is such a button. A red triangle right beside the name.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Quote Originally Posted by Elysia
    There is such a button. A red triangle right beside the name.
    Many thanks for the enlightenment !

    I did see it earlier but i didn't know it was a button until you told me !

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    But that isn't for something as trivial as getting a post moved (read the words).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Ok now i read the words, I won't do anything next time if i post in wrong forum ! Thanks..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

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