1. Pointer array problem

Hi
Can someone teach me how to change the element in the pointer array?

If I want to convert the element "a" to "9". How can I do it?
I tried to do it as below but failed.

Code:
```char *array = "abcdefgh";

void function1 (char array[]){

array[0] = "9"; // of course this one doesn't work.
}

Ernie```

2. Use an array of writeable characters.
Code:
`char array[] = "abcdefgh";`
And assign a character.
Code:
`array[0] = '9';`

3. > char *array = "abcdefgh";
If you're going to modify it, you need an array.
char array [ ] = "abcdefgh";

> array[0] = "9";
array is an array of chars
array[0] is a single char
So the assignment should be for a single char, which would mean single quotes around your char
array[0] = '9';

4. I understood it work if the array is declared as like this. However, does there have any way so that I can also modify the pointer array like my code?

5. Originally Posted by ernielam1024
However, does there have any way so that I can also modify the pointer array like my code?
I'll need a lot more caffeine before I can make sense of that.

Let's see, you've been shown how to do it correctly, but you instead want to learn how to write broken code?

First I'd say figure out what a "pointer array" is. It's not what you posted.

http://c-faq.com/aryptr/index.html

6. Sorry. I mean if I really need to declare the array as a pointer and I need to use the function to convert the element of the array. Any other way that I can do? Like below...

Code:
```char *array = "abcdefgh";

void function1 (char array[]){

array[0] = '9'; // of course this one doesn't work.
}```

7. You can malloc some memory for the pointer and then copy the string.

8. If I tried list this, the program will be hanged during the run time.
Am I writing a correct code?

Code:
```char *array = "abcdefgh";

void function1 (char array[]){

char *temp = malloc(strlen(array)+1;
temp = array;
strcpy (temp[0], "9");

}```

9. Originally Posted by ernielam1024
Sorry. I mean if I really need to declare the array as a pointer and I need to use the function to convert the element of the array. Any other way that I can do?
It seems like you're asking how do I do B (because I think that's what I need to do to solve A), rather than just asking how do I solve A, but whatever.
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void foo(char *array)
{
array[0] = '9';
}

int main(void)
{
char *array; /* it's a pointer */
array = malloc(9); /* point it to writable memory */
if ( array )
{
strcpy(array, "abcdefgh"); /* write something there */
puts(array);
foo(array); /* ooh, aah */
puts(array);
free(array); /* clean up after yourself */
}
return 0;
}

/* my output
abcdefgh
9bcdefgh
*/```

10. If you have a pointer that you pass to a function, and inside the function you want to change what it points to, and have that change keep, you instead need a pointer to a pointer.

Just like you can't pass an integer, and have it keep its value change, the same goes for any other object. To make changes persist outside of the function you change them in, you need a pointer to that type. Arrays are a slightly special case, because they degrade a bit when you pass them to functions.
Code:
```void foo( char s[] ); /* this is the same as... */
void foo( char *s ) /* ...this */```
Arrays degrade to pointers to the type of the first element in the array. That means an array of characters, when passed to a function, degrades to a pointer to a char. You can change the contents of an array, but you cannot make the array "point" anywhere else. The same goes for passed pointers. You can change the contents of what the pointer points to, but you can't make the pointer point to something else. Unless...
Code:
```void foo( char **s )
{
*s = malloc( strlen( "bar" ) + 1 );
strcpy( *s, "bar" );
}

...

char *p = "point to whatever...";
...
foo( &p ); /* ...now will end up pointing elsewhere. */
...
char array[] = "some array of stuff...";
...
foo( &array ); /* legal call... ...won't point somewhere else */```
Arrays are not actually poitners, so you cannot make the array "point" somewhere else. Basically this will give you some Bad Things(TM).

Quzah.

11. I got it. Thanks for your explanation.