Sizeof(char[]);

New to XMOS and XCore? Get started here.
JohnRobert
Active Member
Posts: 34
Joined: Fri Nov 02, 2012 8:10 am

Sizeof(char[]);

Postby JohnRobert » Sun Feb 03, 2013 9:17 am

G'day,

I have the following code...

Code: Select all

void writeStr(char message[]){
	for(int x = 0; x<sizeof(message)-1; x++){
		txByte(TXD, (int)message[x]);
	}
}
The IDE is telling me: "invalid application of `sizeof'"

Why is this, and how to fix it?

Thanks,

John
User avatar
pstnotpd
XCore Addict
Posts: 161
Joined: Sun Jun 12, 2011 11:47 am

Postby pstnotpd » Sun Feb 03, 2013 9:54 am

In ANSI C an array is passed as a pointer to the first element, i.e. by reference

I think the issue is you're trying to pass an un-sized array so the sizeof function cannot be aware of the size.

Examples usually pass the actual size of the array along with the array itself.

i.e.

Code: Select all

void writeStr(char message[],int arr_size) {
....
I've also found examples where you specify the size in the parameter, but as you appear to want flexibility in size this would not be an option.

Who is online

Users browsing this forum: No registered users and 0 guests