2

I have a question regarding pointers. When I iterate through a char array using pointer to char array in function, original array stays the same, but when I do it in main function, I can't print char array.

I am new to pointers.

void f(char* a)
{
    while (*a!=0) {
    *(a++); // going through array
    }
}
int main()
{
    char* a1 = "test";
    f(a1);
    cout <<a1<<endl; // I can normally print out "test"
    return 0;
}

But,

int main()
{
    char* a1 = "test";
    while (*a1!=0) {
    *(a1++);
    }
    cout <<a1<<endl; // won't print anything
    return 0;
}

So my question is, even though I am passing pointer to function, why is original array not modified?

2
  • 2
    Same reason an int isn't modified when you pass a copy of it to a function. Commented Sep 6, 2013 at 18:30
  • here you Need of Pointer to Pointer Commented Sep 7, 2013 at 7:52

8 Answers 8

5

Since you're incrementing the pointer a1, it points to the character '\0' at the end of the loop in main. Printing a null character prints nothing (empty string).

When you do this with a function, the pointer a is local is local to the function. It is modified within the function but is not changed in main. If you wanted similar results between the two examples, you would have to change the function to accept a reference parameter to the pointer:

void f(char * &a) { . . . }
Sign up to request clarification or add additional context in comments.

Comments

3

f(a1);

won't modify your a1 after f exits

However

while (*a1!=0) {
    *(a1++);
    }

before cout

will make a1 to point to null character, so nothing gets printed

1 Comment

In this case, yes. But if f takes its parameter by reference, then it would be different...
3

you're not changing a1 when you call your function. you're only changeing a copy of a1 that is passed into that function: a

But when you call a1++ in your main function, you are changing a1 and when it's finished, a1 will be 0 or '\0'

Comments

2

The difference is that in second case,a1 is pointing to \0 character after the loop exists. To see the same result in case of 1, receive pointer by reference.

void f(char* & a); // Valid in C++ only

1 Comment

@user2648841: but it won't be the "same"; you would have to explicitly take the address before passing, and explicitly dereference it before using it in the function.
2

The array a1 exists while the function main() is still active.
The address of a1 is passed to the function.
Such address is reserved to a1 because main() is still active.
Now, the variable a is a pointer whose initial value is a copy of a1, in the moment you do the call f(a1).

Take in account that a is a variable, and behaves like int, char, float, etc. So, if you modify its "value" (that is, in this case, the memory address that a points to), the original address of a1 keeps untouched.

However, if you modify a member of a inside f():

  a[0] = '!';

then the original array becomes: a1 equals to "!est".
The reason is that you are changing the content of the character held in the address of a (plus 0), which is (at the very start of execution of f()) the content of the address of a1 (plus 0).

Comments

1

Read more about pointers here

To be able to print the string in your second case, do this:

#include <iostream>
using std::cout;
using std::endl;

int main()
{
    char* a1 = "test";
    char *ptr = a1; //create another pointer to a1
    while (*ptr != 0) cout << *ptr++;
    cout << endl << a1 << endl;
    //You should get an output of the same string on 2 lines
    return 0;
}

Comments

1

The formal parameter a in f is a different object in memory from the actual parameter a1 in main, so changing the value of a does not affect a1.

To mimic the behavior of the second snippet, you would have to pass a pointer to a1, like so:

void f( char **a )
{
  while ( **a != 0 )
    (*a)++;  // increment the thing a points to
}

int main( void )
{
  char *a1 = "test";
  f( &a1 );  // pass a pointer to a1
  cout << a1 << endl;
  return 0;
}

In this code, we don't pass the value stored in a1 to f, we pass the address of a1. In f, we don't write to a, we write to *a, or what a points to.

Comments

0

You are using pass-by-value. (Everything in C++ is pass-by-value unless the function parameter is a reference type, i.e. has &, in which case it would be pass-by-reference.)

In pass-by-value, assignments to a parameter (and ++ is an assignment) do not have any effect on things outside the function.

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.