Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does char** mean in a c program, can someone please give a correct explanation [duplicate]

Tags:

c

what does char** mean in a c program, can someone please give a correct explanation. I am looking at a function pointer related sort pointer and its a bit confusing.

  int compare(const void* a,const void* b)
 {
  char** sa=(char**)a;
  char** sb=(char**)b;
  return strcmp(*sa,*sb);
 }
like image 488
Govardhan Murali Avatar asked Oct 25 '25 06:10

Govardhan Murali


1 Answers

In C, a char** means pointer to a pointer to a character.


char c;

means c is a character.


char *cptr;

means

1. `*cptr` is a character
2. `cptr` is a pointer to a characer

 char **pptr;

means

1. `**pptr` is a character
2. `*pptr` is a pointer to a character 
3. `pptr` is a pointer to a pointer to a character

In your case:

char **sa and char **sb are pointer to pointer to characters.

And, *sa and *sb are pointer to characters.

strcmp takes two pointer to characters as arguments, so you are passing those two pointer to characters when you are calling strcmp as:

strcmp(*sa, *sb)

Just, in case if you are confused how to call this function, you need to do something like this to call it.

/* Two strings */
char st1[] = {'a', 'b', 'c', '\0'};
char st2[] = {'c', 'b', 'a', '\0'};

/* Call compare */
int ret;
ret = compare((void *) &st1, (void *) &st2);
/* Do something based on value of `ret' */
like image 119
sps Avatar answered Oct 26 '25 21:10

sps



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!