Thread: String array function problem

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    9

    String array function problem

    I've been banging my head against the wall trying to figure out why this doesn't work. Isn't strs a pointer to a pointer of char? Any help would be appreciated. Thanks.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define LIM 10
    #define MAX 81
    
    void print_arrs(const char **strs, int num);
    
    int main(void)
    {
            char strs[LIM][MAX];
    
            puts("Enter a string, blank to quit");
            while (cnt < LIM && (gets(strs[cnt]) && strs[cnt] != '\0')) {
                    cnt++;
                    puts("Enter a string, blank to quit");
            }
    
            print_arrs(strs, cnt);
    
            return 0;
    }
    
    void print_arrs(const char **strs, int num)
    {
            int i;
    
            for (i = 0; i < num; i++)
                    puts(strs[i]);
            return;
    }
    I get the following warning:
    tmp.c:33: warning: passing argument 1 of 'print_arrs' from incompatible pointer type

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by albundy
    Isn't strs a pointer to a pointer of char?
    When an array is passed as an argument, it is converted to a pointer to its first element. Therefore, an array of arrays is converted to a pointer to an array, not a pointer to a pointer, hence the "incompatible pointer type".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    See also: Question 6.18


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    While you're browsing the CLC FAQ, check this entry in particular
    Question 12.23
    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.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    9
    Thanks for the replies. I'm still caught up on this. In print_arrs, strs[i] is a pointer to char (the i'th string), correct? It seems I'm obviously wrong because swap_onetwo is complaining about incompatible types in assignment.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define LIM 10
    #define MAX 81
    
    void print_arrs(char (*strs)[MAX], int num);
    void swap_onetwo(char (*strs)[MAX], int num);
    
    int main(void)
    {
            char strs[LIM][MAX];
            int cnt = 0;
    
            puts("Enter a string, blank to quit");
            while ((gets(strs[cnt]) && strs[cnt][0] != '\0')) {
                    if (++cnt == 10)
                            break;
                    puts("Enter a string, blank to quit");
            }
    
            print_arrs(strs, cnt);
    
            return 0;
    }
    
    void swap_onetwo(char (*strs)[MAX], int num)
    {
            char *tmp;
    
            tmp = strs[0];
            strs[0] = strs[1];
            strs[1] = tmp;
    
            return;
    }
    
    void print_arrs(char (*strs)[MAX], int num)
    {
            int i;
    
            for (i = 0; i < num; i++)
                    puts(strs[i]);
            return;
    }
    tmp.c: In function 'swap_onetwo':
    tmp.c:46: error: incompatible types in assignment
    tmp.c:47: error: incompatible types in assignment


    And yes, I fully understand the ramifications of using gets()

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by albundy View Post
    In print_arrs, strs[i] is a pointer to char (the i'th string), correct?
    Code:
    void print_arrs(char (*strs)[MAX], int num);
    strs is a pointer to an array of characters.
    *strs is an array of characters.
    strs[ x ] is an array of characters.
    strs[ x ][ y ] is a single character.
    *strs[ 0 ] is a single character.

    That looks about right:
    Code:
    #include<stdio.h>
    int main( void )
    {
        char array[ 3 ][ 5 ] = { { 0 }, }; /* an array of arrays */
        char (*ap)[ 5 ] = array; /* a pointer to an array of five characters */
        char *p = NULL; /* a pointer to a character */
        char c = 0; /* a character */
        
        p = *ap; /* assign a pointer to a character to a pointer to a character */
        c = *ap[ 0 ]; /* assign a character to a character */
        c = ap[ 1 ][ 1 ]; /* likewise... */
        
        return 0;
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    9
    I have something that works but I'm still not completely sure why my previous code did not. I think it is because you cannot change the pointer unless you pass a pointer to the pointer.


    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define LIM 10
    #define MAX 81
    
    void print_arrs(char *strs[MAX], int num);
    void swap_onetwo(char *strs[MAX], int num);
    
    int main(void)
    {
            char arr[LIM][MAX];
            char *strs[LIM];
    
            int cnt = 0;
    
            puts("Enter a string, blank to quit");
            while ((gets(arr[cnt]) && arr[cnt][0] != '\0')) {
                    strs[cnt] = arr[cnt];
                    if (++cnt == 10)
                            break;
                    puts("Enter a string, blank to quit");
            }
    
            print_arrs(strs, cnt);
            swap_onetwo(strs, cnt);
            print_arrs(strs, cnt);
    
            return 0;
    }
    
    void swap_onetwo(char *strs[MAX], int num)
    {
            char *tmp = NULL;
    
            tmp = strs[0];
            strs[0] = strs[1];
            strs[1] = tmp;
    
            return;
    }
    
    void print_arrs(char *strs[MAX], int num)
    {
            int i;
    
            for (i = 0; i < num; i++)
                    puts(strs[i]);
            return;
    }
    I've created an array of pointers and set them to point to the beginning of each string. Is this the right way to do it? Thanks.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you mean your post #5, that didn't work because you were trying to assign array names (that is, strs[0] was an array and strs[1] was an array).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pass an string of array in a function
    By kantida in forum C Programming
    Replies: 1
    Last Post: 04-29-2011, 04:50 AM
  2. Passing a string array to a function using pointers
    By asofaihp in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2009, 11:31 AM
  3. returning a string array from a function
    By steve1_rm in forum C Programming
    Replies: 15
    Last Post: 05-07-2008, 09:14 AM
  4. binary search function on string array
    By gandolf989 in forum C Programming
    Replies: 6
    Last Post: 10-02-2007, 01:47 PM
  5. store function in string array? possible?
    By sappy in forum C++ Programming
    Replies: 2
    Last Post: 05-03-2005, 12:30 PM