4

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);
 }
Govardhan Murali
  • 91
  • 1
  • 1
  • 8

5 Answers5

8

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' */
sps
  • 2,720
  • 2
  • 19
  • 38
5

Two asterisks designate a pointer to a pointer. Here is why you need it in a program that sorts strings:

Recall that C represents strings as arrays of characters. Each C string is typically represented as a pointer to character, i.e. char*, so an array of C strings is an array of char*.

C standard sort algorithm implementation uses comparison function that takes pointers to array elements. Since each element is a pointer, the function, therefore, takes a pointer to pointer.

The pointer to pointer passed to compare is wrapped in void*, which allows casting to and from any data pointer. The first thing the comparison function does is casting void* back to char**, so that it could dereference the two:

char** sa=(char**)a; // a is a char**, so we do the cast.

Now the string at the left-hand side is found at *sa, and the string on the right is at *sb. That is what we pass to strcmp.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

The simplest, naive, explanation of char**:

char c = 'a';
char* p1 = &a;
char** p2 = &p2;

p2 is a pointer to a pointer to a char.

In an expression,

*p2 evaluates to a char*.

**p2 evaluates to a char.

However, there is more to it in the context of the function in your post. A "compare" function is used to sort objects. The standard library function qsort needs a function with same signature as your compare function to work correctly.

In your case, compare returns a value that can be used by qsort to sort an array of strings.

Given an array of strings such as:

char* strings[] = { ... }; // Initialize the strings

you can sort the strings by using

int numberOfStrings = ...;
qsort(strings, numberOfStrings, sizeof(*strings), compare);

qsort calls compare with the elements of strings by using a void* since qsort is agnostic of the type of data being held by the first argument. The only portable way it can call the compare function is by passing void*.

In the compare function, the user has to cast the pointers appropriately before making the comparisons.

When the compare function is expected to compare two strings, the void* needs to be cast to char** before calling strcmp on the dereferenced pointer.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

It is a pointer to (pointer to char). Consider this

char c='x';
char *ptr=&c;
char **ptr2ptr=&ptr;

Simplifying :

c is char
ptr is char*
*ptr is a char
ptr2ptr is char**
*ptr2ptr is char*
**ptr2ptr is char
sjsam
  • 21,411
  • 5
  • 55
  • 102
0

It's a double pointer(pointer to pointer). Double (**) is used to denote the double Pointer.

Double Pointer Stores the address of the Pointer Variable.

for ex.:

int num, *ptr1, **ptr2;
ptr1 = #
ptr2 = &ptr1;
msc
  • 33,420
  • 29
  • 119
  • 214