I need to pass a null to a function that has a nullable type for an argument.
The XMOS programming guide talks about nullable types but fails to mention how one creates a null pointer. ARG!
I've tried passing 'null','NULL' and '0' to the function but get undeclared for the nulls and invalid argument for the '0'.
Simple example:
void function(server an_if ?i_an_if){
}
main()
{
function(NULL); // undeclared error for NULL
}
So, how does one pass a null pointer to a function that has a nullable argument?
Creating a null type (was pointer)
-
- Experienced Member
- Posts: 94
- Joined: Sun Feb 10, 2013 4:47 am
Creating a null type (was pointer)
Last edited by babazaroni on Wed Jun 24, 2015 2:29 pm, edited 1 time in total.
-
- XCore Addict
- Posts: 158
- Joined: Thu Mar 20, 2014 8:04 am
#include <stdio.h>
void function(int * unsafe a){
if (a == null)
printf("null pointer!!!");
}
int main()
{
function(null); // undeclared error for NULL
}
/* compiled using 14.0.3 Community xTIMEcomposer */
C:\Users\xmos_addict>xsim C:\Users\xmos_addict\null_pointer_eg\bin\null_pointer_eg.xe
null pointer!!!
void function(int * unsafe a){
if (a == null)
printf("null pointer!!!");
}
int main()
{
function(null); // undeclared error for NULL
}
/* compiled using 14.0.3 Community xTIMEcomposer */
C:\Users\xmos_addict>xsim C:\Users\xmos_addict\null_pointer_eg\bin\null_pointer_eg.xe
null pointer!!!
-
- XCore Addict
- Posts: 158
- Joined: Thu Mar 20, 2014 8:04 am
Another version tried using the interface method:
-------------------------------------------------------
#include <print.h>
#include <stdio.h>
interface my_interface {
void msg(int * p);
};
void task1(client interface my_interface c)
{
int a[5] = {0,1,2,3,4};
int *p = null; //&a[0];
{c.msg(p);}
printf("task1 - bye\n");
}
void task2(server interface my_interface c)
{
select {
case c.msg(int * p):
if (p != null ) {printintln(*p);
printintln(*(p+2));}
break;
}
printf("task2 - bye\n");
}
int main() {
interface my_interface c;
par {
task1(c);
task2(c);
}
return 0;
}
====================
C:\Users\srini>xsim C:\interfaceparams.xe
task2 - bye
task1 - bye
Reference: Adapted from AN10025
-------------------------------------------------------
#include <print.h>
#include <stdio.h>
interface my_interface {
void msg(int * p);
};
void task1(client interface my_interface c)
{
int a[5] = {0,1,2,3,4};
int *p = null; //&a[0];
{c.msg(p);}
printf("task1 - bye\n");
}
void task2(server interface my_interface c)
{
select {
case c.msg(int * p):
if (p != null ) {printintln(*p);
printintln(*(p+2));}
break;
}
printf("task2 - bye\n");
}
int main() {
interface my_interface c;
par {
task1(c);
task2(c);
}
return 0;
}
====================
C:\Users\srini>xsim C:\interfaceparams.xe
task2 - bye
task1 - bye
Reference: Adapted from AN10025
-
- Experienced Member
- Posts: 94
- Joined: Sun Feb 10, 2013 4:47 am
Thanks, srinie, for the replies. I should not have said null pointer but null type. I've changed title.
The '?' in front of the arguement type means that it is nullable and can be tested with the isnull macro, like below:
void function(server an_if ?i_an_if)
{
if (!isnull(i_an_if) do_something(i_an_if);
}
Now I need to find out how to pass a null type.
The '?' in front of the arguement type means that it is nullable and can be tested with the isnull macro, like below:
void function(server an_if ?i_an_if)
{
if (!isnull(i_an_if) do_something(i_an_if);
}
Now I need to find out how to pass a null type.
-
- Experienced Member
- Posts: 94
- Joined: Sun Feb 10, 2013 4:47 am
The problem has been found.
'null' was undefined because I had not included xs1.h.
'null' can be passed to a function with nullable types.
'null' was undefined because I had not included xs1.h.
'null' can be passed to a function with nullable types.