Home > Blockchain >  What's the difference between declaring an array in C like this: int a[n] and int a<n>
What's the difference between declaring an array in C like this: int a[n] and int a<n>

Time:12-18

I was trying to do some RPC matrix sending from client to server... and some gut who I don't remember put his .x file with some "array" ¿? declared like this: int X <100>; within an strcuture of the .x... this is his structure created:

struct X_array{
    int X <100>;
    int X_size;
}

Funny thing is that in his client code he's got some like this: (i'll give it kinda sumarized)

printf("Give size)";
scanf("%d",&n);
average_1_arg.X.X_len=n;
avegare_1_arg.X_size=n;
average_1_arg.X.X_val=(int*)malloc(n*sizeof(int));

In his .x, that function is declared as: float average(X_array)=1;

WHERE DOES HE GET THOSE PARTS OF HIS STRCUTURE?

And the most insane thing for me is that I compiled his .x from 0 with "rpcgen -a -C dum.x", then I wrote the client and server the very same as he has in his .c's, I mean, client and server .c's and after that put on console "make -f Makefile.dum" and then ran the ./dum_server and ./dum_client and everything works good with no errors. My mindblowing is that with the parts from the X_array structure that are not in the .x, are in the client code and those work without even declaring the type of that variable; even if those were pointers from the "int X <100>;" stuff, there is an int (.X_len) and an acutal pointer (.X_val) in there. Now I have experimented by changing those pair of "<>" for pair of "[]", and compiled until the command "make -f Makefile.dum", where the compiled gave me many errors. I'd appreciate your help.

A buddy from college gave that code to me, so I don't knwo its origin to ask directly to the creator. Thanks. (:

CodePudding user response:

int X <100>; isn't C. It's XDR input for RPCGEN, so refer to its documentation for the syntax. RPCGEN will process its input file into C code, which may include automatically declaring a struct with a bunch of additional members.

See 6.8 (3) in the RPCGEN manual for "variable-length array declarations", which in particular explains the creation of the _len and _val members.

  • Related