Like Tree3Likes
  • 1 Post By anduril462
  • 2 Post By anduril462

Pass array of char pointers by reference

This is a discussion on Pass array of char pointers by reference within the C Programming forums, part of the General Programming Boards category; Hi, I am building a program with an index of strings (char pointers). So far I have created an array ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    19

    Pass array of char pointers by reference

    Hi,
    I am building a program with an index of strings (char pointers). So far I have created an array 10 char pointers using

    Code:
    char *index[10];
    int i;
            for(i=0; i<10; i++)
            {
                    index[i] = malloc(20);
            }
    I know that to pass an array of char pointers by value you would do
    Code:
    char *str[]
    , but how would you do it by reference?
    Code:
    char **str[]
    in a function definition, and using the & operator with the array of char pointers doesn't seem to work. I know passing by value works fine but that wouldn't be desireable with large amounts of data.

    Thanks
    Joshun

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Code:
    malloc(20)
    this is to cold :P
    I would write it like this
    Code:
    malloc(sizeof(char) * 20);
    on the other hand, if a pointer to the first element of the array is what is actually passed as a parameter, so don't worry about the size of the array.
    On other words, no matter if the size of the array is BIG or small, a pointer is going to be passed as parameter.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,451
    Reading this may help a bit: Arrays and Pointers

    Arrays decay to pointers to the first element, when passed to a function (or when you use the name of the array all by itself, with no [ ]). Thus, as std10093 stated, you are not passing the array by value. It is closer to pass by reference in this regard (though that is not exactly accurate either, IMO).
    laserlight likes this.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,056
    Quote Originally Posted by Joshun View Post
    I know that to pass an array of char pointers by value you would do
    Code:
    char *str[]
    , but how would you do it by reference?
    Code:
    char **str[]
    in a function definition, and using the & operator with the array of char pointers doesn't seem to work.
    In C there is no such thing as "pass by reference". To simulate pass by reference, a pointer is passed. Additionally, when you pass an array in C, the name of the array acts as a pointer to the start of the array. Strings are just a special array where there's a gentleman's agreement that the final element shall be '\0'.

    To understand, put your example into the context of a compilable program:

    Code:
    void fill_strings(char *str[], int nstrings)
    {
        for (int i=0; i < nstrings; i++)
            strcpy(str[i], "hello world");
    }
    
    void print_strings(char *str[], int nstrings)
    {
        for (int i=0; i < nstrings; i++)
            printf("string %d: %s\n", i, str[i]);
    }
    
    int main(void)
    {
        char *index[10];
        int i;
        for(i=0; i<10; i++)
        {
            index[i] = malloc(20);
        }
        fill_strings(index, 10);
        print_strings(index, 10);
        
        return 0;
    }
    When you call fill_strings and print_strings, the parameter index just is an address of where this array happens to be stored in your memory. String copying only takes place once inside the actual function, not in the parameter passing.

    1. By the way, you can write "char **str" in the parameter list for the same effect.
    2. malloc(20) is fine

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    19
    Thanks for the replies std10093, anduril462 and c99tutorial, I finally get what you mean now - all the char pointers point to the same data. I used
    Code:
    malloc(20)
    because a char is always guaranteed to be 1 byte, if it was a different type such as int I would have used sizeof() to return the implementation's size.

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    I am aware about that. But, in my way you don't have to think much, you just follow the same procedure for every data type

    It's only a suggestion.

    // As I and the c99 said, what you have there is correct
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,451
    If you want even less thinking, then follow this method for allocating contiguous objects (like arrays):
    Code:
    some_type *foo;
    
    foo = malloc(N * sizeof(*foo));
    That way, you only need to know the name of the variable you are using to store the result in, and the number of contiguous things you want. No need to remember even the type of the variable. Furthermore, if you change the type of foo, you don't need to change the malloc call.
    Click_here and AndiPersti like this.

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,343
    And you are checking to see if every malloc was successful and freeing the memory at the end, right?...

    Code:
    int main(void)
    {
        char *index[10];
        int i;
        for(i=0; i<10; i++)
        {
            if ((index[i] = malloc(20 * sizeof(**index))) == NULL)
            {
                break;
            }
        }
    
    
        fill_strings(index, 10);
        print_strings(index, 10);
    
    
        while (i > 0)
        {
            free(index[--i]);
        }
    
    
        return EXIT_SUCCESS;
    }
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused with pointers and pass by reference
    By 50tonrobot in forum C Programming
    Replies: 16
    Last Post: 07-12-2012, 12:05 AM
  2. how can i pass 2d and 3d array by reference?
    By johnny8888 in forum C++ Programming
    Replies: 9
    Last Post: 10-23-2011, 10:04 AM
  3. how to pass array by reference
    By dezz101 in forum C Programming
    Replies: 13
    Last Post: 09-19-2010, 09:28 AM
  4. Pass by Reference with a pointer to char
    By JoshNCSU22 in forum C Programming
    Replies: 16
    Last Post: 10-10-2008, 11:28 AM
  5. How to pass an array of strings by reference?`
    By JamesMI in forum C++ Programming
    Replies: 17
    Last Post: 01-25-2003, 07:23 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21