0

Pointer s doesn't get any value. I can't figure out why.
Variable v in exposeStatic() always get desired values.
Please advise.

main.c

#include <stdio.h>
#include <stdlib.h>
#include "myLib.h"

struct node *s = NULL;

int main() 
{
    populateStatic();
    exposeStatic(s);
    printf("myLib myStatic adress: %p\n", s);
    return(0);
}

myLib.h

#ifndef myLib
#define myLib

struct node
{
    int data;
    struct node *next;
};

void exposeStatic(struct node *v);
void populateStatic();

#endif

myLib.c

#include <stdio.h>
#include <stdlib.h>
#include "myLib.h"

static struct node *myStatic = NULL;

void exposeStatic(struct node *v)
{
    v = myStatic;
}

void populateStatic()
{
    struct node *p = (struct node *)malloc(sizeof(struct node));
    p->data = 855; // assume there is some data from populateStatic() arguments
    p->next = NULL; // assume there is some data from populateStatic() arguments
    myStatic = p;
}
3

1 Answer 1

3

You're overwriting the local copy of the variable. Instead, pass a pointer to a pointer:

    exposeStatic(&s);

and modify your function accordingly:

void exposeStatic(struct node **v)
{
    *v = myStatic;
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.