Hello all prorgammers out there.
While just fiddling with the pointer concept of mine which is weak i wrote a very simple function to just give the addr of the array to the char type pointer so that i can refer the array using the pointer instead of the array name.

void pointTo(char* src, char* dst);
 
int main() {

 char text[3] = {0}; // create a array with all elements 0
 char* ptr = 0;        // the char pointer which should refer the array
 
 printf("\nThe addr in text is %p", text); // for debug
 printf("\nThe addr in ptr is %p", ptr); // for debug
 
// here we pass the char pointer as first argument and the array 
// pointer ie the addr pointed by array as second arg.
 
 pointTo(ptr, text);  // actual funtion call
 
 printf("\nThe addr in text after function is %p", text);
 printf("\nThe addr in ptr after function is %p", ptr);

 ptr[0] = 'A';  // change using pointer instead of array name
 ptr[1] = '\0';
 
 printf("%s",text);  // print the output
 
 return 0;
}
 
// attempts to asssign the addr in destination to the addr in 
// the source so that we can manipulate the array
 
 
void pointTo(char* src, char* dst) {
 src = dst; 
}

But this code jumps out as soon as i execute it.

My pointer concepts are a bit weak so can you point me to links which explain them in full depth and all that is required to tame them.

Recommended Answers

All 4 Replies

If you want to change a value in a called function, you need to pass a pointer to the value. If what you want to change is a pointer, you need to pass a pointer to the pointer.

If you want to change a value in a called function, you need to pass a pointer to the value. If what you want to change is a pointer, you need to pass a pointer to the pointer.

Hmm i think i am getting a bit of wat you are trying to say.
So i think i need to redefine the function as void pointTo (char** src, char* dest) and should call the fucntion as pointTo (ptr, text); Is this thing right?
It would we very kind of you to tell me some links where you got your pointer concepts cleared from and which explains pointers from ground up to the professional level.

Call the function as

pointTo (&ptr, text);

Is it because the function accepts double pointer ie ptr to the ptr and hence to make the ptr "scr" the link to the modifiable char pointer "ptr" we are passing the address of the ptr variable.

If not then can you please explain your logic regardin the above change.

And btw i have checked the links and they are real good with a pdf file of 53 pages just on pointer introduction. Thanks a lot. You rock man.

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.