A pointer to a character pointer array... can't pass

This is a discussion on A pointer to a character pointer array... can't pass within the C Programming forums, part of the General Programming Boards category; I was working on another thread and here's what I came up with: Code: #include <stdio.h> #include <string.h> typedef char** ...

  1. #1
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533

    A pointer to a character pointer array... can't pass

    I was working on another thread and here's what I came up with:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef char** char1;
    typedef char1 *cptr;
    
    void str_swap(char** str1, char** str2);
    void bubble_sort(cptr array,int length)
    {
    	int i, j,s;
    	bool flag = 1;   
    	char* tmp;       
    	int arrayLength = length; 
    	for(i = 0; (i < arrayLength) && flag; i++)
    	{
    		flag = 0;
    		for (j=0; j < (arrayLength -1); j++)
    		{
    			s = strcmp(*array[j+1],*array[j]);
    			if(s>0)//string 2 is greater
    			{
    				str_swap(array[j+1],array[j]);
    			}
    		}
    	}
    }
    
    
    int main()
    {
    	char* str1= "Alfred";
    	char* str2 = "Albert";
    	char* strs[] = {str1,str2,"Linux","Penguin","Lorraine","Lucas"};
    	int len = 6;
    	for(int j=0;j<len;j++)
    		printf("%s ",strs[j]);
    	printf("\n");
    	bubble_sort(&strs,len);
    	for(j=0;j<len;j++)
    		printf("%s ",strs[j]);
    	printf("\n");
    	return 0;
    }
    
    void str_swap(char** str1, char** str2)
    {
    	char* tmp;
    	tmp = *str1;
    	*str1 = *str2;
    	*str2=tmp;
    }
    but it says cannot convert char(*)(*)[6] to char***
    I can't understand why it can't convert it. And do you know of a way I can pass the array of strings into the function and be able to MODIFY that array?

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stop using typedefs. That's probably your problem. Personally I never use them for just this sort of reason. Typedefs create a new data type, as such, you get wierd conversion problems when mixing them with their non-typedef equivalents.

    Just use #define. In C I can really don't see the need to ever use typedefs when you can just #define them. With #define you get the same effect, but it avoids all the type conversion issues since it's simple preprocessor text subsitution.

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

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,752
    Nothing wrong with typedefs

    bubble_sort(&strs,len);
    Type is char(*)(*)[6]

    bubble_sort(strs,len);
    Type is char ***

    In both cases, you're passing a pointer to the start of the array, but the TYPE of the pointer is different.

    > Just use #define.
    Renaming types using #define is a really bad idea

    Example
    Code:
    #define int_ptr   int *
    int_ptr a_ptr, b_ptr;
    b_ptr is really an int, not in int*, which casual reading of the code would suggest. If you used a typedef, this would not happen.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    62
    @quzah:

    Years ago, I had a long, but very interesting discussion with someone (cannot remember who, unfortunately) in this forum. The question was: "Does a typedef create a new type or not?"

    The answer is: No, a typedef does not create a new type. It is just an alias. See K&R A.8.9. ;-)

    My personal opinion is: Typedefs are okay as long as they help you to simplify a problem. However, do not use them too much. A bad example is the Win32 API, I think.

    Regards,
    R.

  5. #5
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    typedefs have a purpose, if they didn't and they were bad, they would be removed from the standard.

    who would want to type
    char*** cptrptrptr;
    many times for each one you define?
    it's easier to just
    typedef char*** char2;
    char2 cptrptrptr;
    and it helps you avoid some confusion.

    in my opinion, #define is VERY BAD to use for that instance.

    and another thing, I only used typedef because I was messing around trying to figure out how I could get it to work.

    now it crashes with an access violation (out of bounds I guess)

    the new code (LOL):
    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef char** char1;
    typedef char1 *cptr;
    
    void str_swap(char** str1, char** str2);
    void bubble_sort(cptr array,int length)
    {
    	int i, j,s;
    	bool flag = 1;   
    	char* tmp;       
    	int arrayLength = length; 
    	for(i = 0; (i < arrayLength) && flag; i++)
    	{
    		flag = 0;
    		for (j=0; j < (arrayLength -1); j++)
    		{
    			s = strcmp(*array[j+1],*array[j]);
    			if(s>0)//string 2 is greater
    			{
    				str_swap(array[j+1],array[j]);
    			}
    		}
    	}
    }
    
    
    int main()
    {
    	char* str1= "Alfred";
    	char* str2 = "Albert";
    	char* strs[] = {str1,str2,"Linux","Penguin","Lorraine","Lucas"};
    	int len = 6;
    	for(int j=0;j<len;j++)
    		printf("%s ",strs[j]);
    	printf("\n");
    	bubble_sort((cptr)&strs,len);
    	for(j=0;j<len;j++)
    		printf("%s ",strs[j]);
    	printf("\n");
    	return 0;
    }
    
    void str_swap(char** str1, char** str2)
    {
    	char* tmp;
    	tmp = *str1;
    	*str1 = *str2;
    	*str2=tmp;
    }
    any ideas?

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here's your code, working, minus the typedefs.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef int bool;
    
    void        str_swap(char **str1, char **str2);
    void bubble_sort(char *array[], int length)
    {
      int   i, j, s;
      bool  flag = 1;
      int   arrayLength = length;
      for (i = 0; (i < arrayLength) && flag; i++)
      {
        flag = 0;
        for (j = 0; j < (arrayLength - 1); j++)
        {
          s = strcmp(array[j + 1], array[j]);
          if (s < 0)  //string 2 is greater
          {
            flag = 1;
            str_swap(&array[j + 1], &array[j]);
          }
        }
      }
    }
    
    int main(void)
    {
      char  *strs[] = { "Alfred", "Albert", "Linux", "Penguin", "Lorraine", "Lucas" };
      int   j, len = 6;
      for (j = 0; j < len; j++) printf("%s ", strs[j]);
      printf("\n");
      bubble_sort(strs, len);
      for (j = 0; j < len; j++) printf("%s ", strs[j]);
      printf("\n");
      return(0);
    }
    
    void str_swap(char **str1, char **str2)
    {
      char  *tmp;
      tmp = *str1;
      *str1 = *str2;
      *str2 = tmp;
    }
    To add your typedefs back in, simply go through and replace the relevant bits. But you two typedefs are definately bad style imo.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    yeah thanks, I don't like tyepdefs that much, I RARELY use them, but I thought I could 'trick' my compiler into accepting it. hehe bad style.

    But for all intended purposes it can be good in some cases.

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Originally posted by Lynux-Penguin
    typedefs have a purpose, if they didn't and they were bad, they would be removed from the standard.
    If that's so, what about gets()?
    1) It is bad,
    2) hasn't had it's definition fixed, and
    3) is still in the standard.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >char2 cptrptrptr;
    >and it helps you avoid some confusion.

    [opinion type=mine]
    ...Unless you are the poor SOB that comes across a char2 in some code you are maintaining. Then you get to track down its definition, which may be in some header file somewhere. But a char*** would mean the same thing and require no detective work. And you wouldn't even need to use an ugly name like cptrptrptr to know what it is.

    I think the best use for typedefs is with function pointers. About the only other place I care to use them is for platform-specific exact-sized integers. Anything else (structs, unions, enums), in my opinion, hides information from the programmer.(!)
    [/opinion]
    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
    Originally posted by Salem
    > Just use #define.
    Renaming types using #define is a really bad idea

    Example
    Code:
    #define int_ptr   int *
    int_ptr a_ptr, b_ptr;
    b_ptr is really an int, not in int*, which casual reading of the code would suggest. If you used a typedef, this would not happen.
    Yeah but who on earth typedefs pointers? Not me. Ug. I hate that with a passion. If I want a pointer, I'll add a little * next to the variable. I hate variable types which are defined as pointers. It bugs the hell out of me. Because then in the middle of the code you see something like:
    Code:
    mytype foo;
    
    foo->bar = blah;
    And my brain says, "No, don't do that!, there's no little star!"

    Pet peeve of mine. I hate defined pointer types.

    [edit=Just read Dave's post]
    Couldn't agree more. I hate poinder definitions!
    [/edit]

    Quzah.
    Last edited by quzah; 10-12-2003 at 11:55 PM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. pass the pointer of a two dimension array to a function
    By SoFarAway in forum C Programming
    Replies: 8
    Last Post: 04-13-2005, 06:43 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

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