Setting the value of a pointer from function

I declared a pointer in main with value 0, so I want to change its value so that it points to other variable from a function, I guess the function creates a copy of my pointer that's why whatever I do within function doesn't change the real direction of the pointer in main.
I've been trying something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>

void redirectionate(char *str, char *ptrCopy);

int main()
{
    char str[] = "ThiS Is";
    char *ptrcopy = 0;

    redirectionate(str, ptrcopy);
    printf("%p\t%p\n", str, ptrcopy);
    return 0;
}

void redirectionate(char *str, char *ptrcopy)
{
    ptrcopy = str;
}

And the output is:

0xbfe83f24	(nil)


So, please let me know if there's a way to do this.
Thanks.
closed account (Dy7SLyTq)
look up string.h and strcpy
Uhh, line 11 should be:

printf("%s\t%s\n", str, ptrcopy);

And output should be:

ThiS Is             ThiS Is
closed account (Dy7SLyTq)
I didnt even notice that. thats why its printing out what you had before; it prints out the address of the pointer
maiko wrote:
I guess the function creates a copy of my pointer

Exactly.
Last edited on
You can try by having a reference-to-pointer o pointer-to-pointer input.
Ok thank you all and especially to EssGeEich, I achieved having the pointer declared in main by passing it as a reference to pointer.

void redirectionate(char *str, char *&ptrcopy);

And the output shows the following:


0xbfb8be44	0xbfb8be44


The same memory address xD thanks
Last edited on
You're Welcome ^^
Topic archived. No new replies allowed.