ElEscalador
Posts: 861
Joined: Tue Dec 15, 2015 4:55 pm
Location: Detroit, MI USA
Contact: Website

Generate a variable in an included file - keep it for others

Wed Oct 05, 2016 3:27 pm

Say I need to generate a variable with a function - that function is not in my main file, but rather an included file (sensors.cpp). I then need to other functions in sensors.cpp to access this variable. My first thought was to generate it as a static int, but then it is still only available in the function that created it. Do I need to create a pointer to a global static variable in main.cpp? I'm guessing there is a smarter, more conventional way...
Robotics tips, hacks, book extras https://youtube.com/practicalrobotics

User avatar
joan
Posts: 15112
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Generate a variable in an included file - keep it for ot

Wed Oct 05, 2016 3:34 pm

You're probably meant to use a class variable in C++. In C I'd just declare the variable extern.

User avatar
PeterO
Posts: 5974
Joined: Sun Jul 22, 2012 4:14 pm

Re: Generate a variable in an included file - keep it for ot

Wed Oct 05, 2016 3:42 pm

joan wrote:You're probably meant to use a class variable in C++. In C I'd just declare the variable extern.
In C I would make it a static global , thus restricting its scope to the source file it is declared in (which seems to match the OP's requirement).

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

ElEscalador
Posts: 861
Joined: Tue Dec 15, 2015 4:55 pm
Location: Detroit, MI USA
Contact: Website

Re: Generate a variable in an included file - keep it for ot

Wed Oct 05, 2016 4:11 pm

PeterO wrote:
joan wrote:You're probably meant to use a class variable in C++. In C I'd just declare the variable extern.
In C I would make it a static global , thus restricting its scope to the source file it is declared in (which seems to match the OP's requirement).

PeterO
Thank you both. Can I declare a global within a source file (besides main)? I thought anything in the included files had to be declared within a function...or does c and/or c++ read the setup stuff in those files like it does in main.c?
Robotics tips, hacks, book extras https://youtube.com/practicalrobotics

User avatar
PeterO
Posts: 5974
Joined: Sun Jul 22, 2012 4:14 pm

Re: Generate a variable in an included file - keep it for ot

Wed Oct 05, 2016 4:23 pm

ElEscalador wrote:
PeterO wrote:
joan wrote:You're probably meant to use a class variable in C++. In C I'd just declare the variable extern.
In C I would make it a static global , thus restricting its scope to the source file it is declared in (which seems to match the OP's requirement).

PeterO
Thank you both. Can I declare a global within a source file (besides main)? I thought anything in the included files had to be declared within a function...or does c and/or c++ read the setup stuff in those files like it does in main.c?
You seem a little confused about how C/C++ programs should be structured.

You don't "include" .c or .cpp files in other .c or .cpp files , you compile them separately to .o files then link them together as the final step.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
joan
Posts: 15112
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Generate a variable in an included file - keep it for ot

Wed Oct 05, 2016 4:25 pm

You can declare variables outside functions.

If you declare a variable outside a function by default it may be used by any file. If you specify it as static it has file scope so may only be used within that file.

If you declare a variable static within a function it preserves its value between function invocations.

swampdog
Posts: 420
Joined: Fri Dec 04, 2015 11:22 am

Re: Generate a variable in an included file - keep it for ot

Wed Oct 05, 2016 6:44 pm

Two flavours..

"foo.h"

Code: Select all

#ifndef FOO_H
#define FOO_H FOO_H

extern int foo;
char* bar(void);

#endif	/* FOO_H */
"foo.c"

Code: Select all

#include "foo.h"

int foo;

char * bar(void)
{static char s[1024];
 return s;
}
"c.c"

Code: Select all

#include <stdio.h>
#include <string.h>

#include "foo.h"

int
main()
{
 foo = 69;
 strcpy(bar(),"Hello World");

 printf("%d\n",foo);
 printf("%s\n",bar());

 return 0;
}
$gcc -o c c.c foo.c && ./c

ElEscalador
Posts: 861
Joined: Tue Dec 15, 2015 4:55 pm
Location: Detroit, MI USA
Contact: Website

Re: Generate a variable in an included file - keep it for ot

Wed Oct 05, 2016 11:33 pm

Awesome - thank y'all...I seem to have this part working.
Robotics tips, hacks, book extras https://youtube.com/practicalrobotics

ewaller
Posts: 32
Joined: Tue Oct 04, 2016 5:24 pm

Re: Generate a variable in an included file - keep it for ot

Wed Oct 05, 2016 11:48 pm

Another way to handle this (and in my opinion a better way) is to not expose the variable in a .h file, but rather to keep it static and only available within the source file in which it is defined. Then, provide a function that returns the value of that variable and expose the function to the outside world. That way, other functions cannot mess with the variable (either deliberately or through a bug). If you want other functions to be able to mess with it, provide a function to set the variable -- and enforce sanity on the values that are passed in prior to assigning the variable the new value.

swampdog
Posts: 420
Joined: Fri Dec 04, 2015 11:22 am

Re: Generate a variable in an included file - keep it for ot

Thu Oct 06, 2016 10:41 am

I did that with the bar() function above. Now I'm going to be really silly..

Code: Select all

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

typedef struct foo {
char *s;
void (*ctor)(struct foo*);
void (*dtor)(struct foo*);
struct foo* (*opeq)(struct foo*,const char[]);
} FOO;

void     
str_ctor(FOO*self)
{
 self->s = malloc(1);
 self->s[0] = '\0';
}

void 
str_dtor(FOO*self)
{
 free(self->s);
}

FOO*
str_opeq(FOO*self,const char s[])
{
 self->s = realloc(self->s,strlen(s)+1);
 strcpy(self->s,s);
}

int
main(void)
{FOO    f;

 f.ctor = str_ctor;
 f.dtor = str_dtor;
 f.opeq = str_opeq;

 f.ctor(&f);
 f.opeq(&f,"I am not a C++ compiler!");
 printf("%s\n",f.s);
 f.dtor(&f);

 return 0;
}
I'll get my coat!

Return to “C/C++”