0

is there any way to move pointer, which is initialized in main() function, to first executable function and have it accessible in whole program?

Here's the code:

main function, where is pointer d initialized:

void main(){
    int x;
    deque *d;
    d=(deque*)malloc(sizeof(deque));
    initDeque(d);

and I want to move the pointer into function called initDeque()

void initDeque(deque *d){ //Create new deque
    d->front=NULL;
    d->rear=NULL;
}

Is it possible to move it?

  • why cant you just move the malloc to initDeque() function? – Milind Dumbare Feb 25 '15 at 12:23
  • 2
    The pointer is *copied*, but the copy in `initDequeu` points to the same memory as `d` in the `main` function. So it should work well as written currently. Do you mean that you want to have the declaration of the pointer in the `initDeque` function? Then just have the function *return* the pointer. Also, [don't cast the result of `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Some programmer dude Feb 25 '15 at 12:24
  • and don't use sizeof(type) inside malloc, but sizeof object. `d = malloc(sizeof *d);` – William Pursell Feb 25 '15 at 13:04

2 Answers2

1

If by "move the pointer" you mean that you want to move the variable declaration, then you can do that but it will become a local variable only accesible inside that function. Clearly not what you want.

You need to make it global, that will make it accessible from all scopes. Note that global variables are considered ugly and increase the risk of errors and generally make the code less clear.

With a global pointer it would look like this:

deque *d;

void initDeque(void)
{
  d = malloc(sizeof *d);
  d->front = d->rear = NULL;
}

Note that you shouldn't cast the return value of malloc() in C.

Also note that nobody would have a global variable named using a single lower-case letter. It's way to easy to confuse it with a local variable, so you should at least make the name more obvious, e.g. something like

deque *theDeque;
Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
  • Yeah, I've done similar to what you described above. `deque* initDeque(){ //Create new deque deque* d = (deque*)malloc(sizeof(deque)); d->front=NULL; d->rear=NULL; return d; } ` and in `main` i've done this: `deque *d = initDeque();` . Is there any easier way, than creating global variable? – Rimantas Radžiūnas Feb 25 '15 at 14:33
  • If you want to access in whole program, why a global is disturbing you ? – Ôrel Feb 25 '15 at 14:44
  • @AurélienLAJOIE Because it's risky. Any code anywhere can do something "bad" with the global, there is no encapsulation. – unwind Feb 25 '15 at 14:45
  • @unwind _G is only visible into the file, not anywhere – Ôrel Feb 25 '15 at 18:18
1

Create a static structure to store all you want share. Create a function to allocate your deque. I don't know the size of deque perhaps you can take it on the stack instead of the heap.

static struct {
   deque d*;
} _G;

void main(){
    int x;
    _G.d = deque_new();
}

deque *deque_new(void){
    deque *d;

    d = malloc(sizeof(deque));
    d->front=NULL;
    d->rear=NULL;
    return d;
}

or with the stack

static struct {
   deque d;
} _G;

void main(){
    int x;
    deque_init(&_G.d);
}

deque *deque_init(deque *d){
    memset(d, 0, sizeof(*deque)); 
    return d;
}
Ôrel
  • 7,044
  • 3
  • 27
  • 46