This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.
>To: address@hidden >cc: address@hidden >From: Bill Cassanova <address@hidden> >Subject: multi-dimensional array writes in c++ >Organization: UCAR/Unidata >Keywords: 200201031845.g03IgdN17359 Hi Bill, > I am using the netCDF c++ interface and have not been able to find > an example of doing a multi-dimensional put. > > Here's a couple of code snippets. > > First the CDL: > netcdf foo{ > dimensions: > msn = 18000 ; > ftd = 1 ; > fvd = 10 ; > variables: > int site_list(msn) ; > float maxt_f(msn, fvd) ; > float mint_f(msn, fvd) ; > int icon_code1(msn, fvd) ; > }; > > Now the C++ portion: > float foo[10][10]; > for( int i = 0; i < 10; i++ ){ > for( int j = 0; j < 10; j++ ){ > foo[i][j] = 999; > } > } > > NcVar *var=nc->get_var( "maxt_f" ); > assert(var); > var->put( foo, 10, 10 ); > > The problem is the netCDF interface requires a float * instead of a > float **. Is there some way other than allocating another float * > and copying foo into it to make this work? > > I've tried: > var->put((int*)*foo, 10, 10 ); > var->put(*foo[0], 10, 10 ); Try var->put(&foo[0][0], var->edges()); or equivalently var->put(foo[0], var->edges()); There's an example that puts data in a 3-dimensional array in the distributed netCDF source in the file src/cxx/example.cpp which is supposed to do the same thing in C++ that the equivalent C code does in src/cxx/example.c Generally in either C or C++, if the prototype of a function requires a foo* argument (a pointer to a foo), then you can supply a &foo (the address of a foo). Since the netCDF C++ interface requires a float* and foo[0][0] is a float, then &foo[0][0] is a float* so is the right type. You could also use just foo[0], since a diagram of how arrays work in C (or C++) shows that's the same as &foo[0][0]. Please let us know if this doesn't work. --Russ