1

I am attempting to have a gobal counter the can can used by any function in the program. Is this possible?

int* count;

main(){
   *count = 0;
}

void incrementCount(){
   ++*count;
}

int getCount(){
   return *count;
}

If not possible how can I do something similar?

richard bent
  • 13
  • 1
  • 3
  • regarding: `int* count;` this declares a pointer to `int` However, that pointer is never actually pointer to memory that the application owns, Given where it is declared, it will contain NULL. Any effort to modify a value at address 0 (NULL) will result in a seg fault event – user3629249 Nov 28 '19 at 04:08

5 Answers5

2

No need to take a pointer. Take a Variable like

int count;

If you are going to use it in a single file better to declare it static.

A much better approach would be using the getter and setter functions instead of updating global variable everywhere

The Philomath
  • 954
  • 9
  • 15
1

I am attempting to have a gobal counter the can can used by any function in the program. Is this possible?

Yes, even though it's not a good thing to use global variable.

All you need to do is to declare the function in global scope. You should also initialise it in main before any function call.

int count;

int main()
{
    count = 0;
    incrementCount();
    ...
}

There is no need to use pointer in your case. And it's wrong as well, because you have not allocate any memory for that pointer.

artm
  • 17,291
  • 6
  • 38
  • 54
  • Not strictly necessary to explicitly init as globals are always automatically initialised to 0. – kaylum Nov 27 '19 at 05:23
  • @kaylum - that's true. Anyway explicit initialisation is still better, as not all types have implicit initialisation. – artm Nov 27 '19 at 05:25
  • Are you thinking of C++? AFAIK, in C all globals without an initialiser are implicitly initialised to 0. That's because they are part of the BSS which is always loaded into zeroed memory. It's not a big point but just wanted to clarify that in case it causes any misunderstanding for future readers. – kaylum Nov 27 '19 at 05:31
  • 1
    @kaylum - you're right: https://stackoverflow.com/questions/2091499/why-are-global-and-static-variables-initialized-to-their-default-values Anyway I think explicit initialisation won't harm, and perhaps a good habit too. – artm Nov 27 '19 at 05:41
0

Firstly there are few issues in the shared code sample. Here

int* count; /* count is pointer i.e you need to make count to hold or point to some valid memory location */

count is of int pointer type. And here

 *count = 0; /* de-referencing uninitiated pointer */

you are trying to de-reference count which has not valid memory. It causes segmentation fault. Before de-referencing you need to allocate memory. for e.g

 count = malloc(sizeof(*count));
 /* error handling of malloc return value */
 *count = 0;

I am attempting to have a gobal counter the can can used by any function in the program ?

You can do the same task without using pointer, try using static if use having file scope. If possible avoid using global variable.

Achal
  • 11,821
  • 2
  • 15
  • 37
0

You can just declare the variable as integer (not pointer).
You can have a global getter and setter function. Also initialize the global variable before any function call, best will be in Main().

apb_developer
  • 321
  • 2
  • 9
0

first, you can't do

*count = 0;

if you don't have initialised your pointer, it means refrenced to a variable like:

count = &x;

Then, if you want that variable is shared and the value is always updated between methods you can simply declare the variable as "static",

#include <stdio.h>
static int count;

    main(){
       printf("%d",count);
    }

    void incrementCount(){
       ++count;
    }

    int getCount(){
       return count;
    }
Math Lover
  • 148
  • 10
  • the OPs posted code is tagged as C, not C++ So the statement: `cout` will cause a failure of the code to compile as the function: `cout` is not part of the c language – user3629249 Nov 28 '19 at 04:11