Function Call question.

I was reading Linux socket programing and came across something I have not yet see.


strncpy(adr_unix.sun_path,pth_unix,
sizeof adr_unix.sun_path -1)
[sizeof adr_unix.sun_path - 1] =0; <--- what is this


I understand how to use strncpy not a problem but if you will look above you will see what I am asking about the function is not ";" terminated.It looks like type of indirection not sure anyone know? If you do can you point to the right direction so I can research this more.

Thanks Vinson


Hi,

Thanks I what the function does, what I don't know is the syntax that is going on.

When I use strncpy it would be like this.

strncpy(adr_unix.sun_path,pth_unix,
sizeof adr_unix.sun_path -1) ;

But in the example you see:

strncpy(adr_unix.sun_path,pth_unix,
sizeof adr_unix.sun_path -1) //<----- no “;”
[sizeof adr_unix.sun_path - 1] =0; // <---- it is terminated here.

I changed it to

strncpy(adr_unix.sun_path,pth_unix,
sizeof adr_unix.sun_path -1) ;

[sizeof adr_unix.sun_path - 1] =0;

And get error:

/home/vinson/af_unix2/src/main.c:53:4: error: expected expression before ‘[’ token

So something is going on here, just trying to find out.


Thanks vinson

The relevant part was "strncpy returns a pointer.".
That line does the same as
1
2
char* dest=strncpy(adr_unix.sun_path,pth_unix,sizeof adr_unix.sun_path -1);
dest[sizeof adr_unix.sun_path - 1] =0;

or

1
2
strncpy(adr_unix.sun_path,pth_unix,sizeof adr_unix.sun_path -1);
adr_unix.sun_path[sizeof adr_unix.sun_path - 1] =0;
Thank you very much, just need to have to kicked into my head.

Vinson
Topic archived. No new replies allowed.