const variables

S

Sheldon

Hi,

I have a function defined in the netcdf library as :

int nc_put_att_short (int ncid, int varid, const char *name,
nc_type xtype, size_t len, const short *sp);

Where the 5th argument is a pointer to short constant variable. In my
case this variable is a rather large array.
When I created and initialized the array I had to assign data to the
array before calling the function above.
How can I make my array match what this function is asking for, i.e. a
const short pointer?

I have several arrays to write so I store them in a struct. So my
pointer is of type: struct data *ptr and I have to then call the
above function to write the data.

My question is how do I make ptr->array a variable of type const short
*sp?

Any help is greatly appreciated!

/Marston
 
D

Default User

Sheldon said:
Hi,

I have a function defined in the netcdf library as :

int nc_put_att_short (int ncid, int varid, const char *name,
nc_type xtype, size_t len, const short *sp);

Where the 5th argument is a pointer to short constant variable. In my
case this variable is a rather large array.
When I created and initialized the array I had to assign data to the
array before calling the function above.
How can I make my array match what this function is asking for, i.e. a
const short pointer?

I have several arrays to write so I store them in a struct. So my
pointer is of type: struct data *ptr and I have to then call the
above function to write the data.

My question is how do I make ptr->array a variable of type const short
*sp?

Just because the parameter is const doesn't mean the argument has to
be. All that does is promise that the function won't change the
argument.

Make struct member an array of short of whatever size you need. You
could make it const, but don't do so unless you want it that way for
reasons besides its use as a function argument.




Brian
 
J

jameskuyper

Sheldon said:
Hi,

I have a function defined in the netcdf library as :

int nc_put_att_short (int ncid, int varid, const char *name,
nc_type xtype, size_t len, const short *sp);

Where the 5th argument is a pointer to short constant variable. In my
case this variable is a rather large array.
When I created and initialized the array I had to assign data to the
array before calling the function above.
How can I make my array match what this function is asking for, i.e. a
const short pointer?

I have several arrays to write so I store them in a struct. So my
pointer is of type: struct data *ptr and I have to then call the
above function to write the data.

My question is how do I make ptr->array a variable of type const short
*sp?

Your question fails to say a number of things that make it unclear
exactly what the problem is. You have to have an array of short which
contains your data, in order to pass it to that function, so creating
and initializing such an array is unavoidable.

Since the Subject: line mentions 'const' variables, is that's what's
worrying you? The pointer that you pass to nc_put_att_short() doesn't
have to point to a const short array; the 'const' merely indicates
that the function should not change the contents of that array. If the
data is already in a short array, then you can just pass a pointer to
that array to the function, if that pointer doesn't have 'const' in
it's type, that's not a problem.

Is your data currently stored in an array of some other type, such as
int or double? In that case, you have to create an array of short, and
fill it in by converting the data from the other array - there's
really no way around that.
 
M

Marston

Sheldon said:
I have a function defined in the netcdf library as :
int nc_put_att_short     (int ncid, int varid, const char *name,
nc_type xtype, size_t len, const short *sp);
Where the 5th argument is a pointer to short constant variable. In my
case this variable is a rather large array.

You probably mean the sixth argument: the fifth argument is 'size_t len'.
When I created and initialized the array I had to assign data to the
array before calling the function above.
How can I make my array match what this function is asking for, i.e. a
const short pointer?

It 'matches' already.  The prototype only says that the function will
treat whatever sp points to as const.

Look at the following code and try to compile with a reasonably high
level of diagnostics:
#include <stdio.h>

void foo1(size_t n, const short *v);    /* leaves v alone */
void foo2(size_t n, const short *v);    /* tries to change v */

int main(void)
{
     short array[] = { 3, 4, 5 };
     size_t size = sizeof array / sizeof *array;
     foo1(size, array);
     foo2(size, array);
     foo1(size, array);
     return 0;

}

void foo1(size_t n, const short *v)
{                               /* leaves v alone */
     size_t i;
     printf("foo1 just prints the contents of the array\n");
     for (i = 0; i < n; i++)
         printf("v[%zu] = %d\n", i, v);   /* this should _not_ give a
                                                diagnostic */
     putchar('\n');

}

void foo2(size_t n, const short *v)
{                               /* tries to change v */
     size_t i;
     for (i = 0; i < n; i++)
         v = i * (i + 2);     /* this line should give a diagnostic,
                                    since this is an assignment to a
                                    read-only location. */

}
I have several arrays to write so I store them in a struct. So my
pointer is of type:  struct data *ptr and I have to then call the
above function to write the data.
My question is how do I make ptr->array a variable of type const short
*sp?

There is no need to do so.


Thanks. I see the point. Very informative.

/Mmm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top