Thread: Pointer array problem

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    5

    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. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Use an array of writeable characters.
    Code:
    char array[] = "abcdefgh";
    And assign a character.
    Code:
    array[0] = '9';
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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';
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    5
    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. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote 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

    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    5
    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. #7
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    You can malloc some memory for the pointer and then copy the string.

    See quzah's first post in this thread - http://cboard.cprogramming.com/showthread.php?t=80156
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  8. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    5
    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. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote 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
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Jun 2006
    Posts
    5
    I got it. Thanks for your explanation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Dynamically Increasing Array of Integers
    By laserlight in forum C++ Programming
    Replies: 30
    Last Post: 07-04-2008, 07:27 AM
  2. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 06:04 AM
  3. Need to pass a pointer of a unknown sized 2d array
    By (Slith++) in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2007, 10:15 PM
  4. Returning pointer to array of pointers problem
    By jimzy in forum C Programming
    Replies: 15
    Last Post: 11-11-2006, 06:38 AM
  5. Newbie char pointer problem
    By larry in forum C++ Programming
    Replies: 4
    Last Post: 09-29-2001, 09:38 AM