2

I've research for half a day and cannot figure out how to pass a simple char pointer, and modify the value in a function. Most of the solutions say to modify the function to accept a char ** parameter.

I have a function I cannot modify. I need to pass a char pointer to it because this function will give me a new calculated char value. I was told I can pass a pointer to a char array and it will work, but I am unsure how to do it.

Passing char pointer as argument to function

I followed the above post and came up with the following code but it still does not work. When I pass the pointer to the char arr[] it does not change its value. My goal is to pass a char pointer and be able to write to it in another function by passing its reference. Any help is appreciated.

enum STATUS {
    OK = 0,
    BAD = 1,
};

STATUS func1(char *pData)
{
    pData = "Hello World";

    cout << pData << endl;

    return OK;
}

int main()
{
    STATUS ret;
    char arr[] = "Example String";
    char* pArray = &arr[0];

    ret = func1(pArray);

    cout << arr << endl;
    cout << pArray << endl;


    getchar();

    return 0;
}
JC203
  • 384
  • 7
  • 18
  • If you're using C++, try and actually use C++. That is use `const string` and not `char*`. – tadman Apr 06 '18 at 18:02
  • The function func1 is already made by someone else, I cannot modify it. If I declare my variable as const string I will need to modify the func1() – JC203 Apr 06 '18 at 18:05
  • 3
    Oh, is this one of *those* C++ courses that completely misses the point? Best of luck, and I hope you do well, but keep in mind this is far from what C++ is actually about. – tadman Apr 06 '18 at 18:06

3 Answers3

1

I have a function I cannot modify. I need to pass a char pointer to it because this function will give me a new calculated char value. I was told I can pass a pointer to a char array and it will work, but I am unsure how to do it.

That is not true. Whoever said that is lying to you or they are ignorant of the subject matter.


When you use

pData = "Hello World";

you are modifying where pData points to. However, that change is local to the function. It does not change the data in the calling function. You can use std::strcpy for the change to affect the calling function.

std::strcpy(pData, "Hello World");

You can pass the pointer by reference and change the value of the pointer. That will make the change visible to the calling function.

STATUS func1(char*& pData)
{
    pData = "Hello World";

    cout << pData << endl;

    return OK;
}

However, when you do that, arr and pArray in main will be different. arr will continue to have the same value while pArray will point to a completely different value.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
1
 STATUS func1(char *pData)
 {
     pData = "Hello World";
     cout << pData << endl;
     return OK;
 }

That function is doing a shallow copy (ie. changing the value of the pointer pData and not what it points to). You need to do a deep copy of the new string into the memory pointed to by the pData pointer.

The traditional way to do a deep copy of a string is to use the strcpy() library call. You could also use a loop.

Stephen M. Webb
  • 1,705
  • 11
  • 18
  • Let us forget what func1(char *pData) is doing, but rather that this function wants a char pointer. I am trying to figure out how to pass a char pointer to func1 without modifying any of its content. – JC203 Apr 06 '18 at 18:12
  • In that case, you're passing it correctly but your test is wrong. It fails with a type II error. – Stephen M. Webb Apr 06 '18 at 18:15
1
STATUS func1(char *pData)
{
  *(pData) = "H";
  *(pData+1) = "e";
  *(pData+2) = "l";
  *(pData+3) = "l";
  //... etc ...

  cout << pData << endl;

  return OK;
}

This is changing the memory the pointer points to, not the pointer itself. It can be done better using the functions in the other answers but this is more explicit. If you're using an IDE that allows single step debugging and memory viewing, you can watch the individual bytes in memory change.

This might lead to pointer over runs if the initial memory buffer isn’t big enough.

Also worth mentioning, is your memory big enough for the function you cannot change? Should you allocate memory like this:

char *ptr = new char[1000];
//etc....
if(ptr) delete ptr;

To create (and delete) a bigger, uninitialised memory buffer?

Pam
  • 1,146
  • 1
  • 14
  • 18