Hello,
I am trying to figure out why pointer address is not preserved over a function call and how to fix it. For example:

void test_function(char **,char *);

int main() {
        char **argv = calloc(64,sizeof(char *));
        char *line = "MAMMAJAMMA!";

        test_function(argv,line);
        printf("argv[0] == %s",argv[0]);
        return 0;
}

void test_function(char **argv,char *line) {
/* pointer value not kept with this why? */       
       *argv = line;

/* I know this works , but want way with pointers only if possible */ 
         *argv = malloc(20);
         strcpy(*argv,line); */
}

Recommended Answers

All 5 Replies

Perhaps you could explain what you expect your code to do and what it's doing that fails to meet your expectation.

why doesn't argv[0] point to "MAMMAJAMMA" in main function after assigning *argv = line within the test_function? After test_function call its just null when I want it to point to string in main without having to store extra memory.

What compiler are you using?

ahh.. ok yea it was compiler, thanks for pointing in right direction

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.