Thread: function asking for unsigned char *

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    61

    function asking for unsigned char *

    hello,

    i have a function which is asking for an unsigned char * but when i initialise an unsigned char array

    Code:
    unsigned char data[1024];
    and pass it like so:

    Code:
    ouput((unsigned car *)data)
    it givese me a bus error

    does anyone know what is happening
    (no compiler warnings) i have -Wall on

    thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Note that you have defined an array, but you have not initialized it. If your output function expects a null-terminated string, for instance, then you will very likely be Really Out of Luck.

    So: do you put valid data in the data variable before passing it on? And why do you cast it anyway?

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    61
    lol sorry

    Code:
    unsigned char data[1024] = {  0x0a, 0x00 };
    thats how i initialise it

    -- if i dont cast it gives errors

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I would Call That Strange. Why don't you post the mystery function prototype?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    61
    phew! i though i was going insane

    Code:
    int output(unsigned char *data); //prototype

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If data is defined as
    Code:
    unsigned char data[1024] = {  0x0a, 0x00 };
    and if output is declared as
    Code:
    int output(unsigned char *data); //prototype
    then
    Code:
    output(data);
    must compile and if it doesn't your compiler is broken. Provide more details.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    So you could use that like this:
    Code:
    #include <stdio.h>
    
    int output(unsigned char *data) {
    	printf("%s\n",data);
    	return 42;
    }
    
    int main() {
    	unsigned char string[]="number of the beast";
    	output(string);
    	return 0;
    }
    You could replace the declaration with:
    Code:
    unsigned char data[1024] = {  0x0a, 0x00 };
    which won't hurt -- just the final result is spookier.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    61
    hmm, I think maybe my problem is in output()

    here it is
    Code:
    int output(unsigned char *data) {
    
    int num_bytes =2;
    
    	// use the rfcomm_pcb passed into the rfcomm_connected function as the 'global' pcb 
    	
    	// Put data into rfcomm_pcb payload, send it out then free the buffer
    	struct pbuf *p;
    	p = pbuf_alloc(PBUF_RAW, num_bytes, PBUF_RAM);
    	memcpy( ((u8_t *)p->payload) , data, num_bytes); // <----------is this whats stuffing?
    
    if (0) {
    printf("\n sending via rfcomm... \n");
    int i;
    for (i=0; i< num_bytes; i++) {
    	printf("data[%d] = %d \n", i, ((u8_t *)p->payload)[i] );
    }
    }
    	if(rfcomm_cl(global_pcb)) {
    		// why 6 credits??
    //		printf("using uih_credits = 6 \n");
    		rfcomm_uih_credits(global_pcb, 6,  p);
    	} else {
    		rfcomm_uih(global_pcb, rfcomm_cn(global_pcb), p);
    	}
    
    	pbuf_free(p);
    
    	return ERR_OK;
    
    }
    thanks for your help

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    61
    yes, its definately with the function, i can print my bytes inside it and the error is occuring elsewhere in that func -- thanks for your help!

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. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM