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.
Thomas, > Anyway, I'm using the C interface inside my C++ class now, as I'm > more sure (now) about what happens there (although it's not nice > that it segfaults when trying to get the id to a non-existent > variable name instead of just indicating an error...). I can't reproduce this problem. Could you try compiling and running the small C program I have appended to the end of this message? It just tries to get the id of a variable named "no_such_variable" from whatever netCDF file is named as its command-line argument. When I run it, the output is as expected: $ ./varname test.nc line 24 of varname.c: Variable not found rather than a segfault. If you get a segfault, there must be something wrong with your netCDF installation. In that case, please let us know what version you have installed and if you built from source, whether you ran "make test" after building the library. --Russ #include <stdio.h> #include <stdlib.h> #include <netcdf.h> void check_err(const int stat, const int line, const char *file) { if (stat != NC_NOERR) { (void) fprintf(stderr, "line %d of %s: %s\n", line, file, nc_strerror(stat)); exit(1); } } int main(int argc, char *args[]) { /* test nc_inq_varid for non-existent variable */ int ncid, varid, stat; stat = nc_open(args[1], NC_NOWRITE, &ncid); check_err(stat,__LINE__,__FILE__); stat = nc_inq_varid(ncid, "no_such_variable", &varid); check_err(stat,__LINE__,__FILE__); (void) printf("Should never get here\n"); return 0; }