help-gsl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-gsl] Re: Declaring GSL_Vector Globally


From: Matthew Boulton
Subject: [Help-gsl] Re: Declaring GSL_Vector Globally
Date: Wed, 15 Aug 2007 16:42:11 +0100
User-agent: Thunderbird 2.0.0.6 (Windows/20070728)

After some work I've been able to find a solution so I thought I'd post it up in case anyone else ever has the same question:

Basically I created a main function and a secondary function that altered one of the vectors to check that the vector was truly global. The declarations at the beginning of each file are a slight pain, but these can easily be incorporated into a custom header file.

--main.c:

#include <stdio.h>
#include <gsl/gsl_vector.h>

gsl_vector *v1;
gsl_vector *v2;

/* Declare functions */

void alter_vec();

/* Main function */

int main()

{

 int ncomp;

 ncomp = 1;

 v1 = gsl_vector_calloc( ncomp + 1 + 100 );
 v2 = gsl_vector_calloc( ncomp + 1 + 100 );

 gsl_vector_set_all( v1 , 10.0 );
 gsl_vector_set_all( v2 , 20.0 );

 alter_vec();

 gsl_vector_fprintf( stdout , v1 , "%g" );
 gsl_vector_fprintf( stdout , v2 , "%g" );

 return 0;

}


--alter_vec.c

#include <stdio.h>
#include <gsl/gsl_vector.h>

gsl_vector *v1;
gsl_vector *v2;

void alter_vec() /* Code altering v1 */

{

 gsl_vector_set_all( v1 , 30.0 );

 return;

}


Kind Regards,

Matt


Matthew Boulton wrote:
Hello. Is it possible to declare a GSL Vector globally without declaring it at the very beginning of the code before main?

Basically I have tried using extern as I usually do with integer variables, however since the GSL Vector seems to be declared and initialised all in one line, I can't seem to declare it at the beginning of the code, then declare it as external when I initialise it. For example:

#include <stdio.h>

int num;

int main()

{

 extern int num;
 num = 10;
 return 0;

}

is my usual method, but using this with GSL vectors as follows:

#include <stdio.h>
#include <gsl/gsl_vector.h>

gsl_vector *check_vec;

int main()

{

 int ncomp;

 ncomp = 100;

 extern gsl_vector *check_vec = gsl_vector_calloc( ncomp + 1 + 100 );
 return 0;

}

will not work.

Kind Regards,

Matt





reply via email to

[Prev in Thread] Current Thread [Next in Thread]