0

I am curious how to pass pointer to pointer in the function as an argument. I checked different question, for example - C: How to pass a double pointer to a function , there people discuss that in order to pass pointer to pointer you need to pass pointer to pointer to pointer. e.g if I have

char **lines= malloc(sizeof(char) * MAXWORDNUM);

    int y;
    for (y = 0; y < MAXWORDNUM; y ++)
    {
       lines[y] = malloc(sizeof(char) * MAXWORD);
    }

Do I need to declare a function

func(char ***lines)

? In my code I created a function in the following way -

func(char *lines[]) 

and it still works with

**lines

So what is the difference between these two ways? Is it correct to create function func(char *lines[]) for **lines?

Community
  • 1
  • 1
YohanRoth
  • 3,153
  • 4
  • 31
  • 58
  • This function call is a big problem: `malloc(sizeof(char) * MAXWORDNUM);`, because `sizeof(char)` is not the same as `sizeof(char*)`. – Some programmer dude Jul 19 '14 at 11:19
  • The accepted answer to the question you link (http://stackoverflow.com/questions/4339201/c-how-to-pass-a-double-pointer-to-a-function) shows you how to solve your issue. I tend to close your question as being a duplicate to the question you link. Please explain why your question is different. – alk Jul 19 '14 at 11:31
  • @alk Linked question does not resolve my question which is asking what is the difference between func(char ***lines) and func(char *lines[]) . I explained that in my code I used different way than described in linked question - and it works. My question is a comparison of two ways, while linked question is just about how to pass double pointers... – YohanRoth Jul 19 '14 at 11:42

2 Answers2

2

A function declared such as

some_type func(char ***lines);

takes an argument that is a pointer to a pointer to a pointer to a char.

A function such as

some_type func(char *lines[]);

taeks an argument that is apointer to a pointer to char. In other words, it's the same as

some_type func(char **lines);

I hope you see the difference?

For the first function, the expression **lines is a pointer to char, but for the second function the same expression is a char.


If you want emulate pass by reference with a pointer to pointer to some type, and allocate the matrix in the function, it's the first one is what you should do:

void func(char ***lines)
{
    *lines = malloc(sizeof(char*) * MAXWORDNUM);
    for (int i = 0; i < MAXWORDNUM; ++i)
        (*lines)[i] = malloc(MAXWORD);
}

Note that in the loop you have to use parentheses around *lines because of the operator precedence of the indexing operator [] than for the dereference operator *.

When you call it, you don't pass a variable declared as a tripple-pointer, instead you pass the address (using the address-of operator unary &) of a double-pointer:

char **lines;
func(&lines);
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • ok, thank you. I did a bit different... strangely enough but it worked. Not sure why. but thanks for showing the right way – YohanRoth Jul 19 '14 at 11:38
1

There is no much difference between func(char *lines[]) and func(char **lines). Both represents the 2D array. Let me explain you with a simple example.

#include<stdio.h>
// function for printing the char 2D array
void PRINT(char **array,int r) //here i am catching with char **
{
int i;
for(i=0;i<r;i++)
printf("%s\n",p[i]);
}

main()
{
char *array[]={"one","two","three"}; // for example i am taking 3 string. you can get these from user also after allocating memory dynamically.
PRINT(array,3); //were 3 is the no of strings.
}

In this function i am using char *[]

void PRINT(char *array[],int r)
{
int i;
for(i=0;i<r;i++)
printf("%s\n",p[i]);
}

For both cases output should be

one
two
three

Behavior of both char **array and char *array[] is same. no big differences between them.

Sathish
  • 3,740
  • 1
  • 17
  • 28