Thread: Can't pass and array??

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    53

    Can't pass and array??

    Hello,

    here's some code:

    Code:
    uint8_t receiveBuffer[256];  // Array filled elsewhere! 
    
    
    
    void abc(uint8_t *receiveBuffer[]){
    uint8_t a[256];
    
    strncpy(a,receiveBuffer, 50);   //<<< a has junk contents????
    }
    
    
    
    
    int main(void)
    {
        abc(receiveBuffer);
    
    }
    Why can't a array contain the contents of receiveBuffer???

    Thank you all for your help

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This declares abc as a function with void return type that has a parameter of type uint8_t**:
    Code:
    void abc(uint8_t *receiveBuffer[]){
    Notice the double asterisks, i.e., receiveBuffer is declared to be a pointer to a pointer to uint8_t. Now, when you pass an array of 256 uint8_t as an argument, it is converted to a pointer to its first element. Therefore, you probably want:
    Code:
    void abc(uint8_t receiveBuffer[]){
    or equivalently:
    Code:
    void abc(uint8_t *receiveBuffer){
    Note that this kind of type mismatch is something that your compiler can warn you about, so compile at a high warning level and pay attention to compiler warnings.
    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
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why can't a array contain the contents of receiveBuffer???
    First strncpy() requires the source "array" to be a valid C-string. Are you sure receiveBuffer is a properly terminated C-string?

    Second your receiveBuffer may contain upto 256 characters yet you're only trying to copy 50 characters. Remember that strncpy() doesn't automatically add the end of string character, you need to manually insure that the end of string character exists and add it if it doesn't.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    53
    Hi,

    Okay, it works .... I shouldn't be using strcpy since they are numbers
    so I simply accessed the array by an equals sign see code below.

    Code:
    uint8_t receiveBuffer[256];
    
    
    void abc(uint8_t receiveBuffer[]){
    uint8_t a[256];
    
    //strncpy(a,receiveBuffer, 50);
    
    a[12] = receiveBuffer[12];
    }
    
    
    
    int main(void)
    {
       abc(receiveBuffer);
    
    }

    Its been 5 years I stopped C... LOL ... I would need a good refresher LOL

    Thanks so much for your help!
    Bob

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try memcpy() then.
    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.

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    53
    Yes thank you Salem
    StbC

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-10-2016, 01:23 AM
  2. Replies: 3
    Last Post: 05-09-2012, 06:41 AM
  3. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  4. Pass an array from C++ to C#?
    By So and So in forum C++ Programming
    Replies: 2
    Last Post: 07-19-2004, 03:25 PM
  5. Replies: 3
    Last Post: 04-02-2002, 01:39 PM

Tags for this Thread