11

Okay so I am trying to pass a char pointer to another function. I can do this with an array of a char but cannot do with a char pointer. Problem is I don't know the size of it so I cannot declare anything about the size within the main() function.

#include <stdio.h>

void ptrch ( char * point) {
    point = "asd";
}

int main() {
    char * point;
    ptrch(point);
    printf("%s\n", point);
    return 0;
}

This does not work however, these two works:

1)

#include <stdio.h>

int main() {
    char * point;
    point = "asd";
    printf("%s\n", point);
    return 0;
}

2)

#include <stdio.h>
#include <string.h>

void ptrch ( char * point) {
    strcpy(point, "asd");
}

int main() {
    char point[10];
    ptrch(point);
    printf("%s\n", point);
    return 0;
}

So I am trying to understand the reason and a possible solution for my problem

5 Answers 5

22

This should work since pointer to the char pointer is passed. Therefore any changes to the pointer will be seen outside thereafter.

void ptrch ( char ** point) {
    *point = "asd";
}

int main() {
    char * point;
    ptrch(&point);
    printf("%s\n", point);
    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I would have prefered to return the pointer rather than another level of indirection, but +1 nonetheless
why it works with integer and doesn't need to pass pointer to pointer? i think because after dereference, char pointer remain is the address of first element of array?
12
void ptrch ( char * point) {
    point = "asd";
}

Your pointer is passed by value, and this code copies, then overwrites the copy. So the original pointer is untouched.

P.S. Point to be noted that when you do point = "blah" you are creating a string literal, and any attempt to modify is Undefined behaviour, so it should really be const char *

To Fix - pass a pointer to a pointer as @Hassan TM does, or return the pointer as below.

const char *ptrch () {
    return "asd";
}

...
const char* point = ptrch();

4 Comments

Which way is less expensive?
@SarpKaya both are blazingly fast (difference would be minimal if any), but I would prefer the "return the pointer" method for readability.
This will trigger a warning of local variables if you do not hardcode the return value as a string literal.
why we need const here?
3

Here:

int main() { char * point; ptrch(point);

You're passing point by value. Then, ptrch sets its own local copy of point to point to "asd", leaving the point in main untouched.

A solution would be to pass a pointer to main's point:

void ptrch(char **pp) { *pp = "asd"; return; }

Comments

1

If you change the value of the pointer in a function, it will remain changed only in that one function call. Don't mess your head with pointers and try:

void func(int i){
  i=5;
}
int main(){
  int i=0;
  func(i);
  printf("%d\n",i);
  return 0;
}

The same with your pointer. You do not change the address it points to.

If you assign to a variable passed by value, the variable outside the function will remain unchanged. You could pass it by a pointer (to pointer) and change it by dereferrencing it and it's the same with an int - in this case, it doesn't matter if the type is int or char * .

Comments

-1

first declare funtion......like this

 #include<stdio.h>
 void function_call(char s)

next write main code.....

void main()
{
    void (*pnt)(char);  //  pointer function declaration....
    pnt=&function_call;  //    assign address of function
    (*pnt)('b');   //   call funtion....
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.