Re: const variables



On May 1, 11:06 pm, Martin Ambuhl <mamb...@xxxxxxxxxxxxx> wrote:
Sheldon wrote:
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.

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[i]);   /* 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 * (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
.



Relevant Pages

  • Re: segfault w/ block, but not file scope
    ... void foo{ ... possess by-reference arguments, even ordinary local variables ... The "value" of the array is a pointer to the array's ...
    (comp.lang.c)
  • Re: Aaahhrg! Trouble passing an array of structs to a function!
    ... I like your idea of using a pointer to void as a parameter. ... This is understood as I can compare the data from the array passed in to the ... typedef struct pc_g{ ...
    (microsoft.public.vc.language)
  • Re: qsort semantics
    ... must pass a pointer to the first object of the array to be sorted. ... int cmp(const void *a, const void *b) { ... You cast a to "pointer to an array of N char". ...
    (comp.lang.c)
  • Re: VC++.NET 2003 should not issue C4090 ompiler warning in the following case
    ... Only the cell of the array is of type ... The second actually declares foo to be an array of void* const. ... > typedef const MYTYPE MYCONSTTYPE; ...
    (microsoft.public.vc.language)
  • Re: A little help please
    ... Well I guess it would be if I didn't know I was passing an array of ints ... incrementing the pointer by nBytes I was pointing to next array element.? ... array to a void* using a static cast. ...
    (alt.comp.lang.learn.c-cpp)