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?

egrunin
  • 24,650
  • 8
  • 50
  • 93
user2648841
  • 247
  • 1
  • 4
  • 9

8 Answers8

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) { . . . }
E. Moffat
  • 3,165
  • 1
  • 21
  • 34
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

P0W
  • 46,614
  • 9
  • 72
  • 119
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'

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
Mahesh
  • 34,573
  • 20
  • 89
  • 115
  • @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. – newacct Sep 07 '13 at 00:24
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).

pablo1977
  • 4,281
  • 1
  • 15
  • 41
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;
}
smac89
  • 39,374
  • 15
  • 132
  • 179
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.

John Bode
  • 119,563
  • 19
  • 122
  • 198
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.

newacct
  • 119,665
  • 29
  • 163
  • 224