C :: Use Pointers Instead Of Global Variables?

Oct 15, 2014

I have made an application and I have basically solved everything. But the only problem is that I am using global variables because it felt like the smoothest, so my program is built on it.

But now I've read around and I understand that you should not use these(?). Do you think pointers is the best think to use instead?I have previously declared my board array and some variables as global and I want them in alot of functions.I have read and understand the procedure for the use of pointers so I can use my int's in the other functions by doing like this? Code: #include <stdio.h>

int justprint();
int main()
{
int Row = 2;
int Column = 2;
int *pRow = &Row;
int *pColumn = &Column;
[code]...

But how do I do it with an array like this one? If I declare it in the main function, and then want to use it in other functions.Or are there better, easier solutions?

Code: char game[3][3]={{0,0}};

View 13 Replies


ADVERTISEMENT

C++ :: Access EXE Global Variables From DLL

Jun 25, 2013

On linux, I can compile DLLs (shared objects) as well as executables that use them. I can even access globals and classes that are defined in the EXE from the DLL, and vice versa, simply with the 'export' keyword. flawlessly.

The Problem: But on Windows (using MinGW), no matter what I do, I'm completely unable to access global variables which defined in the EXE, from the DLL. On Linux, this is no sweat, but what's Windows' problem?

I also need to extend classes in the dll with base class method definitions defined in the exe.

Ive heard that on Windows, you need to use declspec(dllimport) and declspec(dllexport). I can compile with CygWin+MinGW/g++4.5.3 as well as "Pure Windows" with MinGW/g++4.7.2 *without* the declspecs. So what's the decljunk for? Is this really just something for MSVC or other compilers?

Here's some Windows code to show what the problem is. The DLL's global variable is accessible to the EXE just fine, but the EXE's global variable is not accessible to the DLL - compilation complains it is an undefined reference.

main.cpp
#include "myLib.h"
#include <stdio.h>
int exe;

[Code].....

edit: I tried using --enable-runtime-pseudo-reloc --allow-shlib-undefined options when compiling the DLL and G++ complains that --allow-shlib-undefined is an unrecognized option.

View 1 Replies View Related

C++ :: Setup Global Variables

Feb 23, 2015

I am using VS2010 to develop an app which includes several windows forms that I am trying to set up global variables for, and I am getting a few errors like:

LNK2005: "wchar_t *dsn"...already defined in ....obj

I have a header file (externals.h) with:
#ifndef MY_GLOBALS_H
#define MY_GLOBALS_H
extern long dbg;
extern wchar_t dsn[50];
extern wchar_t u[30];
extern wchar_t p[30];
#endif

and 2 different forms, each with different namespaces, but both including the above header (#include "externals.h").One of the form .h files defines the values for these externally declared variables like this: namespace PWValidationTools{

wchar_t dsn[50] =_T("MDOTProjectWise");
wchar_t u[30]=_T("api_admin");
wchar_t p[30]=_T("proce55");
long dbg = 1;

public ref class ValidationSetupForm : public System::Windows::Forms::Form {
}

The other form file only uses these variables, never defines them.I am getting the above LNK2005 error only for the variables declared as wchar_t, not the "long" one. why I'm getting the link errors only for the wchar_t variables.

View 1 Replies View Related

C++ :: Global Variables For Multiple CPP Files

Jan 19, 2014

I am trying to get variables that are global to multiple files. I have mananged to make constant variables that are global but maybe not in the best way. In the header i have the constant variables being defined:

const int variable_Name = 5;

And the cpp file:

#include <iostream>
using namespace std;
#include "vars.h"
int main ( ) {
cout << variable_Name<< endl;
system ("pause");
return 0;
}

Is there a better way to do this and to make the variables able to be changed within the cpp files.

View 7 Replies View Related

C++ :: Using Global Variables In Static Library

Jul 22, 2014

Is it possible to use & change global variables in a Static Library? For example:

I declare a
bool test = true;
globally.

Then later in an exported function If the user wants, he can set that test to false. So the program later when checks test if it's true, will notice that it's not true, since one of my function changed it.

Is it right?

View 3 Replies View Related

C++ :: Creating Global Variable / Pointers?

Jan 6, 2012

I am making two classes using the juce library [URL] ....

this is a problem that has now come up after solving an earlier problem i posted but its a completely seperate issue.

i have a class for loading a file when i button is clicked that seems to work. Here is the function in my LoadButton cpp:

void LoadButton::buttonClicked(Button* button)
{
FileChooser pedoFileChooser ("Choose an Audio File", File::nonexistent, String::empty, true);
if (pedoFileChooser.browseForFileToOpen())
{
File pedoFile (pedoFileChooser.getResult());
}
}

I want to be able to then use the file stored in pedoFile in the cpp of another class called PlayButton. I tried doing this with a pointer? not sure if that's correct way of doing it (i know very little about C++ or programming) by changing the function to this. I'm getting the error invalid initialisation of non-const reference of type 'juce::File*&' from a temporary of type 'juce::File'

void LoadButton::buttonClicked(Button* button)
{
FileChooser pedoFileChooser ("Choose an Audio File", File::nonexistent, String::empty, true);
if (pedoFileChooser.browseForFileToOpen())
{
File* &pedoFile = (pedoFileChooser.getResult());
}
}

View 3 Replies View Related

C++ :: Passing Data Between Threads Without Using Global Variables

Nov 6, 2013

I have a main thread and a worker thread. How do i pass data between them when both are already running without using global variables?

View 1 Replies View Related

C/C++ :: Why Implicit Int Approach Taken With No Specifier Only For Global Variables

Sep 22, 2013

I realize that implicit int rule was removed in C99 but gcc still takes this approach by default. I wonder why this happens:

bbb = 5; // legal  
int main(void) {
  aaa = 10; // illegal
  auto aaa = 10 // legal

Inside function a specifier is needed. Error message with no specifier used is:

error: ‘aaa’ undeclared (first use in this function)

Is this because of linkage - bbb variable has an external linkage so compiler knows that we are defining a variable here while inside mean() we need to show compiler that aaa is defined right here, it does not come from external functions?

View 4 Replies View Related

Visual C++ :: Static Variables Local Or Global?

Mar 10, 2014

I came across the following code today and I was a bit surprised that it worked:-

Code:
std::string func_A () {
static std::string x;
if (!x.empty())
return x;

[Code] ....

I've simplified things slightly - but the basic point is that both functions are in the same source file and they both have a static std::string called 'x'. Being static, I guess they aren't (strictly) local variables. So how does the compiler know that they're different entities? Does it encode their signatures using the function name or something like that? If I call each function separately I do seem to get the correct string...

View 5 Replies View Related

C :: Compiling Sudoku Program - Declare Constant Instances As Global Variables

Sep 18, 2013

I am trying to compile a c program for sudoku. I have declare const instances as global variables, but when i try to compile the code it says that my declarations are not constant, here is some of the code.

#include <stdio.h>
#include <assert.h>

const int GRIDSIZE = 3;
const int GRID_SQUARED = GRIDSIZE * GRIDSIZE; //this line
const int ALL_VALUES = (1<<GRID_SQUARED)-1; //and this give//the error
int board [GRID_SQUARED][GRID_SQUARED];

View 3 Replies View Related

C++ :: Using Pointers Instead Of Reference Variables In Function

Mar 26, 2013

The following function uses reference variables as parameters. Rewrite the function so it uses pointers instead of reference variables, and then demonstrate the function in a complete program.

int doSomething(int &x, int &y)
{
int temp =x;
x = y * 10;
y = temp * 10;
return x + y;
}

I understand how to covert the reference variables to pointers, however I am stuck on this error. Either I get the error listed in the title or (with a few changes) the error "invalid conversion from 'int' to 'int*'"

What am I doing incorrectly?

#include <iostream>
using namespace std;

int doSomething(int*, int*);

int main(){
int X, Y, result;

[Code] ....

I have multiplied both x and y by 10 and then added them together!

Here is the result " //I really didn't know how else to use the "doSomething" function in a meaningful way. So... I just stated what the function does.

<< result << ".
";
system("PAUSE");
return 0;
}
int doSomthing(int *x, int *y)

[Code] .....

View 1 Replies View Related

C++ :: Declaring Pointers Vs Regular Variables?

Aug 9, 2013

I really do not see the difference between these two declarations:

int myvariable;
int * mypointer;

It is said that when you define a pointer, instead of containing actual data, it contains a pointer to the memory location where information can be found.

But doesn't the other variable declaration do the same? It obviously doesn't have data either. And it must be stored in a memory location as well. So I do not see the difference.

View 6 Replies View Related

C :: Local Variables - Swap Char Pointers

Apr 23, 2013

I have the following code. According to this the values of pointers p[0] and p[1] remains unchanged since the swap is made to local variables in swap function.Now my doubt is how can I swap the pointers p[0] and p[1] inside the function swap??

Code:

#include<stdio.h>int main(){char*p[2]={"hello","good morning"};
swap(p[0],p[1]);
printf("%s %s",p[0],p[1]);return0;
}void swap(char*a,char*b){char*t; t=a; a=b; b=t;
}

View 5 Replies View Related

C++ :: Returning Values Or Using Pointers To Redefine Variables Passed As Arguments?

Feb 28, 2014

Which is more efficient in functions? Returning values or using pointers to redefine variables passed as arguments?

I mean either using:

void ptr_Func(int *x)
{
*x = *x+1
}

or

int ptr_Func(int x)
{
return x + 1;
}

In terms of speed, memory use etc.I want to know general efficiency, I know it will obviously vary with different uses and circumstances.

View 7 Replies View Related

C :: Create Array Of Pointers To Pointers Which Will Point To Array Of Pointers

Feb 28, 2014

I'm trying to create an array of pointers to pointers which will point to array of pointers (to strings) I tried

Code:

int i;
char *string[]={
"my name is dave",
"we like to dance together",
"sunny day",
"hello",

[code]...

the app keeps crashing , I don't know how to make the array-elements to point to another array-elements..

View 4 Replies View Related

C++ :: Comparing Char Pointers To Integer Pointers

May 21, 2013

I am a little confused while comparing char pointers to integer pointers. Here is the problem:

Consider the following statement;
char *ptr = "Hello";
char cArr[] = "Hello";

When I do cout << ptr; it prints Hello, same is the case with the statement
cout << cArr;

As ptr and cArr are pointers, they should print addresses rather than contents, but if I have an interger array i.e.
int iArr[] = {1, 2, 3};

If I cout << iArr; it displays the expected result(i.e. prints address) but pointers to character array while outputting doesn't show the address but shows the contents, Why??

View 2 Replies View Related

C++ :: How To Create A Global Object

Jan 18, 2013

if (choice ==1){
Light *myobj = new Light();
}
FlipUpCommand switchUp(*myobj);

Error: `myobj' undeclared (first use this function)

How to solve this problem without changing the Light class.

View 4 Replies View Related

C# :: Setting Global Hot Key On Second Form?

Jun 23, 2014

I created a WinForm app in C# using VS 2013 Express.

I added code to create a Global Hot Key on the main form. This works fine. My hot key is Ctrl-T. I can press the hot key and make the main form show and hide.

Then I created a second form (ChecklistForm) and now I want to press ctrl-T and make that form show and hide. I do not need the main form to do this any more. I just used the main form to test my Global Hot Key code.

So I'm having trouble getting the second form to respond to the hot key. When I put a break on the WndProc(), there is no break.

namespace ChecklistFSX {
public partial class MainForm : Form {
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(String sClassName, String AppName);
private IntPtr thisWindow;
private GlobalHotKeys hotkey;
public MainForm()

[code]....

View 2 Replies View Related

C/C++ :: Passing A Global Pointer?

Feb 29, 2012

Let's say I've got something like this:

struct Box{
//something
};
typedef struct Box* pBox;

[Code]....

function fun recieves the address(which is NULL) and then allocates the memory for the Box; Let's say I cannot return the address of new allocated p and I can't also use that pointer p(from main) without passing it into a function.

Q: How can I make it that I could operate in function "fun" as I operate on orginal pointer p(from main), right now I'm just passing the address to my function but I can't change the 'global' pointer p ;(.

I remember in pascal it's like:
"function(var pointer:[pointer to sth])"
and all is done.

View 14 Replies View Related

C++ :: How To Create Global Object

Jan 18, 2013

if (choice ==1){
Light *myobj = new Light();
}
FlipUpCommand switchUp(*myobj);

Error: `myobj' undeclared (first use this function)

How to solve this problem without changing the Light class.

View 9 Replies View Related

C++ :: Global Variable - Calling Sub Function

Dec 19, 2013

Expected output: 20

But what I got is: 22

Why. While calling sub function it should take the global variable am I right

insert Code:
#include <iostream>
using namespace std;
int a=0;
void sub()

[Code] ....

View 3 Replies View Related

C++ :: Updating Global Pointer In Tree

Jul 16, 2013

I am having trouble updating my global pointer in the following code,

Code:
#include <iostream>
using namespace std;

struct RB{
RB()=default;
RB(int clr):color(clr) { }
int color;

[Code] ....

The problem is, at line where I compar y==Tnil, It is evaluating to false at the first insert. But It should be true. again, after ending the function, T again becomes equal Tnil, as a result , none of the is being inserted.

View 2 Replies View Related

C :: Global Variable - Only Printf Works

Apr 20, 2013

Okay so I am programming an 8051 variant to interact with an SD card. In a separate initialization routine in SD_CARD.c I pull out the vital information of the card into global variables. I then call Menu() which is in another source file but includes a header file with all of the variables declared as extern. Now here is the weird, part this is from my Menu.c

printf("%u" , VOLUME_1_SECTOR);
if(VOLUME_1_SECTOR==16384)
printf("Correct");
else
printf("Incorrect");

Now the output of the first printf is 16384 but the conditional evaluates to false. If I put this code in SD_CARD.c (Where VOLUME_1_SECTOR is defined) the conditional evaluates to true. I am confused why the printf works correctly in Menu.c but not the conditional.

View 2 Replies View Related

C :: How To Scanf Global Variable Outside The Main

Sep 7, 2013

The problem that I want to make an array " vertextDegree [nbColours] " with " nbColours " elements in it ,but the "nbColours" unknown and I have to get it get it from a file .

Code:

int nbEdges,nbVetices, nbColours ;
typedef struct st_graphVertex {
int index;
int colour;
int val ;
int vertexDegree[nbColours]; // it won't work because nbColours unknown
// here and I want get it from file in the main
struct st_graphVertex *next;
t_edgeList *out;
}t_grapheVertex;

View 3 Replies View Related

C :: Declare Global Array And Then Use It In Few Functions

Jan 22, 2015

I want to declare a global array and then use it in a few functions. When I want to assign any number to that array I face with this error:

Code:
a.c:11:6: error: expected expression before ']' token
n[]={1,2,3,4,5};
^

The code is:
#include <stdio.h>
int n[5]; // I need to define the array here to be global
int main () {
n[]={1,2,3,4,5};

[Code] .....

View 6 Replies View Related

C++ :: Private Global Variable To Class?

Sep 30, 2014

I want to make a destructor counter...So it needs to be outside that specific instance of the class. And i want to keep it hidden. I don't want to be able to access it from outside the class...I Don't want main.cpp to have access to this variable

Is there a way to do this?

static int destructorCount;

View 8 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved