[C] Is this proper way of passing an array to be manipulated

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jake Putz
    New Member
    • Jul 2010
    • 1

    [C] Is this proper way of passing an array to be manipulated

    I have limited experience with C and some experience with C++. I am developing code to be used on a microcontroller (which is why I am using the keyword "unsigned") and I want to make sure I am doing this correctly. I want to fill an unfilled array in main using a subroutine, is this the proper way of passing a variable in this situation?

    Code:
    #include <stdio.h>
    
    void fillZero(unsigned int *data[], unsigned int size);  //Function Prototype
    
    int main(void) {
    	
    	unsigned int myData[2];
    	
    	fillZero(myData[], 2);
    
    	// Additional code that uses the now filled array
    	
    	return(0);
    	
    }
    
    void fillZero(unsigned int *data[], unsigned int size) {
    	
    	for (int i=0; i < size; i++) {
    		
    		data[i] = 0;
    		
    	}
    	
    }
    Thank you for the help.

    Jake
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Did it compile?

    Any reason you can't just use memset from the C standard library?

    Code:
    void fillZero(unsigned int *data[], unsigned int size);  //Function Prototype
    No this isn't right, [] used in a function prototype is equivalent to declaring a pointer so your type data has type unsigned int **, not what you mean I suspect.

    Either us the * or use the [].

    Loose the [] from line 9 where you call your function.

    Comment

    Working...