1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

char[][] instead of char*[] in calling a function

Discussion in 'C' started by misho123, Apr 21, 2011.

  1. misho123

    misho123 New Member

    Joined:
    Apr 21, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi All,

    I have a function that takes 'char *[]' as parameter:

    void myFunc(char *mystrings[]){}

    In order to call it I need to create an array of 'char *' and call it. (with malloc and all)

    Is there anyways to call this function with char[][] instead of char*[]? Something like this:

    char mystring[2][20];

    strcpy(mystring[0], "This is first line");
    strcpy(mystring[1], "This is second line");
    myFunc(mystring);


    I've tried myFunc(*mystring) and myFunc(**mystring) already.

    Thanks
     
  2. eriyer

    eriyer New Member

    Joined:
    Jan 22, 2011
    Messages:
    32
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include <stdio.h>
    
    /* a generic method to handle variable no of strings of variable length - */
    
    void genericstrhandler(char *strarray, int numstr, int stringlen);
    
    char strings[ 5 ][ 10 ] =
    							{
    								"one",
    								"two",
    								"three",
    								"four",
    								"five"
    							};
    void main()
    {
    	genericstrhandler(strings[ 0 ], 5, 10);
    }
    
    void genericstrhandler(char *strarray, int numstr, int stringlen)
    {
    	char *p;
    	int i;
    	
    	for( i = 0, p = strarray; i < numstr; i++, p += stringlen )
    	{
    		printf("%d : %s\n", i, p);
    	}
    	
    }
    
     

Share This Page