Thread: Passing a char* by reference in C++

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  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, 10:18 AM